getAddresses test service for getting eth and bnc addresses by private key

This commit is contained in:
Kirill Fedoseev 2019-10-09 18:31:49 +03:00
parent fcfdce153c
commit 1793f6cf3b
4 changed files with 53 additions and 0 deletions

View File

@ -0,0 +1,11 @@
FROM node:10.16.0-alpine
WORKDIR /test
COPY package.json /test/
RUN npm install
COPY testGetAddresses.js /test/
ENTRYPOINT ["node", "testGetAddresses.js"]

View File

@ -0,0 +1,8 @@
{
"name": "get-addresses",
"version": "0.0.1",
"dependencies": {
"ethers": "4.0.37",
"bech32": "1.1.3"
}
}

View File

@ -0,0 +1,9 @@
#!/bin/bash
set -e
cd $(dirname "$0")
docker build -t get-addresses . > /dev/null
docker run --rm get-addresses $@

View File

@ -0,0 +1,25 @@
const { utils } = require('ethers')
const bech32 = require('bech32')
const crypto = require('crypto')
const privateKey = process.argv[2].startsWith('0x') ? process.argv[2] : '0x' + process.argv[2]
const ethAddress = utils.computeAddress(privateKey)
const publicKey = utils.computePublicKey(privateKey, true)
console.log(`Eth address: ${ethAddress}\nBnc address: ${publicKeyToAddress(publicKey)}`)
function publicKeyToAddress (publicKey) {
const sha256Hash = sha256(Buffer.from(publicKey.substr(2), 'hex'))
const hash = ripemd160(Buffer.from(sha256Hash, 'hex'))
const words = bech32.toWords(Buffer.from(hash, 'hex'))
return bech32.encode('tbnb', words)
}
function sha256 (bytes) {
return crypto.createHash('sha256').update(bytes).digest('hex')
}
function ripemd160 (bytes) {
return crypto.createHash('ripemd160').update(bytes).digest('hex')
}