Browse Source

Merge pull request #80 from Dcousens/versionextract

Extracts version from ECKey private/public key utility class
hk-custom-address
Wei Lu 11 years ago
parent
commit
9a2953767c
  1. 2
      src/address.js
  2. 4
      src/ecdsa.js
  3. 94
      src/eckey.js
  4. 10
      src/hdwallet.js
  5. 2
      src/message.js
  6. 2
      src/transaction.js
  7. 4
      src/wallet.js
  8. 137
      test/eckey.js
  9. 48
      test/hdwallet.js
  10. 4
      test/message.js

2
src/address.js

@ -19,7 +19,7 @@ var Address = function (bytes, version) {
}
else {
this.hash = bytes;
this.version = version || bytes.version || mainnet;
this.version = version || mainnet;
}
};

4
src/ecdsa.js

@ -291,11 +291,11 @@ var ECDSA = {
*/
calcPubkeyRecoveryParam: function (origPubkey, r, s, hash)
{
var address = origPubkey.getBitcoinAddress().toString();
var address = origPubkey.getAddress().toString();
for (var i = 0; i < 4; i++) {
var pubkey = ECDSA.recoverPubKey(r, s, hash, i);
pubkey.compressed = origPubkey.compressed;
if (pubkey.getBitcoinAddress().toString() == address) {
if (pubkey.getAddress().toString() == address) {
return i;
}
}

94
src/eckey.js

@ -7,25 +7,22 @@ var Address = require('./address');
var ecdsa = require('./ecdsa');
var ECPointFp = require('./jsbn/ec').ECPointFp;
var Network = require('./network')
var mainnet = Network.mainnet.addressVersion
var testnet = Network.testnet.addressVersion
var ecparams = sec("secp256k1");
// input can be nothing, array of bytes, hex string, or base58 string
var ECKey = function (input,compressed,version) {
if (!(this instanceof ECKey)) { return new ECKey(input,compressed,version); }
var ECKey = function (input,compressed) {
if (!(this instanceof ECKey)) { return new ECKey(input,compressed); }
if (!input) {
// Generate new key
var n = ecparams.getN();
this.priv = ecdsa.getBigRandom(n);
this.compressed = compressed || false;
this.version = version || mainnet;
}
else this.import(input,compressed,version)
else this.import(input,compressed)
};
ECKey.prototype.import = function (input,compressed,version) {
ECKey.prototype.import = function (input,compressed) {
function has(li,v) { return li.indexOf(v) >= 0 }
function fromBin(x) { return BigInteger.fromByteArrayUnsigned(x) }
this.priv =
@ -55,26 +52,11 @@ ECKey.prototype.import = function (input,compressed,version) {
: input.length == 64 ? false
: input.length == 65 ? true
: null
this.version =
version !== undefined ? version
: input instanceof ECKey ? input.version
: input instanceof BigInteger ? mainnet
: Array.isArray(input) ? mainnet
: typeof input != "string" ? null
: input.length == 44 ? mainnet
: input.length == 51 && input[0] == '5' ? mainnet
: input.length == 51 && input[0] == '9' ? testnet
: input.length == 52 && has('LK',input[0]) ? mainnet
: input.length == 52 && input[0] == 'c' ? testnet
: input.length == 64 ? mainnet
: input.length == 65 ? mainnet
: null
};
ECKey.prototype.getPub = function(compressed) {
if (compressed === undefined) compressed = this.compressed
return ECPubKey(ecparams.getG().multiply(this.priv),compressed,this.version)
return ECPubKey(ecparams.getG().multiply(this.priv),compressed)
}
/**
@ -83,17 +65,22 @@ ECKey.prototype.getPub = function(compressed) {
ECKey.prototype['export'] = function(format) {
format || (format = 'hex')
return this['to' + format.substr(0, 1).toUpperCase() + format.substr(1)]()
};
}
ECKey.prototype.toBin = function() {
return convert.bytesToString(this.toBytes())
}
ECKey.prototype.toBase58 = function() {
return base58.checkEncode(this.toBytes(), ECKey.version_bytes[this.version])
ECKey.version_bytes = {
0: 128,
111: 239
}
ECKey.prototype.toWif = ECKey.prototype.toBase58
ECKey.prototype.toWif = function(version) {
var version = version || Network.mainnet.addressVersion;
return base58.checkEncode(this.toBytes(), ECKey.version_bytes[version])
}
ECKey.prototype.toHex = function() {
return convert.bytesToHex(this.toBytes())
@ -109,10 +96,10 @@ ECKey.prototype.toBase64 = function() {
return convert.bytesToBase64(this.toBytes())
}
ECKey.prototype.toString = ECKey.prototype.toBase58
ECKey.prototype.toString = ECKey.prototype.toWif
ECKey.prototype.getBitcoinAddress = function() {
return this.getPub().getBitcoinAddress(this.version)
ECKey.prototype.getAddress = function(version) {
return this.getPub().getAddress(version)
}
ECKey.prototype.add = function(key) {
@ -123,24 +110,18 @@ ECKey.prototype.multiply = function(key) {
return ECKey(this.priv.multiply(ECKey(key).priv),this.compressed)
}
ECKey.version_bytes = {
0: 128,
111: 239
}
var ECPubKey = function(input,compressed,version) {
if (!(this instanceof ECPubKey)) { return new ECPubKey(input,compressed,version); }
var ECPubKey = function(input,compressed) {
if (!(this instanceof ECPubKey)) { return new ECPubKey(input,compressed); }
if (!input) {
// Generate new key
var n = ecparams.getN();
this.pub = ecparams.getG().multiply(ecdsa.getBigRandom(n))
this.compressed = compressed || false;
this.version = version || mainnet;
}
else this.import(input,compressed,version)
else this.import(input,compressed)
}
ECPubKey.prototype.import = function(input,compressed,version) {
ECPubKey.prototype.import = function(input,compressed) {
var decode = function(x) { return ECPointFp.decodeFrom(ecparams.getCurve(), x) }
this.pub =
input instanceof ECPointFp ? input
@ -155,20 +136,14 @@ ECPubKey.prototype.import = function(input,compressed,version) {
: input instanceof ECPointFp ? input.compressed
: input instanceof ECPubKey ? input.compressed
: (this.pub[0] < 4)
this.version =
version ? version
: input instanceof ECPointFp ? input.version
: input instanceof ECPubKey ? input.version
: mainnet
}
ECPubKey.prototype.add = function(key) {
return ECPubKey(this.pub.add(ECPubKey(key).pub),this.compressed,this.version)
return ECPubKey(this.pub.add(ECPubKey(key).pub),this.compressed)
}
ECPubKey.prototype.multiply = function(key) {
return ECPubKey(this.pub.multiply(ECKey(key).priv),this.compressed,this.version)
return ECPubKey(this.pub.multiply(ECKey(key).priv),this.compressed)
}
ECPubKey.prototype['export'] = function(format) {
@ -189,18 +164,18 @@ ECPubKey.prototype.toBin = function() {
return convert.bytesToString(this.toBytes())
}
ECPubKey.prototype.toBase58 = function() {
return base58.checkEncode(this.toBytes(), this.version)
ECPubKey.prototype.toWif = function(version) {
var version = version || Network.mainnet.addressVersion;
return base58.checkEncode(this.toBytes(), version)
}
ECPubKey.prototype.toWif = ECPubKey.prototype.toBase58
ECPubKey.prototype.toString = ECPubKey.prototype.toWif
ECPubKey.prototype.toString = function() {
return this.getBitcoinAddress().toString()
}
ECPubKey.prototype.getAddress = function(version) {
var version = version || Network.mainnet.addressVersion;
ECPubKey.prototype.getBitcoinAddress = function() {
return new Address(util.sha256ripe160(this.toBytes()), this.version);
return new Address(util.sha256ripe160(this.toBytes()), version);
}
ECKey.prototype.sign = function (hash) {
@ -214,6 +189,7 @@ ECKey.prototype.verify = function (hash, sig) {
/**
* Parse an exported private key contained in a string.
*/
module.exports = { ECKey: ECKey, ECPubKey: ECPubKey };
module.exports = {
ECKey: ECKey,
ECPubKey: ECPubKey
};

10
src/hdwallet.js

@ -21,7 +21,7 @@ var HDWallet = module.exports = function(seed, network) {
throw new Error("Unknown network: " + this.network)
}
this.priv = new ECKey(I.slice(0, 32).concat([1]), true, this.getKeyVersion())
this.priv = new ECKey(I.slice(0, 32).concat([1]), true)
this.pub = this.priv.getPub()
this.index = 0
this.depth = 0
@ -109,10 +109,10 @@ HDWallet.fromBytes = function(input) {
// 33 bytes: the public key or private key data (0x02 + X or 0x03 + X for
// public keys, 0x00 + k for private keys)
if (type == 'priv') {
hd.priv = new ECKey(input.slice(46, 78).concat([1]), true, hd.getKeyVersion())
hd.priv = new ECKey(input.slice(46, 78).concat([1]), true)
hd.pub = hd.priv.getPub()
} else {
hd.pub = new ECPubKey(input.slice(45, 78), true, hd.getKeyVersion())
hd.pub = new ECPubKey(input.slice(45, 78), true)
}
return hd
@ -126,7 +126,7 @@ HDWallet.prototype.getFingerprint = function() {
return this.getIdentifier().slice(0, 4)
}
HDWallet.prototype.getBitcoinAddress = function() {
HDWallet.prototype.getAddress = function() {
return new Address(util.sha256ripe160(this.pub.toBytes()), this.getKeyVersion())
}
@ -221,7 +221,7 @@ HDWallet.prototype.derive = function(i) {
hd.pub = hd.priv.getPub()
} else {
// Ki = (IL + kpar)*G = IL*G + Kpar
hd.pub = this.pub.add(new ECKey(IL.concat([1]), true, this.getKeyVersion()).getPub())
hd.pub = this.pub.add(new ECKey(IL.concat([1]), true).getPub())
}
// ci = IR.

2
src/message.js

@ -60,7 +60,7 @@ Message.verifyMessage = function (address, sig, message) {
var pubKey = ecdsa.recoverPubKey(sig.r, sig.s, hash, sig.i);
pubKey.compressed = isCompressed;
var expectedAddress = pubKey.getBitcoinAddress().toString();
var expectedAddress = pubKey.getAddress().toString();
return (address === expectedAddress);
};

2
src/transaction.js

@ -310,7 +310,7 @@ Transaction.prototype.signWithKeys = function(keys, outputs, type) {
key = new ECKey(key);
return {
key: key,
address: key.getBitcoinAddress().toString()
address: key.getAddress().toString()
}
});
var hmap = {};

4
src/wallet.js

@ -50,13 +50,13 @@ var Wallet = function (seed, options) {
this.generateAddress = function() {
var key = externalAccount.derive(this.addresses.length)
this.addresses.push(key.getBitcoinAddress().toString())
this.addresses.push(key.getAddress().toString())
return this.addresses[this.addresses.length - 1]
}
this.generateChangeAddress = function() {
var key = internalAccount.derive(this.changeAddresses.length)
this.changeAddresses.push(key.getBitcoinAddress().toString())
this.changeAddresses.push(key.getAddress().toString())
return this.changeAddresses[this.changeAddresses.length - 1]
}

137
test/eckey.js

@ -1,16 +1,18 @@
/* global describe, it */
var assert = require('assert');
var ECKey = require('../src/eckey.js').ECKey;
var ECPubKey = require('../src/eckey.js').ECPubKey;
var convert = require('../src/convert.js');
var bytesToHex = convert.bytesToHex;
var hexToBytes = convert.hexToBytes;
var Address = require('../src/address');
var Network = require('../src/network')
var mainnet = Network.mainnet.addressVersion
var testnet = Network.testnet.addressVersion
describe('ECKey', function() {
describe('constructor (base58 private) on mainnet', function() {
describe('constructor', function() {
it('parses hex', function() {
var priv = '18e14a7b6a307f426a94f8114701e7c8e774e7f9a47e2c2035db29a206321725';
var pub = '0450863ad64a87ae8a2fe83c1af1a8403cb53f53e486d8511dad8a04887e5b235' +
@ -19,7 +21,6 @@ describe('ECKey', function() {
assert.equal(key.getPub().toHex(), pub);
assert.equal(key.compressed, false);
assert.equal(key.version, mainnet);
})
it('parses base64', function() {
@ -30,7 +31,6 @@ describe('ECKey', function() {
assert.equal(key.getPub().toHex(), pub);
assert.equal(key.compressed, false);
assert.equal(key.version, mainnet);
})
it('parses WIF', function() {
@ -42,8 +42,7 @@ describe('ECKey', function() {
assert.equal(key.compressed, false);
assert.equal(key.getPub().toHex(), pub);
assert.equal(key.getBitcoinAddress().toString(), addr);
assert.equal(key.version, mainnet);
assert.equal(key.getAddress().toString(), addr);
})
it('parses compressed WIF', function() {
@ -54,73 +53,89 @@ describe('ECKey', function() {
assert.equal(key.compressed, true);
assert.equal(key.getPub().toHex(), pub);
assert.equal(key.getBitcoinAddress().toString(), addr);
assert.equal(key.version, mainnet);
assert.equal(key.getAddress().toString(), addr);
})
})
describe('constructor (base58 private) on testnet', function() {
it('parses hex', function() {
it('alternative constructor syntax', function() {
var priv = 'ca48ec9783cf3ad0dfeff1fc254395a2e403cbbc666477b61b45e31d3b8ab458';
var pub = '044b12d9d7c77db68388b6ff7c89046174c871546436806bcd80d07c28ea81199' +
'283fbec990dad6fb98f93f712d50cb874dd717de6a184158d63886dda3090f566';
var key = new ECKey(priv, false, testnet);
var key = ECKey(priv, false);
assert.equal(key.getPub().toHex(), pub);
assert.equal(key.compressed, false);
assert.equal(key.version, testnet);
assert.equal(key.toHex(), priv);
})
})
it('parses base64', function() {
var priv = 'VYdB+iv47y5FaUVIPdQInkgATrABeuD1lACUoM4x7tU=';
var pub = '042f43c16c08849fed20a35bb7b1947bbf0923c52d613ee13b5c665a1e10d24b2' +
'8be909a70f5f87c1adb79fbcd1b3f17d20aa91c04fc355112dba2ce9b1cbf013b';
var key = new ECKey(priv, false, testnet);
assert.equal(key.getPub().toHex(), pub);
assert.equal(key.compressed, false);
assert.equal(key.version, testnet);
assert.equal(key.toBase64(), priv);
})
it('parses WIF', function() {
var priv = '92tb9mjz6q9eKZjYvLsgk87kPrMoh7BGRumSzPeUGhmigtsfrbP';
var pub = '04b70d0bd71417d7903b6c4b451d8cfbd6514cb3557d9363319e5d87377905c4f' +
'3bb6c6e8c1819b6dd95d60aa8da73f0e726a2311545842bf07e78487e8ea2801f';
var addr = 'mmktwdfQ3oaEvyjS5p1TgovfJmgBQLjGa4';
var key = new ECKey(priv);
assert.equal(key.compressed, false);
assert.equal(key.getPub().toHex(), pub);
assert.equal(key.getBitcoinAddress().toString(), addr);
assert.equal(key.version, testnet);
assert.equal(key.toBase58(), priv);
})
it('parses compressed WIF', function() {
var priv = 'cTLhjiUj2jX9iwUotSiiRLQnXrrq62GK6wuBiJxPNJxP15ERrX8o';
var pub = '03b70d0bd71417d7903b6c4b451d8cfbd6514cb3557d9363319e5d87377905c4f3'
var addr = 'n3nBy5dyx24CLT3kg3ZJwfTeTX5oxxtxNE';
var key = new ECKey(priv);
assert.equal(key.compressed, true);
assert.equal(key.getPub().toHex(), pub);
assert.equal(key.getBitcoinAddress().toString(), addr);
assert.equal(key.version, testnet);
assert.equal(key.toBase58(), priv);
describe('toAddress', function() {
var privkeys = [
'CA48EC9783CF3AD0DFEFF1FC254395A2E403CBBC666477B61B45E31D3B8AB458',
'1111111111111111111111111111111111111111111111111111111111111111',
'18E14A7B6A307F426A94F8114701E7C8E774E7F9A47E2C2035DB29A206321725'
];
// compressed pubkeys
var cpubkeys = [
'024B12D9D7C77DB68388B6FF7C89046174C871546436806BCD80D07C28EA811992',
'034F355BDCB7CC0AF728EF3CCEB9615D90684BB5B2CA5F859AB0F0B704075871AA',
'0250863AD64A87AE8A2FE83C1AF1A8403CB53F53E486D8511DAD8A04887E5B2352'
];
var pubkeys = cpubkeys.map(function(x) {
return new ECPubKey(x).pub.getEncoded(false);
});
it('mainnet', function() {
var addresses = [
'19SgmoUj4xowEjwtXvNAtYTAgbvR9iBCui',
'1MsHWS1BnwMc3tLE8G35UXsS58fKipzB7a',
'16UwLL9Risc3QfPqBUvKofHmBQ7wMtjvM'
];
var compressedAddresses = [
'1AA4sjKW2aUmbtN3MtegdvhYtDBbDEke1q',
'1Q1pE5vPGEEMqRcVRMbtBK842Y6Pzo6nK9',
'1PMycacnJaSqwwJqjawXBErnLsZ7RkXUAs',
];
for (var i = 0; i < addresses.length; ++i) {
var priv = new ECKey(privkeys[i], false);
var pubcomp = new ECPubKey(cpubkeys[i], true);
var pub = new ECPubKey(pubkeys[i], false);
var addr = addresses[i];
var caddr = compressedAddresses[i];
assert.equal(priv.getAddress().toString(), addr);
assert.equal(pub.getAddress().toString(), addr);
assert.equal(pubcomp.getAddress().toString(), caddr);
}
})
it('initiation via alternative constructor syntax', function() {
var priv = 'ca48ec9783cf3ad0dfeff1fc254395a2e403cbbc666477b61b45e31d3b8ab458';
var pub = '044b12d9d7c77db68388b6ff7c89046174c871546436806bcd80d07c28ea81199' +
'283fbec990dad6fb98f93f712d50cb874dd717de6a184158d63886dda3090f566';
var key = ECKey(priv, false, testnet);
assert.equal(key.getPub().toHex(), pub);
assert.equal(key.compressed, false);
assert.equal(key.version, testnet);
assert.equal(key.toHex(), priv);
it('testnet', function() {
var addresses = [
'19SgmoUj4xowEjwtXvNAtYTAgbvR9iBCui',
'1MsHWS1BnwMc3tLE8G35UXsS58fKipzB7a',
'16UwLL9Risc3QfPqBUvKofHmBQ7wMtjvM'
];
var compressedAddresses = [
'1AA4sjKW2aUmbtN3MtegdvhYtDBbDEke1q',
'1Q1pE5vPGEEMqRcVRMbtBK842Y6Pzo6nK9',
'1PMycacnJaSqwwJqjawXBErnLsZ7RkXUAs',
];
for (var i = 0; i < addresses.length; ++i) {
var priv = new ECKey(privkeys[i], false);
var pubcomp = new ECPubKey(cpubkeys[i], true);
var pub = new ECPubKey(pubkeys[i], false);
var addr = addresses[i];
var caddr = compressedAddresses[i];
assert.equal(priv.getAddress().toString(), addr);
assert.equal(pub.getAddress().toString(), addr);
assert.equal(pubcomp.getAddress().toString(), caddr);
}
})
})
});
})

48
test/hdwallet.js

@ -76,7 +76,7 @@ describe('HDWallet', function() {
// m
assert.equal(b2h(hd.getIdentifier()), '3442193e1bb70916e914552172cd4e2dbc9df811')
assert.equal(b2h(hd.getFingerprint()), '3442193e')
assert.equal(hd.getBitcoinAddress().toString(), '15mKKb2eos1hWa6tisdPwwDC1a5J1y9nma')
assert.equal(hd.getAddress().toString(), '15mKKb2eos1hWa6tisdPwwDC1a5J1y9nma')
assert.equal(hd.priv.toHex(), 'e8f32e723decf4051aefac8e2c93c9c5b214313817cdb01a1494b917c8436b3501')
assert.equal(hd.priv.toWif(), 'L52XzL2cMkHxqxBXRyEpnPQZGUs3uKiL3R11XbAdHigRzDozKZeW')
assert.equal(hd.pub.toHex(), '0339a36013301597daef41fbe593a02cc513d0b55527ec2df1050e2e8ff49c85c2')
@ -90,7 +90,7 @@ describe('HDWallet', function() {
hd = hd.derivePrivate(0)
assert.equal(b2h(hd.getIdentifier()), '5c1bd648ed23aa5fd50ba52b2457c11e9e80a6a7')
assert.equal(b2h(hd.getFingerprint()), '5c1bd648')
assert.equal(hd.getBitcoinAddress().toString(), '19Q2WoS5hSS6T8GjhK8KZLMgmWaq4neXrh')
assert.equal(hd.getAddress().toString(), '19Q2WoS5hSS6T8GjhK8KZLMgmWaq4neXrh')
assert.equal(hd.priv.toHex().slice(0, 64), 'edb2e14f9ee77d26dd93b4ecede8d16ed408ce149b6cd80b0715a2d911a0afea')
assert.equal(hd.priv.toWif(), 'L5BmPijJjrKbiUfG4zbiFKNqkvuJ8usooJmzuD7Z8dkRoTThYnAT')
assert.equal(hd.pub.toHex(), '035a784662a4a20a65bf6aab9ae98a6c068a81c52e4b032c0fb5400c706cfccc56')
@ -104,7 +104,7 @@ describe('HDWallet', function() {
hd = hd.derive(1)
assert.equal(b2h(hd.getIdentifier()), 'bef5a2f9a56a94aab12459f72ad9cf8cf19c7bbe')
assert.equal(b2h(hd.getFingerprint()), 'bef5a2f9')
assert.equal(hd.getBitcoinAddress().toString(), '1JQheacLPdM5ySCkrZkV66G2ApAXe1mqLj')
assert.equal(hd.getAddress().toString(), '1JQheacLPdM5ySCkrZkV66G2ApAXe1mqLj')
assert.equal(hd.priv.toHex().slice(0, 64), '3c6cb8d0f6a264c91ea8b5030fadaa8e538b020f0a387421a12de9319dc93368')
assert.equal(hd.priv.toWif(), 'KyFAjQ5rgrKvhXvNMtFB5PCSKUYD1yyPEe3xr3T34TZSUHycXtMM')
assert.equal(hd.pub.toHex(), '03501e454bf00751f24b1b489aa925215d66af2234e3891c3b21a52bedb3cd711c')
@ -118,7 +118,7 @@ describe('HDWallet', function() {
hd = hd.derivePrivate(2)
assert.equal(b2h(hd.getIdentifier()), 'ee7ab90cde56a8c0e2bb086ac49748b8db9dce72')
assert.equal(b2h(hd.getFingerprint()), 'ee7ab90c')
assert.equal(hd.getBitcoinAddress().toString(), '1NjxqbA9aZWnh17q1UW3rB4EPu79wDXj7x')
assert.equal(hd.getAddress().toString(), '1NjxqbA9aZWnh17q1UW3rB4EPu79wDXj7x')
assert.equal(hd.priv.toHex().slice(0, 64), 'cbce0d719ecf7431d88e6a89fa1483e02e35092af60c042b1df2ff59fa424dca')
assert.equal(hd.priv.toWif(), 'L43t3od1Gh7Lj55Bzjj1xDAgJDcL7YFo2nEcNaMGiyRZS1CidBVU')
assert.equal(hd.pub.toHex(), '0357bfe1e341d01c69fe5654309956cbea516822fba8a601743a012a7896ee8dc2')
@ -132,7 +132,7 @@ describe('HDWallet', function() {
hd = hd.derive(2)
assert.equal(b2h(hd.getIdentifier()), 'd880d7d893848509a62d8fb74e32148dac68412f')
assert.equal(b2h(hd.getFingerprint()), 'd880d7d8')
assert.equal(hd.getBitcoinAddress().toString(), '1LjmJcdPnDHhNTUgrWyhLGnRDKxQjoxAgt')
assert.equal(hd.getAddress().toString(), '1LjmJcdPnDHhNTUgrWyhLGnRDKxQjoxAgt')
assert.equal(hd.priv.toHex().slice(0, 64), '0f479245fb19a38a1954c5c7c0ebab2f9bdfd96a17563ef28a6a4b1a2a764ef4')
assert.equal(hd.priv.toWif(), 'KwjQsVuMjbCP2Zmr3VaFaStav7NvevwjvvkqrWd5Qmh1XVnCteBR')
assert.equal(hd.pub.toHex(), '02e8445082a72f29b75ca48748a914df60622a609cacfce8ed0e35804560741d29')
@ -146,7 +146,7 @@ describe('HDWallet', function() {
hd = hd.derive(1000000000)
assert.equal(b2h(hd.getIdentifier()), 'd69aa102255fed74378278c7812701ea641fdf32')
assert.equal(b2h(hd.getFingerprint()), 'd69aa102')
assert.equal(hd.getBitcoinAddress().toString(), '1LZiqrop2HGR4qrH1ULZPyBpU6AUP49Uam')
assert.equal(hd.getAddress().toString(), '1LZiqrop2HGR4qrH1ULZPyBpU6AUP49Uam')
assert.equal(hd.priv.toHex().slice(0, 64), '471b76e389e528d6de6d816857e012c5455051cad6660850e58372a6c3e6e7c8')
assert.equal(hd.priv.toWif(), 'Kybw8izYevo5xMh1TK7aUr7jHFCxXS1zv8p3oqFz3o2zFbhRXHYs')
assert.equal(hd.pub.toHex(), '022a471424da5e657499d1ff51cb43c47481a03b1e77f951fe64cec9f5a48f7011')
@ -163,7 +163,7 @@ describe('HDWallet', function() {
// m
assert.equal(b2h(hd.getIdentifier()), 'bd16bee53961a47d6ad888e29545434a89bdfe95')
assert.equal(b2h(hd.getFingerprint()), 'bd16bee5')
assert.equal(hd.getBitcoinAddress().toString(), '1JEoxevbLLG8cVqeoGKQiAwoWbNYSUyYjg')
assert.equal(hd.getAddress().toString(), '1JEoxevbLLG8cVqeoGKQiAwoWbNYSUyYjg')
assert.equal(hd.priv.toHex().slice(0, 64), '4b03d6fc340455b363f51020ad3ecca4f0850280cf436c70c727923f6db46c3e')
assert.equal(hd.priv.toWif(), 'KyjXhyHF9wTphBkfpxjL8hkDXDUSbE3tKANT94kXSyh6vn6nKaoy')
assert.equal(hd.pub.toHex(), '03cbcaa9c98c877a26977d00825c956a238e8dddfbd322cce4f74b0b5bd6ace4a7')
@ -177,7 +177,7 @@ describe('HDWallet', function() {
hd = hd.derive(0)
assert.equal(b2h(hd.getIdentifier()), '5a61ff8eb7aaca3010db97ebda76121610b78096')
assert.equal(b2h(hd.getFingerprint()), '5a61ff8e')
assert.equal(hd.getBitcoinAddress().toString(), '19EuDJdgfRkwCmRzbzVBHZWQG9QNWhftbZ')
assert.equal(hd.getAddress().toString(), '19EuDJdgfRkwCmRzbzVBHZWQG9QNWhftbZ')
assert.equal(hd.priv.toHex().slice(0, 64), 'abe74a98f6c7eabee0428f53798f0ab8aa1bd37873999041703c742f15ac7e1e')
assert.equal(hd.priv.toWif(), 'L2ysLrR6KMSAtx7uPqmYpoTeiRzydXBattRXjXz5GDFPrdfPzKbj')
assert.equal(hd.pub.toHex(), '02fc9e5af0ac8d9b3cecfe2a888e2117ba3d089d8585886c9c826b6b22a98d12ea')
@ -191,7 +191,7 @@ describe('HDWallet', function() {
hd = hd.derivePrivate(2147483647)
assert.equal(b2h(hd.getIdentifier()), 'd8ab493736da02f11ed682f88339e720fb0379d1')
assert.equal(b2h(hd.getFingerprint()), 'd8ab4937')
assert.equal(hd.getBitcoinAddress().toString(), '1Lke9bXGhn5VPrBuXgN12uGUphrttUErmk')
assert.equal(hd.getAddress().toString(), '1Lke9bXGhn5VPrBuXgN12uGUphrttUErmk')
assert.equal(hd.priv.toHex().slice(0, 64), '877c779ad9687164e9c2f4f0f4ff0340814392330693ce95a58fe18fd52e6e93')
assert.equal(hd.priv.toWif(), 'L1m5VpbXmMp57P3knskwhoMTLdhAAaXiHvnGLMribbfwzVRpz2Sr')
assert.equal(hd.pub.toHex(), '03c01e7425647bdefa82b12d9bad5e3e6865bee0502694b94ca58b666abc0a5c3b')
@ -205,7 +205,7 @@ describe('HDWallet', function() {
hd = hd.derive(1)
assert.equal(b2h(hd.getIdentifier()), '78412e3a2296a40de124307b6485bd19833e2e34')
assert.equal(b2h(hd.getFingerprint()), '78412e3a')
assert.equal(hd.getBitcoinAddress().toString(), '1BxrAr2pHpeBheusmd6fHDP2tSLAUa3qsW')
assert.equal(hd.getAddress().toString(), '1BxrAr2pHpeBheusmd6fHDP2tSLAUa3qsW')
assert.equal(hd.priv.toHex().slice(0, 64), '704addf544a06e5ee4bea37098463c23613da32020d604506da8c0518e1da4b7')
assert.equal(hd.priv.toWif(), 'KzyzXnznxSv249b4KuNkBwowaN3akiNeEHy5FWoPCJpStZbEKXN2')
assert.equal(hd.pub.toHex(), '03a7d1d856deb74c508e05031f9895dab54626251b3806e16b4bd12e781a7df5b9')
@ -219,7 +219,7 @@ describe('HDWallet', function() {
hd = hd.derivePrivate(2147483646)
assert.equal(b2h(hd.getIdentifier()), '31a507b815593dfc51ffc7245ae7e5aee304246e')
assert.equal(b2h(hd.getFingerprint()), '31a507b8')
assert.equal(hd.getBitcoinAddress().toString(), '15XVotxCAV7sRx1PSCkQNsGw3W9jT9A94R')
assert.equal(hd.getAddress().toString(), '15XVotxCAV7sRx1PSCkQNsGw3W9jT9A94R')
assert.equal(hd.priv.toHex().slice(0, 64), 'f1c7c871a54a804afe328b4c83a1c33b8e5ff48f5087273f04efa83b247d6a2d')
assert.equal(hd.priv.toWif(), 'L5KhaMvPYRW1ZoFmRjUtxxPypQ94m6BcDrPhqArhggdaTbbAFJEF')
assert.equal(hd.pub.toHex(), '02d2b36900396c9282fa14628566582f206a5dd0bcc8d5e892611806cafb0301f0')
@ -233,7 +233,7 @@ describe('HDWallet', function() {
hd = hd.derive(2)
assert.equal(b2h(hd.getIdentifier()), '26132fdbe7bf89cbc64cf8dafa3f9f88b8666220')
assert.equal(b2h(hd.getFingerprint()), '26132fdb')
assert.equal(hd.getBitcoinAddress().toString(), '14UKfRV9ZPUp6ZC9PLhqbRtxdihW9em3xt')
assert.equal(hd.getAddress().toString(), '14UKfRV9ZPUp6ZC9PLhqbRtxdihW9em3xt')
assert.equal(hd.priv.toHex().slice(0, 64), 'bb7d39bdb83ecf58f2fd82b6d918341cbef428661ef01ab97c28a4842125ac23')
assert.equal(hd.priv.toWif(), 'L3WAYNAZPxx1fr7KCz7GN9nD5qMBnNiqEJNJMU1z9MMaannAt4aK')
assert.equal(hd.pub.toHex(), '024d902e1a2fc7a8755ab5b694c575fce742c48d9ff192e63df5193e4c7afe1f9c')
@ -246,26 +246,14 @@ describe('HDWallet', function() {
})
describe('network types', function() {
it('ensures that a mainnet Wallet has mainnet child keys (pub and priv)', function() {
var wallet = new HDWallet("foobar", "mainnet")
assert.equal(wallet.priv.version, mainnet)
var privChild = wallet.derivePrivate(0)
assert.equal(privChild.priv.version, mainnet)
var pubChild = wallet.derive(0)
assert.equal(pubChild.priv.version, mainnet)
it('ensures that a mainnet Wallet generates mainnet addresses', function() {
var wallet = new HDWallet('foobar', 'mainnet')
assert.equal(wallet.getAddress().toString(), '1JNymexJHEr5u1BndiChMStFkCgPm4EQ6o');
})
it('ensures that a testnet Wallet has testnet child keys (pub and priv)', function() {
var wallet = new HDWallet("foobar", "testnet")
assert.equal(wallet.priv.version, testnet)
var privChild = wallet.derivePrivate(0)
assert.equal(privChild.priv.version, testnet)
var pubChild = wallet.derive(0)
assert.equal(pubChild.priv.version, testnet)
it('ensures that a testnet Wallet generates testnet addresses', function() {
var wallet = new HDWallet('foobar', 'testnet')
assert.equal(wallet.getAddress().toString(), 'mxtw4i3H6GHLg7fQMHB5BN6acCH6kQ7aoY');
})
it('throws an excption when unknown network type is passed in', function() {

4
test/message.js

@ -12,7 +12,7 @@ describe('Message', function() {
describe('verify', function() {
it('passes case 1', function() {
var key = new ECKey(hexToBytes(priv));
assert.equal(key.getBitcoinAddress().toString(), addr);
assert.equal(key.getAddress().toString(), addr);
var sig = Message.signMessage(key, msg);
assert.ok(Message.verifyMessage(addr, sig, msg));
@ -35,7 +35,7 @@ describe('Message', function() {
var key = new ECKey(hexToBytes(priv));
key.compressed = true
var addr = key.getBitcoinAddress().toString()
var addr = key.getAddress().toString()
var sig = Message.signMessage(key, msg);
assert.ok(Message.verifyMessage(addr, sig, msg));

Loading…
Cancel
Save