bitcoinjs / bitcoinjs-lib

A javascript Bitcoin library for node.js and browsers.
MIT License
5.67k stars 2.1k forks source link

There is a problem in crypto.js #1092

Closed jiangbo0216 closed 6 years ago

jiangbo0216 commented 6 years ago
let assert = require('assert')
let bitcoin = require('bitcoinjs-lib')
let dhttp = require('dhttp/200')

// deterministic RNG for testing only
// function rng () { return Buffer.from('zzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzz') }

function rng () {
    return Buffer.from('YT8dAtK4d16A3P1z+TpwB2jJ4aFH3g9M1EioIBkLEV4=', 'base64')
  }

// TODO: remove
let baddress = bitcoin.address
let bcrypto = bitcoin.crypto
function getAddress (node, network) {
  network = network || bitcoin.networks.bitcoin
  return baddress.toBase58Check(bcrypto.hash160(node.publicKey), network.pubKeyHash)
}

    var keyPair = bitcoin.ECPair.makeRandom({ rng: rng })
    console.log(keyPair)
    var address = getAddress(keyPair)

TypeError: Data must be a string or a buffer
    at Hash.update (crypto.js:99:16)
    at sha256 (\node_modules\bitcoinjs-lib\src\crypto.js:12:31)
    at Object.hash160 (\node_modules\bitcoinjs-lib\src\crypto.js:16:20)
    at getAddress (I\src\bitcoinjs\addresses.js:19:41)
    at Object.<anonymous> \src\bitcoinjs\addresses.js:25:19)
    at Module._compile (module.js:652:30)
    at Object.Module._extensions..js (module.js:663:10)
    at Module.load (module.js:565:32)
    at tryModuleLoad (module.js:505:12)
    at Function.Module._load (module.js:497:3)

keyPair don't have publicKey

dcousens commented 6 years ago

@JQiMeng this is using master branch, yes?

What does

console.log(keyPair)

Print?

dabura667 commented 6 years ago

use node.getPubkeyBuffer() instead of node.publicKey

dcousens commented 6 years ago

@dabura667 our example links should ref the latest release, probably

jiangbo0216 commented 6 years ago

thank your help, I use the command ( npm install bitcoinjs-lib) to install ,is it stable?

and i see code as follows:

ECPair.prototype.getAddress = function () { return baddress.toBase58Check(bcrypto.hash160(this.getPublicKeyBuffer()), this.getNetwork().pubKeyHash) } so we can Modify the code as follows:

    var keyPair = bitcoin.ECPair.makeRandom({ rng: rng })
    keyPair.getAddress()
    rather then   var address = getAddress(keyPair)

is this right?

dcousens commented 6 years ago

@JQiMeng that is stable. And that is right.

The master branch on this repository is about to release to 4.0.0.

Please see https://github.com/bitcoinjs/bitcoinjs-lib/blob/v3.3.2/test/integration/addresses.js#L13-L16 for the examples for 3.3.2 (latest release).

https://github.com/bitcoinjs/bitcoinjs-lib/blob/f4caaf42e7b58332d74c1540f88bcda7e55b82e6/test/integration/addresses.js#L13-L16

zhaozhiming commented 6 years ago

@dcousens So when the 4.0.0 version to release? I found the latest version is 3.3.2

dcousens commented 6 years ago

@zhaozhiming hopefully soon :)