Cockatoo: TrustNote Payment & IoT Hackathon API
What is Cockatoo?
Cockatoo is TrustNote’s Payment & IoT Hackathon API providing a simple web application interface that gives you easy access to instant payment and online transactions from your wallet on any device, with anyone, in just a few lines of JavaScript code.
What I can do with Cockatoo?
Here are some of the sample codes that are developed using Cockatoo:
- Fusion Wallet — A web-based TTT wallet: https://github.com/fusionwallet/wallet
- Chrome Extension — A web-based TTT wallet developed as a Chrome browser extension: https://github.com/fusionwallet/chrome_extensions
- IoT Light — A python script that light up a light bulb for 10 minutes when receives TTT payment: https://github.com/trustnote/how-to-code/blob/master/samples/iot_light/task.py
How to create the TrustNote Wallet?
Before use the Cockatoo, you will need to create a valid TrustNote wallet address, which starts with installing the wallet base library using npm:
npm install wallet-base — save
Then, in a few lines of JavaScript code, you will be able to create your wallet and get your wallet address:
// Import the wallet base
const Client = require('wallet-base')// Generate the mnemonic code
let mnemonic = Client.mnemonic()// Generate the private key using the mnemonic code
let privkey = Client.xPrivKey(mnemonic)// Generate the public key using the private key
let walletPubkey = Client.walletPubKey(privkey, 0)// Generate the wallet address using the public key
let address = Client.walletAddress(walletPubkey, 0, 0)
Register the wallet
Register the wallet, the wallet address will be returned if successful. A wallet must be registered before use.
Method: POST
URL: http://cockatoo.trustnote.org/api/v1/account/register
- pubkey The public key of the wallet
Response:
- address Return the address of the wallet if the registration is successful
Example of parameters:
{“pubkey”: “xpub6BwQQwThGkpzP5uLJ8NMXMFLsFt1B7rqNyeNTB3VXGb95eoK1caM5JpmPAMg8vJQf7d86689qwtGeRC4KL4fVTvMtp9u5W8jo5V5GiRNMNo”}
Example of returned JSON object:
{“address”:”xxxxxx”}
Getting the balance of an address
Retrieve the balance of a TrustNote address, the total balance should include both the balance in stable state and the balance in pending state
Method: GET
URL: http://cockatoo.trustnote.org/api/v1/asset/balance/:address/:asset
- :address The TrustNote addtress to lookup
- :asset The name of the TRC20 based asset to lookup, TTT is the only supported asset name in Cockatoo API
Response:
- stable The balance of the address that is in stable state
- pending The balance of the address that is in pending state
Example of returned JSON object:
{“network”: “devnet”,“errCode”: 0,“errMsg”: “success”,“data”: {“stable”: 25,“pending”: 80451}}
Creating a payment message
Creating an unsigned payment message including the payment details. The payment transaction will only take place after the message is signed with the private key.
Method: POST
URL: http://cockatoo.trustnote.org/api/v1/asset/transfer
- payer The address of the payer’s wallet
- address The address of receiver’s wallet
- amount The amount of the asset to be transferred
Response:
- b64_to_sign the unsigned payment transaction message
- txid Id of the transaction
Example of parameters:
{“asset”: “TTT”,“payer”: “ZDKNB2DQJPQR7PKYI37A5M2MTU5SIZ2A”,“message”: “hello world”,“outputs”: [{“address”: “FYQXBPQWBPXWMJGCHWJ52AK2QMEOICR5”,“amount”: 5}]}
Example of returned JSON object:
{network”: “devnet”,“errCode”: 0,“errMsg”: “success”,“data”: {“b64_to_sign”: “iQjSol75QjDLtzapgxZBWPMgxJnRj2IoOO6pt41eBW8=”,“txid”: “XQ/8hrHpYgtVZxAHSdScUaQGjyVaKwsv52q2qmqLtQE=”}}
Signing a transaction
The unsigned payment message returned from the transfer API can be signed using the sign method from the wallet base library.
- message The message to be signed with
- privatekey The private key
- path The default path, must be specified as “m/44'/0'/0'/0/0”
// Sign a transaction
let sig = Client.sign(b64_to_sign, privkey, “m/44'/0'/0'/0/0”)
Committing the payment message
Committing a signed payment message to complete the payment transaction.
Method: POST
URL: http://cockatoo.trustnote.org/api/v1/asset/commit
- txid Id of the transaction
- sig The signed message
Response:
- errMsg Error message
Example of parameters:
{“txid”: “BWEGp9t1yKEttWrLkshn7b3brWMy/tHMdyisLFL/3ck=”,“sig”: “li4xPfMMbiMyw7YWGuiHjklWu6IPxQOnlB9S0rjlUkAplCyP5OrlfcjOWOuRO4Ua99cgCTI23wI6rg0outpUwA==”}
Example of returned JSON object:
{“network”: “devnet”,“errMsg”: “not enough asset: TTT from address OQXCPQ6NQVKPI46U2YFBY55EBCPAV3GF”,“data”: “null”}
Retrieving the transaction history
Creating an unsigned payment message including the payment details. The payment transaction will only take place after the message is signed with the private key.
Method: GET
URL: http://cockatoo.trustnote.org/api/v1/asset/txhistory/:address/:asset/:page/:itemsPerPage
- payer The address of the payer’s wallet
- address The address of receiver’s wallet
- amount The amount of the asset to be transferred
Response:
- b64_to_sign the unsigned payment transaction message
- txid Id of the transaction
Example of parameters:
{“asset”: “TTT”,“payer”: “ZDKNB2DQJPQR7PKYI37A5M2MTU5SIZ2A”,“message”: “hello world”,“outputs”: [{“address”: “FYQXBPQWBPXWMJGCHWJ52AK2QMEOICR5”,“amount”: 5}]}
Example of returned JSON object:
{“network”: “devnet”,“errCode”: 0,“errMsg”: “success”,“data”: {“unit”: {“unit”: “F8ofJgi8wokp0uIetxK/xwg3aAJ5t7Pvln2MNLGyS8M=”,“version”: “1.0”,“alt”: “1”,“witness_list_unit”: “MtzrZeOHHjqVZheuLylf0DX7zhp10nBsQX5e/+cA3PQ=”,“last_ball_unit”: “FU7+kILFFfH4UK2pIctXWbO6kNliAVP3toei/HHxbiE=”,“last_ball”: “HlVm3tqKy6hWYR3ia/Qat5oNeOxTbxW2siBwhGtdmrc=”,“headers_commission”: 714,“payload_commission”: 358,“main_chain_index”: 153960,“timestamp”: 1539789830,“parent_units”: [“91IvDcfFy37zKdJ30WXbyUl6/bD2ip1Y6tmBUnK4YW8=”],“earned_headers_commission_recipients”: [{“address”: “OHLL5L5W57IROOH4A3GISUGSP6KMFBRQ”,“earned_headers_commission_share”: 100}],“authors”: [{“address”: “5AOABXFRL5AX3MWEPWKQ6QY3MY6A5TMH”,“authentifiers”: {“r”: “nC+l/MzXcqsYyHjurBqEUasUz3Eje8TF6XbIuKXZgWw0CsXBF8ORE+0EiHO4PdqGUijDtQ3XNCaa1OFT7I3NpA==”}}, {“address”: “OHLL5L5W57IROOH4A3GISUGSP6KMFBRQ”,“authentifiers”: {“r.0.0”: “nC+l/MzXcqsYyHjurBqEUasUz3Eje8TF6XbIuKXZgWw0CsXBF8ORE+0EiHO4PdqGUijDtQ3XNCaa1OFT7I3NpA==”},“definition”: [“or”, [[“and”, [[“address”, “5AOABXFRL5AX3MWEPWKQ6QY3MY6A5TMH”],[“in data feed”, [[“4VYYR2YO6NV4NTF572AUBEKJLSTM4J4E”], “timestamp”, “>”, 1531299600000]]]],[“and”, [[“address”, “752L4B7Y7WQF3BRFEI2IGIN5RDZE54DM”],[“in data feed”, [[“4VYYR2YO6NV4NTF572AUBEKJLSTM4J4E”], “timestamp”, “=”, 0]]]]]]}],“messages”: [{“app”: “payment”,“payload_hash”: “FOfA6SWV/0UVgefXXHMsGk0U4u7lNqo5ewVwZLhNaMc=”,“payload_location”: “inline”,“payload”: {“inputs”: [{“unit”: “4Cq1KWx1vmXO1L35F6cUj6yXilweVgBn9lCG6d/MLa4=”,“message_index”: 0,“output_index”: 1}],“outputs”: [{“address”: “OHLL5L5W57IROOH4A3GISUGSP6KMFBRQ”,“amount”: 88928}]}}, {“app”: “payment”,“payload_hash”: “grQJshnOKYhYUQCuS1tXEJ7X3schjU5FyGdJ8uvUUy8=”,“payload_location”: “inline”,“payload”: {“inputs”: [{“unit”: “eap7glIf3PDZ95doA+ngk3vdhFDUhhoQmBn+Cj5SU/A=”,“message_index”: 1,“output_index”: 0}],“asset”: “7acKn25O/OuxUHJFXFHOACvNWpSDejx/BzxcWsQ8qzY=”,“outputs”: [{“address”: “LVP5X4PB2T757EIWJPACVLACLOOEMAVV”,“amount”: 150}]}}]},“ball”: “ZEHEija1zDNT2Bc52pmmrEZ+Az89lkLAnEpiClUxt94=”,“skiplist_units”: [“KkXI52tTaQSiOz6buEK7i/rJEYuz6rGW7tCGfeqms0M=”],“arrShareDefinition”: [{“arrDefinition”: [“or”, [[“and”, [[“address”, “5AOABXFRL5AX3MWEPWKQ6QY3MY6A5TMH”],[“in data feed”, [[“4VYYR2YO6NV4NTF572AUBEKJLSTM4J4E”], “timestamp”, “>”, 1531299600000]]]],[“and”, [[“address”, “752L4B7Y7WQF3BRFEI2IGIN5RDZE54DM”],[“in data feed”, [[“4VYYR2YO6NV4NTF572AUBEKJLSTM4J4E”], “timestamp”, “=”, 0]]]]]],“assocSignersByPath”: {“r.0.0”: {“device_address”: “0YJBUFXU6NX3YII2NZWXBE63CMCKH53BM”,“address”: “5AOABXFRL5AX3MWEPWKQ6QY3MY6A5TMH”,“member_signing_path”: “r”},“r.1.0”: {“device_address”: “0IQQISCHCS7OHLNPSZKG2W4CLJCNV3QXW”,“address”: “752L4B7Y7WQF3BRFEI2IGIN5RDZE54DM”,“member_signing_path”: “r”}}