Skip to content

[Deprecated, use nkn-sdk-js instead] JavaScript implementation of NKN wallet

License

Notifications You must be signed in to change notification settings

nknorg/nkn-wallet-js

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

89 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Note: This repo is deprecated in favor of nkn-sdk-js.

nkn-wallet-js

JavaScript implementation of NKN wallet.

Install

Node

npm i nkn-wallet

Browser

Use dist/nkn-wallet.js or dist/nkn-wallet.min.js.

If you use it in React Native, you also need to follow the installation guide in react-native-crypto.

Usage

  • import
// ES6
import nknWallet from 'nkn-wallet';

// ES5
var nknWallet = require('nkn-wallet');
  • Create a new wallet
const wallet = nknWallet.newWallet('password');
  • Get wallet's json string
const walletJson = wallet.toJSON();
  • Load wallet from a wallet json string
const walletFromJson = nknWallet.loadJsonWallet(walletJson, 'password');
  • Restore wallet from a private key
const walletFromSeed = nknWallet.restoreWalletBySeed(wallet.getSeed(), 'new-wallet-password');
  • Verify whether an address is valid
console.log(nknWallet.verifyAddress(wallet.address));
  • Verify password of the wallet
console.log(wallet.verifyPassword('password'));
  • Get balance of this wallet
wallet.getBalance()
  .then(function(value) {
    console.log('Balance for this wallet is:', value.toString());
  })
  .catch(function(error) {
    console.log('Get balance fail:', error);
  });
  • Transfer token to some address
wallet.transferTo(wallet.address, 1)
  .then(function(data) {
    console.log('Transfer success:', data);
  })
  .catch(function(error) {
    console.log('Transfer fail:', error);
  });
  • Register name for this wallet
wallet.registerName('some-name')
  .then(function(data) {
    console.log('Register name success:', data);
  })
  .catch(function(error) {
    console.log('Register name fail:', error);
  });
  • Delete name for this wallet
wallet.deleteName('some-name')
  .then(function(data) {
    console.log('Delete name success:', data);
  }).catch(function(error) {
    console.log('Delete name fail:', error);
  });
  • Subscribe to specified topic for this wallet for next 10 blocks
wallet.subscribe('topic', 10, 'identifier')
  .then(function(data) {
    console.log('Subscribe success:', data);
  }).catch(function(error) {
    console.log('Subscribe fail:', error);
  });

Check examples for full examples.

Configure

NKN wallet only stores some static information such as encrypted private keys, addresses and so on. All dynamic information needs to be queried from a NKN node. By default it will try to use RPC server provided by NKN, but you can change it by calling the global configure function:

nknWallet.configure({
  rpcAddr: 'http://127.0.0.1:30003',
})

Note that configure is optional. If you don't call configure(), default configurations will be used.

API

  • nknWallet
/***
 * global configuration:
 * {
 *  rpcAddr:'',
 * }
 *
 * @param config | Object
 */
nknWallet.configure(config)
/**
 * create a new wallet
 * @param password : string : the password to encrypt wallet
 * @returns {NknWallet} : a NknWallet instance
 */
nknWallet.newWallet(password)
/***
 * load wallet from json string
 * @param walletJson : string : a json format wallet
 * @param password : string : password for this wallet
 * @returns {NknWallet | null} : return NknWallet instance or null if key information is missing.
 *
 * !this method will thow an error if the password is wrong!
 */
nknWallet.loadJsonWallet(walletJson, password)
/***
 * restore a wallet from private key
 * @param privateKey : string : the private key for wallet restore
 * @param password : string : password for new wallet
 * @returns {NknWallet} : a NknWallet instance
 */
nknWallet.restoreWalletBySeed(privateKey, password)
/***
 * verify whether an address is valid
 * @param address : string : an address
 * @returns {boolean} : verifies whether an address is valid
 */
nknWallet.verifyAddress(address)
  • NknWallet

All of the following methods are instance methods

/***
 * generate wallet json
 * @returns {string} : wallet json
 */
toJSON()
/***
 * get the public key of this wallet
 * @returns {string} : the public key of this wallet
 */
getPublicKey()
/***
 * verify password of the wallet
 * @param password : string : password for this wallet
 * @returns {boolean} : verifies whether the password is correct
 */
verifyPassword(password)
/***
 * get the private key of this wallet
 * @returns {string} : the private key of this wallet
 *
 * !!! anyone with the private key has the power to restore a full-featured wallet !!!!
 */
getPrivateKey()
/***
 * transfer nkn to some valid address
 * @param toAddress : string : valid nkn address
 * @param value : number : value for transfer
 *
 * !!! the fail function will be called for any transfer errors  
 *     and the parameter applied is a WalletError instance. !!!
  */
transferTo(toAddress, value)
/***
 * register name on nkn for current wallet
 * @param name : string : name to register
 *
 * !!! the fail function will be called for any register errors  
 *     and the parameter applied is a WalletError instance. !!!
  */
registerName(name)
/***
 * delete name on nkn for current wallet
 * @param name : string : name to delete
 *
 * !!! the fail function will be called for any delete errors  
 *     and the parameter applied is a WalletError instance. !!!
  */
deleteName(name)
/***
 * subscribe to topic on nkn for current wallet
 * @param topic : string : topic to subscribe to
 * @param duration : number : subscription duration
 * @param identifier : string : optional identifier
 * @param meta : string : optional metadata
 *
 * !!! the fail function will be called for any register errors  
 *     and the parameter applied is a WalletError instance. !!!
  */
subscribe(topic, duration, identifier = '', meta = '', options = {})
/***
 * query balance
 * @returns {promise} : if resolved, the parameter is a decimal.js instance
 */
getBalance()

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

About

[Deprecated, use nkn-sdk-js instead] JavaScript implementation of NKN wallet

Topics

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published