neo-go-sdk open-sourced by Jarvis+ Team

Jarvis+
4 min readJun 25, 2018

--

Jarvis+, the World’s 1st Smart Contract based AI Conversation as Service Platform, is invested by NGC (Neo Global Capital) and will soon provide rich functions to NEO communities, including:

· Create/use NEP5 smart contracts in natural languages;

· Exchange NEP5 assets with natural languages;

· NEO Community events/service/management support with AI bots;

During the development, Jarvis+ team notice that there is currently lack of Go Sdk for Neo and thus team have developed one and made it open source to serve the whole community:

https://github.com/x-contract/neo-go-sdk/

*X-Contract is the foundation name of Jarvis+ team

The neo-go-sdk provides basic functionality to Neo block chain. It’s designed to work with Neo-Cli node, not to relace it. With neo-go-sdk you can handle making kinds of transactions, create addresses, encryption and signature stuff, Neo script and so on. It makes developing Neo in Go more easy. Anyone interested in neo-go-sdk is welcomed to bugfix, improve, or fork the project. The major contributor of this neo-go-sdk is Terender (https://www.reddit.com/user/terenderZhang), pls feel free to discuss with him about the sdk.

Download

Just run the command

go get -u github.com/x-contract/neo-go-sdk

How to use

Neo-Cli Node

First of all you need a Neo-Cli full node. The neo-go-sdk calls Neo-Cli json-rpc APIs to gain access to block chain data.

You can create a node yourself, the tools and knowledge all you need to create a Neo-Cli node could be found here http://docs.neo.org/ . However you may want to have a quick start then you can directly use the official node without need for permission, these nodes are from http://seed1.neo.org:10332 to http://seed5.neo.org:10332. You can change port 10332 to 20332 for test net.

Algorithm, keys, addresses and script hashes etc.

Base58 and Hashes

endata, checksum := neoutils.EncodeBase58WithChecksum(data)data, checked := neoutils.DecodeBase58WithChecksum(endata)hash256 := neoutils.Hash256(data)hash160 := neoutils.Hash160(data)

Get a private key from WIF string

key, _ := neotransaction.DecodeFromWif(`KzSToRnDi9V********************************`)

Encode private key to WIF string

wif := key.EncodeWif()

Create new key pair and new account address with it

key := neotransaction.GenerateKeyPair()addr := key.CreateBasicAddress()

Address to ScriptHash

addr, _ := neotransaction.ParseAddress(“ASMGHQPzZqxFB2yKmzvfv82jtKVnjhp1ES”)scripthash := addr.ScripHash

Sign and CheckSign

sig, _ := key.Sign(data)check := key.Verify(data, sig)

Make a ContractTransaction (Transfer with common UTXO assets)

UTXO assets like NEO and GAS was transfered using a kind of transaction called ContractTransaction. It’s the basic transaction in all block chains.

If you want to make a ContractTransaction from your code you should first get the UTXOs for the account address which you want to transfer from. Unfortunately the Neo-Cli node does not provide the ability to get that from rpc APIs. So I have to achieve that by myself using a Block-Spider (or called Block-Listener).

Block-Spider fetch all the blocks in sequence and parse the transactions within those blocks to storage all the outputs data then filter all the UTXOs for any address. For now I’m using Block-Spider contributed by NEL (https://github.com/NewEconoLab/NEO_Block_API). The Go implemented Block-Spider is just on the way.

Make ContractTransaction is so easy

utxos, _ := neoextapi.FetchUTXO(config.NEOEXTURL, addr, neotransaction.AssetNeoID)tx := neotransaction.CreateContractTransaction()tx.AppendInput(utxos[0])tx.AppendOutput(taddr, utxos[0].AssetID, utxos[0].Value)txid := tx.TXID()tx.AppendBasicSignWitness(key)result := neocliapi.SendRawTransaction(config.NEOCLIURL, tx.RawTransactionString())

Make InvocationTransaction (Using Neo smart contract)

Neo smart contract is publish and invoke by InvocationTransaction, with the script that push params and call to specific contract hash. In fact, InvocationTransaction just invoke a slice of compiled NeoVM script regardless of what the script means. Call to another contract or publish a new contract is some NeoVM functions just like others, there’s nothing special except the cost GAS differs.

The result of the script is logged in the ApplicationLog, which you can get from the Neo-Cli node via rpc APIs. Remember to start the Neo-Cli node with — log args to enable the ApplicationLog.

Before you make a ContractTransaction you have to build your script invoked by the transaction. There’s a simple ScriptBuilder in neo-go-sdk that works with NeoVM OpCodes and parameters. Complicated contract script like NEP-5 ICO contract script should be written and compiled using tools from Neo and finally transferd to bytes, then impacted into a InvocationTransaction

Build script

sb := neotransaction.ScriptBuilder{}// If you want to make an invocation transaction without utxo transferd// then you need to push a random number so that the hash(txid) could vary on each transactionrand.Seed(time.Now().UnixNano())sb.EmitPushNumber(int64(rand.Uint32()))sb.Emit(OpCode.DROP)args := []interface{}{205, addr.ScripHash}sb.EmitPushArray(args)sb.EmitPushBool(false)sb.EmitPushString(`name`)contractHash, _ := hex.DecodeString(contractHashString)sb.EmitAppCall(contractHash)

Make InvocationTransaction

tx := neotransaction.CreateInvocationTransaction()extra := tx.ExtraData.(*neotransaction.InvocationExtraData)extra.Script = sb.Bytes()// If the transaction need additional Witness then put the ScriptHash in attributestx.AppendAttribute(neotransaction.UsageScript, addr.ScripHash)// Perhaps the transaction need Witnesstx.AppendBasicSignWitness(key)txid := tx.TXID()rawtx := tx.RawTransactionString()result := neocliapi.SendRawTransaction(neocliurl, rawtx)

Helper function to call Neo-Cli rpc APIs

There’s some helper function that making call to Neo-Cli rpc APIs more easily, just like:

neocliapi.FetchBalance(config.NEOCLIURL, user.UserNeoAddress.Addr)

This could get an account’s balance from a Neo-Cli node

TODO List

The list here contains the job that I am currently struggling with. There’s more work to do with the neo-go-sdk to make it more convenient to use :).

· Block Spider

· Nep6 Wallet and Private Key encryption

· Nep5 Contract asset support (Balance, Transfer, ICO etc.)

Website:

http://www.jarvisplus.io / http://www.jarvisplus.com

Twitter:

https://twitter.com/Jarvis33785861

Telegram:

https://telegram.im/jarvisplus

Discord:

https://discord.gg/pzesPmc

--

--

Jarvis+
Jarvis+

Written by Jarvis+

The World's 1st Smart Contract based AI Conversation as Service Platform.

No responses yet