1. 程式人生 > >比特幣主網地址生成和簽名

比特幣主網地址生成和簽名

一.地址生成

var bip39 = require('bip39')
const bip32 = require('bip32')
const bitcoin = require('bitcoinjs-lib')
const baddress = require('bitcoinjs-lib/src/address')
const bcrypto = require('bitcoinjs-lib/src/crypto')
const NETWORKS = require('bitcoinjs-lib/src/networks')

function getAddress (node) {
    console.log("PrivateKey = " + node.toWIF().toString('hex'));
    console.log("PublicKey =" + node.publicKey.toString('hex'))
    return baddress.toBase58Check(bcrypto.hash160(node.publicKey), NETWORKS.bitcoin.pubKeyHash)
}
var mnemonic = bip39.generateMnemonic()
console.log("-----------助記詞--------------")
console.log(mnemonic)
console.log("-----------助記詞--------------")
var seed = bip39.mnemonicToSeed(mnemonic)
console.log("---------seed------------")
console.log(seed)
console.log("---------seed------------")
const rootMasterKey = bip32.fromSeed(seed)

var key1 = rootMasterKey.derivePath("m/44'/0'/0'/0/0")
var key2 = rootMasterKey.derivePath("m/44'/0'/0'/0/1")
var key3 = rootMasterKey.derivePath("m/44'/0'/0'/1/0")
var key4 =rootMasterKey.derivePath("m/44'/0'/0'/1/1")

// receive addresses
console.log(getAddress(key1))
console.log(getAddress(key2))

// change addresses
console.log(getAddress(key3))
console.log(getAddress(key4))

二.比特幣簽名

const bitcoin = require('bitcoinjs-lib');

function bitcoinSign(privateKey, amount, utxo, sendFee, toAddress, changeAddress) {
    if(!privateKey || !amount || !utxo || !sendFee || !toAddress || !changeAddress ) {
        console.log("one of privateKey, amount, utxo, sendFee, toAddress and changeAddress is null, please give a valid param");
    } else {
        console.log("param is valid, start sign transaction");
        set = bitcoin.ECPair.fromWIF(privateKey);
        txb = new bitcoin.TransactionBuilder();
        var sendAmount = parseFloat(amount);
        var fee = parseFloat(sendFee);
        sendAmount += fee;
        console.log("Send Transaction total amount is: " + sendAmount)
        txb.setVersion(1);
        var totalMoney = 0;
        for(var i=0; i<utxo.length; i++){
            txb.addInput(utxo[i].tx_hash_big_endian, utxo[i].tx_output_n);
            totalMoney += utxo[i].value;
        }
        console.log("this address total money is: " + totalMoney)
        txb.addOutput(toAddress, sendAmount - fee);
        for(var i=0;i<utxo.length;i++){
            txb.sign(0, set);
        }
    }
    return txb.buildIncomplete().toHex();
}

var bitUtxo = {
    "unspent_outputs":[
        {
            "tx_hash":"8ee886ba0c66ba2df2c0e3da3beee526996d9a5e6bbbdfea43e1a78340cb0128",
            "tx_hash_big_endian":"2801cb4083a7e143eadfbb6b5e9a6d9926e5ee3bdae3c0f22dba660cba86e88e",
            "tx_index":382253932,
            "tx_output_n": 0,
            "script":"76a914ca45c6eceea7aed14b6aea7e0ed466c6134f14bc88ac",
            "value": 1899000,
            "value_hex": "1cf9f8",
            "confirmations":2
        }
    ]
}
var privateKey = "KwHEU8DTrY2ekGuqE6EqMMrcFj6Kdb6gWF4k8SpUeV7vDfc9c5Fn";
var amount = 1898000;
var utxo = bitUtxo.unspent_outputs;
var sendFee = 1000;
var toAddress = "12zEJohMNqSZLXH1Msxpw41ykkk3rxgx1s";
var changeAddress = "1KSX5wmrVax3LYaB4uKUxXzCRcv5SiLDq3";
var sign = bitcoinSign(privateKey, amount, bitUtxo.unspent_outputs, sendFee, toAddress, changeAddress);
console.log(sign);


// 01000000018ee886ba0c66ba2df2c0e3da3beee526996d9a5e6bbbdfea43e1a78340cb0128000000006a473044022050e857f867743a4306f9d94a40a7be1b7602351e30c4b8d65eabf48bd9d5cf4f0220789d8bcfb16996e62ebb37d18fddc29a1abb5e9cb66280b17f06655b870d4f6d0121020218415740e8ae879fc6b6837e4ab6e8c44d452c62f7e0f7bceb37ae1b13e685ffffffff0110f61c00000000001976a91415caf1976fc8334d4f2f071548d9cbace1a4517988ac00000000

想要學習更多關於區塊鏈錢包的知識請去筆者的GitHub,區塊鏈錢包技術指南:https://github.com/guoshijiang/blockchain-wallet/