1.0.1 • Published 6 years ago
nanjs v1.0.1
PREVIEW RELEASE This is a beta preview release with breaking changes! The current version is 1.0.0
Nanjs - JavaScript API
This is the Nanjs JavaScript API which connects to the Nanj-coin system.
Building
Features
- Create NANJ Wallet
- Import NANJ Wallet via Private Key / Key Store
- Export Private Key/ Key Store from NANJ Wallet
- Generate NANJ Address
- Generate QRCode from Wallet Address
- Generate Hash Sign
- Get NANJ Rate
- Get NANJ Balance
Requirements
- Node.js
- npm
Installation
/directory/your-project$ npm install nanjs
Set config file
/directory/your-project$ cp .env.example .env
API
(static) init
var nanjs = require('nanjs')
createWallet(): Create Wallet
/*
It's a async function
1. generate address from mnemonic
2. password default is 123456789
3. options kdf
*/
var mnemonic = nanjs.wallet.generateMnemonic()
var password = '123456789'
var options = {
kdf: "scrypt", // or "pbkdf2" to use the pbkdf2 kdf
cipher: "aes-128-ctr",
kdfparams: {
n: 8192,
r: 8,
p: 1
}
}
console.log( await nanjs.wallet.createWallet(mnemonic, password, options) )
The createWallet
function is used to export key info to keystore "secret-storage" format.
importWallet(): Import Wallet
// It's a async function
// generate address from mnemonic
var mnemonic = 'brain surround have swap horror body response double fire dumb bring hazard'
var password = '123456789'
console.log( nanjs.wallet.importWallet(mnemonic, password) )
Generate NANJ Address
// It's a async function
// generate nanj address
var address = '0xe79c03e29ee86c1d0af6053737dccb029402d0f3'
var privateKey = '0541a5d81178f67887203996fe596b4fd3de72244e86a371e295f660aab0f039'
nanjs.wallet.generateAddress(address, privateKey).then(function(nanjAddress) {
console.log(nanjAddress)
}, function(err) {
console.log(err)
})
Get NANJ Balance
// Get NANJ Balance
var address = '0xe79c03e29ee86c1d0af6053737dccb029402d0f3'
nanjs.wallet.nanjBalance(address).then(function(response) {
console.log(response)
}, function(err) {
console.log(err)
})
Generate QRCode from Wallet Address
// Generate QRCode
// Response is a base64 image
var address = '0xe79c03e29ee86c1d0af6053737dccb029402d0f3'
nanjs.wallet.QRCodeAddress(address).then(function(response) {
console.log(response);
done();
}, function(err) {
console.log(err)
done();
})
// Using in html: <img src="base64Url">
Generate Hash Sign
/**
* It's a async function
* dataHash have dest, hash, data, v, r, s, nonce
*/
var from = '0xe79c03e29ee86c1d0af6053737dccb029402d0f3'
var privateKey = '0541a5d81178f67887203996fe596b4fd3de72244e86a371e295f660aab0f039'
var to = '0xfce1759a46647adfe4f9564320631c4f0a90deba'
var amount = 5
var message = 'nanj transaction'
var nonce = process.env.NANJ_HOST+process.env.PATH_RELAY_NONCE+'?sender='+from
var txHash = await nanjs.transaction.getRelayerTxHash(from, to, amount, message, nonce)
var dataHash = await nanjs.transaction.getHashSign(txHash.data, txHash.hash, privateKey, txHash.destinationAddress)
console.log(dataHash)
Testing (mocha)
npm test
Versioning
For the versions available, see the tags on this repository.
Acknowledgements
Inspired by code from the following great projects:
- https://www.npmjs.com/package/eth-hd-wallet
- https://github.com/ConsenSys/eth-lightwallet
- https://nanjcoin.com/sdk
Authors
- NANJ TEAM NANJ, support@nanjcoin.com