nkn_sdk_flutter 0.6.2 copy "nkn_sdk_flutter: ^0.6.2" to clipboard
nkn_sdk_flutter: ^0.6.2 copied to clipboard

Flutter NKN SDK. Send and receive data for free between any NKN clients regardless their network condition without setting up a server or relying on any third party services.

nkn-sdk-flutter #

Usage #

install #

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

Wallet #

Create wallet:

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

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

wallet.keystore;
copied to clipboard

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);
copied to clipboard

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);
                      
copied to clipboard

Query asset balance for this wallet:

double balance = await wallet.getBalance();
copied to clipboard

Transfer asset to some address:

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

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);
copied to clipboard

Listen for connection established:

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

Receive data from other clients

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

Send text message to other clients

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

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

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

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);
copied to clipboard
7
likes
150
points
20
downloads

Publisher

verified publishernkn.org

Weekly Downloads

2024.07.06 - 2025.01.18

Flutter NKN SDK. Send and receive data for free between any NKN clients regardless their network condition without setting up a server or relying on any third party services.

Repository (GitHub)

Documentation

API reference

License

Apache-2.0 (license)

Dependencies

convert, crypto, flutter, logger

More

Packages that depend on nkn_sdk_flutter