Skip to content
Go implementation of NKN client and wallet
Go
Branch: master
Clone or download
yilunzhang Prevent close of closed channel
Signed-off-by: Yilun <zyl.skysniper@gmail.com>
Latest commit fe151d2 Aug 17, 2019
Permalink
Type Name Latest commit message Commit time
Failed to load latest commit information.
examples update for mainnet Jul 10, 2019
.gitignore initial commit Jan 2, 2019
LICENSE Create LICENSE Dec 30, 2018
README.md update for mainnet Jul 10, 2019
client.go Prevent close of closed channel Aug 20, 2019
common.go update for mainnet Jul 10, 2019
go.mod update for mainnet Jul 10, 2019
go.sum update for mainnet Jul 10, 2019
nanopay.go update for mainnet Jul 10, 2019
wallet.go update for mainnet Jul 10, 2019

README.md

nkn-sdk-go

Go implementation of NKN SDK.

Note: This repository is in the early development stage and may not have all functions working properly. It should be used only for testing now.

Usage

Before you use SDK please call:

Init()

Client Usage

Create a client with a generated key pair:

account, _ := vault.NewAccount()
client, _ := NewClient(account, "")

Or with an identifier (used to distinguish different clients sharing the same key pair):

client, _ := NewClient(account, "any string")

Get client key pair:

fmt.PrintLn(account.PrivateKey, account.PublicKey)

Create a client using an existing private key:

privateKey, _ := common.HexStringToBytes("039e481266e5a05168c1d834a94db512dbc235877f150c5a3cc1e3903672c67352dff44c21790d9edef7a7e3fc9bd7254359246d0ae605a3c97e71aad83d6b0d")
account, _ := vault.NewAccountWithPrivatekey(privateKey)
client, _ := NewClient(account, "any string")

By default the client will use bootstrap RPC server (for getting node address) provided by us. Any NKN full node can serve as a bootstrap RPC server. To create a client using customized bootstrap RPC server:

client, _ := NewClient(account, "any string", ClientConfig{SeedRPCServerAddr: "https://ip:port"})

Private key should be kept SECRET! Never put it in version control system like here.

Get client NKN address, which is used to receive data from other clients:

fmt.Println(client.Address)

Listen for connection established:

<- client.OnConnect
fmt.Println("Connection opened.")

Send text message to other clients:

client.Send([]string{"another client address"}, []byte("hello world!"))

You can also send byte array directly:

client.Send([]string{"another client address"}, []byte{1, 2, 3, 4, 5})

Or publish text message to a bucket 0 of specified topic:

client.Publish("topic", 0, []byte("hello world!"))

Receive data from other clients:

msg := <- client.OnMessage
fmt.Println("Receive binary message from", msg.Src + ":", string(msg.Payload))

Listen for new blocks mined:

block := <- client.OnBlock
fmt.Println("New block mined:", block.Header.Height)

Wallet Usage

Create wallet SDK:

account, _ := vault.NewAccount()
w := NewWalletSDK(account)

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

account, _ := vault.NewAccount()
w := NewWalletSDK(account, WalletConfig{SeedRPCServerAddr: "https://ip:port"})

Query asset balance for this wallet:

balance, err := w.Balance()
if err == nil {
    log.Println("asset balance for this wallet is:", balance.String())
} else {
    log.Println("query balance fail:", err)
}

Transfer asset to some address:

address, _ := account.ProgramHash.ToAddress()
txid, err := w.Transfer(address, "100")
if err == nil {
    log.Println("success:", txid)
} else {
    log.Println("fail:", err)
}

Open nano pay channel to specified address:

np, err := w.NewNanoPay(address)
if err == nil {
    log.Println("success:", txid)
} else {
    log.Println("fail:", err)
}

Send 100 NKN into channel with claim available for next 100 blocks and channel open for next 200 blocks

txid, err = np.Send("100", 100, 200)
if err == nil {
    log.Println("success:", txid)
} else {
    log.Println("fail:", err)
}

Register name for this wallet (only a-z and length 8-12):

txid, err = w.RegisterName("somename")
if err == nil {
    log.Println("success:", txid)
} else {
    log.Println("fail:", err)
}

Delete name for this wallet:

txid, err = w.DeleteName("somename")
if err == nil {
    log.Println("success:", txid)
} else {
    log.Println("fail:", err)
}

Subscribe to bucket 0 of specified topic for this wallet for next 10 blocks:

txid, err = w.Subscribe("identifier", "topic", 0, 10, "meta")
if err == nil {
    log.Println("success:", txid)
} else {
    log.Println("fail:", err)
}

Subscribe to first available bucket of specified topic for this wallet for next 10 blocks:

txid, err = w.SubscribeToFirstAvailableBucket("identifier", "topic", 10, "meta")
if err == nil {
    log.Println("success:", txid)
} else {
    log.Println("fail:", err)
}

Resolve name to wallet address:

address, _ := w.GetAddressByName("somename")

Get subscribers of bucket 0 of specified topic:

subscribers, _ := w.GetSubscribers("topic", 0)

Get first available bucket of specified topic:

bucket, _ := w.GetFirstAvailableTopicBucket("topic")

Get buckets count of specified topic:

bucket, _ := w.GetTopicBucketsCount("topic")

Contributing

Can I submit a bug, suggestion or feature request?

Yes. Please open an issue for that.

Can I contribute patches?

Yes, we appreciate your help! To make contributions, please fork the repo, push your changes to the forked repo with signed-off commits, and open a pull request here.

Please sign off your commit. This means adding a line "Signed-off-by: Name " at the end of each commit, indicating that you wrote the code and have the right to pass it on as an open source patch. This can be done automatically by adding -s when committing:

git commit -s

Community

You can’t perform that action at this time.