bitcore-wallet-utils
Advanced tools
Comparing version 0.2.0 to 0.2.1
@@ -17,2 +17,6 @@ 'use strict'; | ||
WalletUtils.SCRIPT_TYPES = { | ||
P2SH: 'P2SH', | ||
P2PKH: 'P2PKH', | ||
}; | ||
WalletUtils.DERIVATION_STRATEGIES = { | ||
@@ -80,3 +84,5 @@ BIP44: 'BIP44', | ||
WalletUtils.deriveAddress = function(publicKeyRing, path, m, network) { | ||
WalletUtils.deriveAddress = function(scriptType, publicKeyRing, path, m, network) { | ||
$.checkArgument(_.contains(_.keys(WalletUtils.SCRIPT_TYPES), scriptType)); | ||
var publicKeys = _.map(publicKeyRing, function(item) { | ||
@@ -87,3 +93,12 @@ var xpub = new Bitcore.HDPublicKey(item.xPubKey); | ||
var bitcoreAddress = Address.createMultisig(publicKeys, m, network); | ||
var bitcoreAddress; | ||
switch (scriptType) { | ||
case WalletUtils.SCRIPT_TYPES.P2SH: | ||
bitcoreAddress = Address.createMultisig(publicKeys, m, network); | ||
break; | ||
case WalletUtils.SCRIPT_TYPES.P2PKH: | ||
$.checkState(_.isArray(publicKeys) && publicKeys.length == 1); | ||
bitcoreAddress = Address.fromPublicKey(publicKeys[0], network); | ||
break; | ||
} | ||
@@ -189,6 +204,15 @@ return { | ||
_.each(txp.inputs, function(i) { | ||
t.from(i, i.publicKeys, txp.requiredSignatures); | ||
}); | ||
$.checkState(_.contains(_.values(WalletUtils.SCRIPT_TYPES), txp.addressType)); | ||
switch (txp.addressType) { | ||
case WalletUtils.SCRIPT_TYPES.P2SH: | ||
_.each(txp.inputs, function(i) { | ||
t.from(i, i.publicKeys, txp.requiredSignatures); | ||
}); | ||
break; | ||
case WalletUtils.SCRIPT_TYPES.P2PKH: | ||
t.from(txp.inputs); | ||
break; | ||
} | ||
if (txp.toAddress && txp.amount && !txp.outputs) { | ||
@@ -195,0 +219,0 @@ t.to(txp.toAddress, txp.amount); |
@@ -5,3 +5,3 @@ { | ||
"author": "BitPay Inc", | ||
"version": "0.2.0", | ||
"version": "0.2.1", | ||
"main": "index.js", | ||
@@ -8,0 +8,0 @@ "keywords": [ |
@@ -18,2 +18,8 @@ 'use strict'; | ||
var masterPrivateKey = 'tprv8ZgxMBicQKsPdPLE72pfSo7CvzTsWddGHdwSuMNrcerr8yQZKdaPXiRtP9Ew8ueSe9M7jS6RJsp4DiAVS2xmyxcCC9kZV6X1FMsX7EQX2R5'; | ||
var derivedPrivateKey = { | ||
'BIP44': WalletUtils.deriveXPrivFromMaster(masterPrivateKey, 'BIP44', 'testnet'), | ||
'BIP45': WalletUtils.deriveXPrivFromMaster(masterPrivateKey, 'BIP45', 'testnet'), | ||
}; | ||
var helpers = {}; | ||
@@ -34,8 +40,19 @@ | ||
// Amounts in satoshis | ||
helpers.generateUtxos = function(publicKeyRing, path, requiredSignatures, amounts) { | ||
helpers.generateUtxos = function(scriptType, publicKeyRing, path, requiredSignatures, amounts) { | ||
var amounts = [].concat(amounts); | ||
var utxos = _.map(amounts, function(amount, i) { | ||
var address = WalletUtils.deriveAddress(publicKeyRing, path, requiredSignatures, 'testnet'); | ||
var address = WalletUtils.deriveAddress(scriptType, publicKeyRing, path, requiredSignatures, 'testnet'); | ||
var scriptPubKey; | ||
switch (scriptType) { | ||
case WalletUtils.SCRIPT_TYPES.P2SH: | ||
scriptPubKey = Bitcore.Script.buildMultisigOut(address.publicKeys, requiredSignatures).toScriptHashOut(); | ||
break; | ||
case WalletUtils.SCRIPT_TYPES.P2PKH: | ||
scriptPubKey = Bitcore.Script.buildPublicKeyHashOut(address.address); | ||
break; | ||
} | ||
should.exist(scriptPubKey); | ||
var obj = { | ||
@@ -45,6 +62,6 @@ txid: Bitcore.crypto.Hash.sha256(new Buffer(i)).toString('hex'), | ||
satoshis: helpers.toSatoshi(amount), | ||
scriptPubKey: Bitcore.Script.buildMultisigOut(address.publicKeys, requiredSignatures).toScriptHashOut().toBuffer().toString('hex'), | ||
scriptPubKey: scriptPubKey.toBuffer().toString('hex'), | ||
address: address.address, | ||
path: path, | ||
publicKeys: address.publicKeys | ||
publicKeys: address.publicKeys, | ||
}; | ||
@@ -261,7 +278,2 @@ return obj; | ||
describe('#buildTx', function() { | ||
var masterPrivateKey, derivedPrivateKey; | ||
beforeEach(function() { | ||
masterPrivateKey = 'tprv8ZgxMBicQKsPdPLE72pfSo7CvzTsWddGHdwSuMNrcerr8yQZKdaPXiRtP9Ew8ueSe9M7jS6RJsp4DiAVS2xmyxcCC9kZV6X1FMsX7EQX2R5'; | ||
derivedPrivateKey = WalletUtils.deriveXPrivFromMaster(masterPrivateKey, 'BIP45', 'testnet'); | ||
}); | ||
it('should build a tx correctly', function() { | ||
@@ -272,6 +284,6 @@ var toAddress = 'msj42CCGruhRsFrGATiUuh25dtxYtnpbTx'; | ||
var publicKeyRing = [{ | ||
xPubKey: new Bitcore.HDPublicKey(derivedPrivateKey) | ||
xPubKey: new Bitcore.HDPublicKey(derivedPrivateKey['BIP44']), | ||
}]; | ||
var utxos = helpers.generateUtxos(publicKeyRing, 'm/1/0', 1, [1000, 2000]); | ||
var utxos = helpers.generateUtxos('P2PKH', publicKeyRing, 'm/1/0', 1, [1000, 2000]); | ||
var txp = { | ||
@@ -288,3 +300,4 @@ version: '2.0.0', | ||
fee: 10050, | ||
derivationStrategy: 'BIP45', | ||
derivationStrategy: 'BIP44', | ||
addressType: 'P2PKH', | ||
}; | ||
@@ -306,6 +319,6 @@ var t = WalletUtils.buildTx(txp); | ||
var publicKeyRing = [{ | ||
xPubKey: new Bitcore.HDPublicKey(derivedPrivateKey) | ||
xPubKey: new Bitcore.HDPublicKey(derivedPrivateKey['BIP45']), | ||
}]; | ||
var utxos = helpers.generateUtxos(publicKeyRing, 'm/1/0', 1, [1000, 2000]); | ||
var utxos = helpers.generateUtxos('P2SH', publicKeyRing, 'm/2147483647/0/0', 1, [1000, 2000]); | ||
var txp = { | ||
@@ -324,2 +337,3 @@ version: '1.0.1', | ||
derivationStrategy: 'BIP45', | ||
addressType: 'P2SH', | ||
}; | ||
@@ -341,6 +355,6 @@ var t = WalletUtils.buildTx(txp); | ||
var publicKeyRing = [{ | ||
xPubKey: new Bitcore.HDPublicKey(derivedPrivateKey) | ||
xPubKey: new Bitcore.HDPublicKey(derivedPrivateKey['BIP44']), | ||
}]; | ||
var utxos = helpers.generateUtxos(publicKeyRing, 'm/1/0', 1, [1, 2]); | ||
var utxos = helpers.generateUtxos('P2PKH', publicKeyRing, 'm/1/0', 1, [1, 2]); | ||
var txp = { | ||
@@ -356,3 +370,4 @@ inputs: utxos, | ||
fee: 1.5e8, | ||
derivationStrategy: 'BIP45', | ||
derivationStrategy: 'BIP44', | ||
addressType: 'P2PKH', | ||
}; | ||
@@ -385,6 +400,6 @@ | ||
var publicKeyRing = [{ | ||
xPubKey: new Bitcore.HDPublicKey(derivedPrivateKey) | ||
xPubKey: new Bitcore.HDPublicKey(derivedPrivateKey['BIP44']), | ||
}]; | ||
var utxos = helpers.generateUtxos(publicKeyRing, 'm/1/0', 1, [1000, 2000]); | ||
var utxos = helpers.generateUtxos('P2PKH', publicKeyRing, 'm/1/0', 1, [1000, 2000]); | ||
var txp = { | ||
@@ -408,3 +423,4 @@ inputs: utxos, | ||
fee: 10000, | ||
derivationStrategy: 'BIP45', | ||
derivationStrategy: 'BIP44', | ||
addressType: 'P2PKH', | ||
}; | ||
@@ -423,6 +439,6 @@ var t = WalletUtils.buildTx(txp); | ||
var publicKeyRing = [{ | ||
xPubKey: new Bitcore.HDPublicKey(derivedPrivateKey) | ||
xPubKey: new Bitcore.HDPublicKey(derivedPrivateKey['BIP44']), | ||
}]; | ||
var utxos = helpers.generateUtxos(publicKeyRing, 'm/1/0', 1, [0.001]); | ||
var utxos = helpers.generateUtxos('P2PKH', publicKeyRing, 'm/1/0', 1, [0.001]); | ||
var txp = { | ||
@@ -447,3 +463,4 @@ inputs: utxos, | ||
fee: 10000, | ||
derivationStrategy: 'BIP45', | ||
derivationStrategy: 'BIP44', | ||
addressType: 'P2PKH', | ||
}; | ||
@@ -470,6 +487,6 @@ var t = WalletUtils.buildTx(txp); | ||
var publicKeyRing = [{ | ||
xPubKey: new Bitcore.HDPublicKey(derivedPrivateKey) | ||
xPubKey: new Bitcore.HDPublicKey(derivedPrivateKey['BIP44']), | ||
}]; | ||
var utxos = helpers.generateUtxos(publicKeyRing, 'm/1/0', 1, [0.001]); | ||
var utxos = helpers.generateUtxos('P2PKH', publicKeyRing, 'm/1/0', 1, [0.001]); | ||
var txp = { | ||
@@ -495,3 +512,4 @@ inputs: utxos, | ||
fee: 10000, | ||
derivationStrategy: 'BIP45', | ||
derivationStrategy: 'BIP44', | ||
addressType: 'P2PKH', | ||
}; | ||
@@ -512,8 +530,3 @@ (function() { | ||
describe('#signTxp', function() { | ||
var masterPrivateKey, derivedPrivateKey; | ||
beforeEach(function() { | ||
masterPrivateKey = 'tprv8ZgxMBicQKsPdPLE72pfSo7CvzTsWddGHdwSuMNrcerr8yQZKdaPXiRtP9Ew8ueSe9M7jS6RJsp4DiAVS2xmyxcCC9kZV6X1FMsX7EQX2R5'; | ||
derivedPrivateKey = WalletUtils.deriveXPrivFromMaster(masterPrivateKey, 'BIP45', 'testnet'); | ||
}); | ||
it('should sign correctly', function() { | ||
it('should sign BIP45 P2SH correctly', function() { | ||
var toAddress = 'msj42CCGruhRsFrGATiUuh25dtxYtnpbTx'; | ||
@@ -523,7 +536,6 @@ var changeAddress = 'msj42CCGruhRsFrGATiUuh25dtxYtnpbTx'; | ||
var publicKeyRing = [{ | ||
xPubKey: new Bitcore.HDPublicKey(derivedPrivateKey) | ||
xPubKey: new Bitcore.HDPublicKey(derivedPrivateKey['BIP45']), | ||
}]; | ||
var path = 'm/1/0'; | ||
var utxos = helpers.generateUtxos(publicKeyRing, path, 1, [1000, 2000]); | ||
var utxos = helpers.generateUtxos('P2SH', publicKeyRing, 'm/2147483647/0/0', 1, [1000, 2000]); | ||
var txp = { | ||
@@ -540,2 +552,3 @@ inputs: utxos, | ||
derivationStrategy: 'BIP45', | ||
addressType: 'P2SH', | ||
}; | ||
@@ -545,6 +558,3 @@ var signatures = WalletUtils.signTxp(txp, masterPrivateKey); | ||
}); | ||
it('should sign BIP44 proposal correctly', function() { | ||
masterPrivateKey = 'tprv8ZgxMBicQKsPdPLE72pfSo7CvzTsWddGHdwSuMNrcerr8yQZKdaPXiRtP9Ew8ueSe9M7jS6RJsp4DiAVS2xmyxcCC9kZV6X1FMsX7EQX2R5'; | ||
derivedPrivateKey = WalletUtils.deriveXPrivFromMaster(masterPrivateKey, 'BIP44', 'testnet'); | ||
it('should sign BIP44 P2PKH correctly', function() { | ||
var toAddress = 'msj42CCGruhRsFrGATiUuh25dtxYtnpbTx'; | ||
@@ -554,7 +564,6 @@ var changeAddress = 'msj42CCGruhRsFrGATiUuh25dtxYtnpbTx'; | ||
var publicKeyRing = [{ | ||
xPubKey: new Bitcore.HDPublicKey(derivedPrivateKey) | ||
xPubKey: new Bitcore.HDPublicKey(derivedPrivateKey['BIP44']), | ||
}]; | ||
var path = 'm/1/0'; | ||
var utxos = helpers.generateUtxos(publicKeyRing, path, 1, [1000, 2000]); | ||
var utxos = helpers.generateUtxos('P2PKH', publicKeyRing, 'm/1/0', 1, [1000, 2000]); | ||
var txp = { | ||
@@ -571,2 +580,3 @@ inputs: utxos, | ||
derivationStrategy: 'BIP44', | ||
addressType: 'P2PKH', | ||
}; | ||
@@ -581,7 +591,6 @@ var signatures = WalletUtils.signTxp(txp, masterPrivateKey); | ||
var publicKeyRing = [{ | ||
xPubKey: new Bitcore.HDPublicKey(derivedPrivateKey) | ||
xPubKey: new Bitcore.HDPublicKey(derivedPrivateKey['BIP44']), | ||
}]; | ||
var path = 'm/1/0'; | ||
var utxos = helpers.generateUtxos(publicKeyRing, path, 1, [1000, 2000]); | ||
var utxos = helpers.generateUtxos('P2PKH', publicKeyRing, 'm/1/0', 1, [1000, 2000]); | ||
var txp = { | ||
@@ -605,3 +614,4 @@ inputs: utxos, | ||
fee: 10000, | ||
derivationStrategy: 'BIP45', | ||
derivationStrategy: 'BIP44', | ||
addressType: 'P2PKH', | ||
}; | ||
@@ -616,6 +626,6 @@ var signatures = WalletUtils.signTxp(txp, masterPrivateKey); | ||
var publicKeyRing = [{ | ||
xPubKey: new Bitcore.HDPublicKey(derivedPrivateKey) | ||
xPubKey: new Bitcore.HDPublicKey(derivedPrivateKey['BIP44']), | ||
}]; | ||
var utxos = helpers.generateUtxos(publicKeyRing, 'm/1/0', 1, [0.001]); | ||
var utxos = helpers.generateUtxos('P2PKH', publicKeyRing, 'm/1/0', 1, [0.001]); | ||
var txp = { | ||
@@ -640,3 +650,4 @@ inputs: utxos, | ||
fee: 10000, | ||
derivationStrategy: 'BIP45', | ||
derivationStrategy: 'BIP44', | ||
addressType: 'P2PKH', | ||
}; | ||
@@ -643,0 +654,0 @@ var signatures = WalletUtils.signTxp(txp, masterPrivateKey); |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
39255
939