0

We're working with a ICO project based on NEO Blockchain. We've requirement of getting payments through NEO, ETH and BTC. We're using deposit address concept to get payments from the users, and for that we have been using some Node js to create BTC address as below:

var express = require('express');
var app = express();
var fs = require("fs");
var bitcoin = require('bitcoinjs-lib');
var lightwallet = require('eth-lightwallet');

function generateBitcoinAddress(){
    console.log("generateBitcoinAddress");
    const crypto = require('crypto');
    const EC = require('elliptic').ec;
    const RIPEMD160 = require('ripemd160');
    const bs58 = require('bs58');
    const buffer = require('buffer');
    const ec = new EC('secp256k1');

    function hasha256(data) {
        return crypto.createHash('sha256').update(data).digest();
    } // A small function I created as there is a lot of sha256 hashing.

    const addrVer = Buffer.alloc(1, 0x00); // 0x00 P2PKH Mainnet, 0x6f P2PKH Testnet
    const wifByte = Buffer.alloc(1, 0x80); // 0x80 Mainnet, 0xEF Testnet

    var key = ec.genKeyPair();
    var privKey = key.getPrivate().toString('hex');
    var pubPoint = key.getPublic();
    var x = pubPoint.getX(); // elliptic x
    var y = pubPoint.getY(); // elliptic y

    // Private Key Hashing
    var bufPrivKey = Buffer.from(privKey, 'hex');
    var wifBufPriv = Buffer.concat([wifByte, bufPrivKey], wifByte.length + bufPrivKey.length);
    var wifHashFirst = hasha256(wifBufPriv);
    var wifHashSecond = hasha256(wifHashFirst);
    var wifHashSig = wifHashSecond.slice(0, 4);
    var wifBuf = Buffer.concat([wifBufPriv, wifHashSig], wifBufPriv.length + wifHashSig.length);
    var wifFinal = bs58.encode(wifBuf);

    // Public Key Hashing
    var publicKey = pubPoint.encode('hex');
    var publicKeyInitialHash = hasha256(Buffer.from(publicKey, 'hex'));
    var publicKeyRIPEHash = new RIPEMD160().update(Buffer.from(publicKeyInitialHash, 'hex')).digest('hex');
    var hashBuffer = Buffer.from(publicKeyRIPEHash, 'hex');
    var concatHash = Buffer.concat([addrVer, hashBuffer], addrVer.length + hashBuffer.length);
    var hashExtRipe = hasha256(concatHash);
    var hashExtRipe2 = hasha256(hashExtRipe);
    var hashSig = hashExtRipe2.slice(0, 4);
    var bitcoinBinaryStr = Buffer.concat([concatHash, hashSig], concatHash.length + hashSig.length);

    var bitcoinWifAddress = wifFinal.toString('hex');
    var bitcoinAddress = bs58.encode(Buffer.from(bitcoinBinaryStr));
    data={};
    data.WIFPrivateKey = bitcoinWifAddress.toString('hex');
    data.BitcoinAddress =bitcoinAddress.toString('hex');
    return data;
}

app.get('/generateBTCAddress', function (req, res) {
      res.send(generateBitcoinAddress());
})

app.get('/', function(req, res) {
    console.log("welcome");
    var wifkey = bitcoin.ECPair.makeRandom().toWIF();
    var keyPair = bitcoin.ECPair.fromWIF('L22fA7nJgiig78A8nubSAQPwYp4Fnqk4W6FUen7GWsQxp2H4UD2V');
    var data = {};
    data.address = keyPair.getAddress();
    data.wifkey  = wifkey;
    data.message = "Welcome to Home";
    res.send(data);
});

app.get('/generateETHAddress', function(req, res) {
    var password = Math.random().toString();
    var seed = lightwallet.keystore.generateRandomSeed();

    lightwallet.keystore.createVault({
        password: password,
        seedPhrase: seed,
        hdPathString: "m/0'/0'/0'"
    }, function (err, ks) {

    ks.keyFromPassword(password, function (err, pwDerivedKey) {
        if (!ks.isDerivedKeyCorrect(pwDerivedKey)) {
            throw new Error("Incorrect derived key!");
        }

        try {
            ks.generateNewAddress(pwDerivedKey, 1);
        } catch (err) {
            console.log(err);
            console.trace();
        }
        var result = {};
        result.address = ks.getAddresses()[0];
        result.prv_key = ks.exportPrivateKey(result.address, pwDerivedKey);
        res.send(result);
        });
    });
})

var server = app.listen(8081, function () {
var host = server.address().address
var port = server.address().port
      console.log("Example app listening at http://%s:%s", host, port)
    }) 

return bitcoin.ECPair.makeRandom().toWIF()

This returning wallet address and private key.

{
    "WIFPrivateKey": "5XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXa",
    "BitcoinAddress": "12zyHTgqfm3XT5KXXXXXHk"
}

To get those BTC deposited by user, we have to login to the wallet.

The flow is very straight forward in NEO and Ethereum, user can login to the wallet using private key, but in Bitcoin I'm not able to figure out a way to login to the wallet.

Is there something that I have missed?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.