Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
yash2shail opened this issue Oct 3, 2018 · 7 comments
Open

Comments

@yash2shail
Copy link

No description provided.

@MechJosh0
Copy link

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
Copy link

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
Copy link
Author

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
Copy link
Author

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
Copy link

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
Copy link
Contributor

@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
Copy link
Author

@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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

5 participants