Intro
- This is an SDK to interact with Coop Network Node.
- Main use is for local signing of transactions.
- Offers helper functions to easily call Coop Network node API.
- This SDK was initially designed for NodeJS. For running it in browser please see browser directory
- please read browser/readme.md and browser/index.html example
Instructions
- To run tests:
npm install npm run test - To use in your NodeJS app:
npm install gmd-nodejs-sdk
For browser usage please see browser directory readme.md and index.html example.
Examples on hot to call the GMD node API
- For a complete list of API endpoints and their exact parameters you can see them on any node in a browser at address /test (e.g. https://node.thecoopnetwork.io/test )
- All examples in this readme.txt can also be found and ran in test directory.
Wallet
//generate a random passphrase of 12 words
let passPhrase = Wallet.generatePassphrase(12);
//generate a wallet from a passphrase
//please note that wallet does not store the mnemonic passphrase, but only the generated private key and public key. Mnemonic passphrase cannot be recovered by using Wallet.
let wallet = await Wallet.fromPassphrase(passPhrase);
//Create encrypted JSON:
let encryptedJSON = await Wallet.encryptedJSONFromPassPhrase(passPhrase, "password example 123@@!");
//recover wallet from encrypted json:
let wallet = await Wallet.fromEncryptedJSON(encryptedJSON, "password example 123@@!");
Provider
- Provider is the component that haddles all interaction with a remote GMD node. It extends the RemoteAPICaller class wich enables the calling of the REST API exposed by a node. Full list of REST API endpoints https://node.thecoopnetwork.io/test:
- Usage example:
const provider = new Provider(new URL('https://node.thecoopnetwork.io:6877'));
const params = { requestType: 'getTransactionsBulk', pageSize: 3, page: 0}
const data = await provider.apiCall('get', paramsGetTransactions);
console.log('List of latest 3 transactions: ' + JSON.stringify(data, null, 2));
- Getting the blockchain height:
let blockNo = await provider.getBlockNumber();
console.log('Blockchain height: ' + blockNo);
Transaction
- Transaction is an abstract class that models all blockcahin transactions performed on the Coop Network blockchain.
- Any transaction has 5 steps:
- Create request JSON
- Process request JSON to an unsigned transaction (remote API call to a node is necessary)
- Sign the unsigned transaction
- Broadcast the signed transaction (remote API call to a node is necessary)
- [Optional] Transaction is confirmed after the trasaction is written to the blockchain and at leat one block is written on top of the transaction block (remote API call to a node is necessary).
The state of the transaction can only go through each step in the specified order as the output of each step is the input for the next step.
Each step can be executed on different device as long as the output of the previous step is somehow transmited out-of-band (e.g. via QR code). This mechanism is usefull when signing happens on a mobile device. - None of the outputs of the described steps contain any secret so exposing a QR is a safe operation.
For now, only concrete implementation of Transaction is "SendMoney" class, but further transactions will be added in the future.
Example on how to use "SendMoney":
provider = new Provider(new URL('https://node.coopnetwork.io:6877')); wallet = await Wallet.fromPassphrase('screen drawn leave power connect confidence liquid everytime wall either poet shook'); transaction = SendMoney.createTransaction('GMD-43MP-76UW-L69N-ALW39', '10000', wallet.publicKey); // Step 1 - local await provider.createUnsignedTransaction(transaction); // Step 2 - remote call await wallet.signTransaction(transaction); // Step 3 - local call await provider.broadcastTransaction(transaction); // Step 4 - remote call