nuls-io / nuls-v1

MIT License
361 stars 91 forks source link

Any Idea how to generate address of nuls coin in main net in node js? #34

Open yash2shail opened 6 years ago

MechJosh0 commented 6 years ago

Please don't spam your question(s) across multiple repositories.

You will need to use REST to send commands to the node, and it can be done like:

post(`${nodeAddress}/account`, {count: 1, password: "${addressPassword}"});
moshemalawach commented 6 years ago

As I replied in nuls.community (https://nuls.community/d/77-api-to-generate-address-in-terminal/8)

You'll need to npm install some packages, but I leave that to you :):

#!/usr/bin/node

const RIPEMD160 = require('ripemd160')
const secp256k1 = require('secp256k1')
const shajs = require('sha.js')
const getRandomValues = require('get-random-values')
const bs58 = require('bs58')

function getxor (body) {
  let xor = 0
  for (var i = 0; i < body.length; i++) {
    xor = xor ^ body[i]
  }
  return xor
}

function private_key_to_public_key (prv) {
  return secp256k1.publicKeyCreate(prv)
}

function public_key_to_hash (pub, { chain_id = 8964, address_type = 1 } = {}) {
  let sha = new shajs.sha256().update(pub).digest()
  let pubkeyHash = new RIPEMD160().update(sha).digest()
  let output = Buffer.allocUnsafe(3)
  output.writeInt16LE(chain_id, 0)
  output.writeInt8(address_type, 2)
  return Buffer.concat([output, pubkeyHash]) //.toString('hex')
}

function address_from_hash (hash) {
  //const bytes = Buffer.from(hash, 'hex')
  const address = bs58.encode(Buffer.concat([hash, new Buffer([getxor(hash)])]))
  return address
}

function generate() {
  let randArr = new Uint8Array(32) //create a typed array of 32 bytes (256 bits)
  let privKey
  do {
    getRandomValues(randArr)
    privKey = Buffer.from(randArr)
  } while (!secp256k1.privateKeyVerify(privKey))
  return privKey
}

var numberOfWallets = 5;

for (var i = 0; i < numberOfWallets; i++) {
  let privateKey = generate();
  let pub = private_key_to_public_key(privateKey);
  let hash = public_key_to_hash(pub);
  let address = address_from_hash(hash);
  console.log(address,privateKey.toString('hex'));
};
yash2shail commented 6 years ago

Please don't spam your question(s) across multiple repositories.

You will need to use REST to send commands to the node, and it can be done like:

post(`${nodeAddress}/account`, {count: 1, password: "${addressPassword}"});

Sorry for that but actually we are working in this coin in very urgent basis and it is a new coin.And as i was not getting any help in node js so posted here ..Thanks for your reply......

yash2shail commented 6 years ago

As I replied in nuls.community (https://nuls.community/d/77-api-to-generate-address-in-terminal/8)

You'll need to npm install some packages, but I leave that to you :):

#!/usr/bin/node

const RIPEMD160 = require('ripemd160')
const secp256k1 = require('secp256k1')
const shajs = require('sha.js')
const getRandomValues = require('get-random-values')
const bs58 = require('bs58')

function getxor (body) {
  let xor = 0
  for (var i = 0; i < body.length; i++) {
    xor = xor ^ body[i]
  }
  return xor
}

function private_key_to_public_key (prv) {
  return secp256k1.publicKeyCreate(prv)
}

function public_key_to_hash (pub, { chain_id = 8964, address_type = 1 } = {}) {
  let sha = new shajs.sha256().update(pub).digest()
  let pubkeyHash = new RIPEMD160().update(sha).digest()
  let output = Buffer.allocUnsafe(3)
  output.writeInt16LE(chain_id, 0)
  output.writeInt8(address_type, 2)
  return Buffer.concat([output, pubkeyHash]) //.toString('hex')
}

function address_from_hash (hash) {
  //const bytes = Buffer.from(hash, 'hex')
  const address = bs58.encode(Buffer.concat([hash, new Buffer([getxor(hash)])]))
  return address
}

function generate() {
  let randArr = new Uint8Array(32) //create a typed array of 32 bytes (256 bits)
  let privKey
  do {
    getRandomValues(randArr)
    privKey = Buffer.from(randArr)
  } while (!secp256k1.privateKeyVerify(privKey))
  return privKey
}

var numberOfWallets = 5;

for (var i = 0; i < numberOfWallets; i++) {
  let privateKey = generate();
  let pub = private_key_to_public_key(privateKey);
  let hash = public_key_to_hash(pub);
  let address = address_from_hash(hash);
  console.log(address,privateKey.toString('hex'));
};

Thanks for the help I have successfully generated the public and private key....

shweta037 commented 6 years ago

As I replied in nuls.community (https://nuls.community/d/77-api-to-generate-address-in-terminal/8)

You'll need to npm install some packages, but I leave that to you :):

#!/usr/bin/node

const RIPEMD160 = require('ripemd160')
const secp256k1 = require('secp256k1')
const shajs = require('sha.js')
const getRandomValues = require('get-random-values')
const bs58 = require('bs58')

function getxor (body) {
  let xor = 0
  for (var i = 0; i < body.length; i++) {
    xor = xor ^ body[i]
  }
  return xor
}

function private_key_to_public_key (prv) {
  return secp256k1.publicKeyCreate(prv)
}

function public_key_to_hash (pub, { chain_id = 8964, address_type = 1 } = {}) {
  let sha = new shajs.sha256().update(pub).digest()
  let pubkeyHash = new RIPEMD160().update(sha).digest()
  let output = Buffer.allocUnsafe(3)
  output.writeInt16LE(chain_id, 0)
  output.writeInt8(address_type, 2)
  return Buffer.concat([output, pubkeyHash]) //.toString('hex')
}

function address_from_hash (hash) {
  //const bytes = Buffer.from(hash, 'hex')
  const address = bs58.encode(Buffer.concat([hash, new Buffer([getxor(hash)])]))
  return address
}

function generate() {
  let randArr = new Uint8Array(32) //create a typed array of 32 bytes (256 bits)
  let privKey
  do {
    getRandomValues(randArr)
    privKey = Buffer.from(randArr)
  } while (!secp256k1.privateKeyVerify(privKey))
  return privKey
}

var numberOfWallets = 5;

for (var i = 0; i < numberOfWallets; i++) {
  let privateKey = generate();
  let pub = private_key_to_public_key(privateKey);
  let hash = public_key_to_hash(pub);
  let address = address_from_hash(hash);
  console.log(address,privateKey.toString('hex'));
};

Do you have any idea for the api for the nuls coin to get balance for mainnet....i have found this but not getting balance and utxo details... https://apiserver.nuls.io/nuls/tx?address=Nsdugx6ha5RypuC9nEBzbaSJV6WN4Y3t..

Thanks for the above code helped to generate the address..

daviyang35 commented 6 years ago

@shweta037 You want get balance for your address.

  1. Running wallet program
  2. Import your address into wallet
  3. Use wallet RPC interface do that.
yash2shail commented 6 years ago

@shweta037 You want get balance for your address.

  1. Running wallet program
  2. Import your address into wallet
  3. Use wallet RPC interface do that.

I got the balance using:-

nuls.getTxListByAddress({"address":"Nse2vPEzc2wvjKXUoRbw1JmyTDjNmpbT"},function(data){ console.log("This is the Transaction List----------" +JSON.stringify(data.data.list)); });

now I need to do transaction in mainnet without using password in node.js