Skip to content

iheron/nkn-sdk-flutter

 
 

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

74 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

nkn-sdk-flutter

Usage

install

void main() {
  WidgetsFlutterBinding.ensureInitialized();
  Wallet.install();
  Client.install();
  runApp(MyApp());
}

Wallet

Create wallet:

Wallet wallet = await Wallet.create(null, config: WalletConfig(password: '123'));
print(wallet.address);
print(wallet.seed);
print(wallet.publicKey);

Export wallet to JSON string, where sensitive contents are encrypted by password provided in config:

wallet.keystore;

By default the wallet will use RPC server provided by nkn.org. Any NKN full node can serve as a RPC server. To create a wallet using customized RPC server:

Wallet wallet = await Wallet.create(null, config: WalletConfig(password: '123', seedRPCServerAddr: ['http://seed.nkn.org:30003']));
print(wallet.address);
print(wallet.seed);
print(wallet.publicKey);

Load wallet from JSON string, note that the password needs to be the same as the one provided when creating wallet:

Wallet wallet = await Wallet.restore(w.keystore,
    config: WalletConfig(password: '123'));
print(wallet.address);
print(wallet.seed);
print(wallet.publicKey);
                      

Query asset balance for this wallet:

double balance = await wallet.getBalance();

Transfer asset to some address:

String txHash = await wallet.transfer(NKN_ADDRESS, '1.23');

Client

NKN Client provides low level p2p messaging through NKN network.

Create a client with a generated key pair:

var client = await Client.create(WALLET_SEED);

Listen for connection established:

client.onConnect.listen((event) {
    print(event.node);
});

Receive data from other clients

client.onMessage.listen((event) {
    print(event.type);
    print(event.encrypted);
    print(event.messageId);
    print(event.data);
    print(event.src);
});

Send text message to other clients

await client.sendText([CLIENT_ADDRESS], jsonEncode({'contentType': 'text', 'content': 'Hello'}));

publish a message to a specified topic (see wallet section for subscribing to topics):

await client.publishText(TOPIC, jsonEncode({'contentType': 'text', 'content': 'Hello'}));

Pub/Sub

var res = await client.subscribe(topic: TOPIC);
var res = await client.unsubscribe(topic: TOPIC);
var res = await client.getSubscribersCount(topic: TOPIC);
var res = await client.getSubscription(topic: TOPIC, subscriber: CLIENT_ADDRESS);

About

No description, website, or topics provided.

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages

  • Objective-C 37.4%
  • Swift 22.6%
  • Kotlin 18.8%
  • Dart 17.4%
  • Go 1.5%
  • C 1.0%
  • Other 1.3%