bitcore-wallet-utils
Advanced tools
Comparing version 0.1.0 to 0.1.1
@@ -20,4 +20,4 @@ 'use strict'; | ||
REQUEST_KEY: "m/1'/0", | ||
TMP_REQUEST_KEY: "m/1/0", | ||
TXPROPOSAL_KEY: "m/1'/1", | ||
REQUEST_KEY_AUTH: "m/2", // relative to BASE_ | ||
}; | ||
@@ -30,16 +30,2 @@ | ||
WalletUtils.FEE_LEVELS = [{ | ||
name: 'priority', | ||
nbBlocks: 1, | ||
defaultValue: 50000 | ||
}, { | ||
name: 'normal', | ||
nbBlocks: 4, | ||
defaultValue: 20000 | ||
}, { | ||
name: 'economy', | ||
nbBlocks: 12, | ||
defaultValue: 10000 | ||
}]; | ||
/* TODO: It would be nice to be compatible with bitcoind signmessage. How | ||
@@ -193,7 +179,15 @@ * the hash is calculated there? */ | ||
if (txp.toAddress && txp.amount) { | ||
if (txp.toAddress && txp.amount && !txp.outputs) { | ||
t.to(txp.toAddress, txp.amount); | ||
} else if (txp.outputs) { | ||
_.each(txp.outputs, function(o) { | ||
t.to(o.toAddress, o.amount); | ||
$.checkState(!o.script != !o.toAddress, 'Output should have either toAddress or script specified'); | ||
if (o.script) { | ||
t.addOutput(new Bitcore.Transaction.Output({ | ||
script: o.script, | ||
satoshis: o.amount | ||
})); | ||
} else { | ||
t.to(o.toAddress, o.amount); | ||
} | ||
}); | ||
@@ -236,5 +230,5 @@ } | ||
var self = this; | ||
$.checkArgument(txp); | ||
$.checkArgument(xPrivKey); | ||
$.checkArgument(txp.toAddress || (txp.outputs && txp.outputs[0].toAddress), 'toAddress is invalid'); | ||
$.checkArgument(txp.amount || (txp.outputs && txp.outputs[0].amount), 'amount is invalid'); | ||
@@ -248,4 +242,3 @@ $.checkArgument(txp.changeAddress && txp.changeAddress.address, 'changeAddress is invalid'); | ||
var toAddress = txp.toAddress || txp.outputs[0].toAddress; | ||
var network = new Bitcore.Address(toAddress).network.name; | ||
var network = new Bitcore.Address(txp.changeAddress.address).network.name; | ||
var xpriv = new Bitcore.HDPrivateKey(xPrivKey, network).derive(WalletUtils.PATHS.BASE_ADDRESS_DERIVATION); | ||
@@ -318,2 +311,12 @@ | ||
WalletUtils.signRequestPubKey = function(requestPubKey, xPrivKey) { | ||
var priv = new Bitcore.HDPrivateKey(xPrivKey).derive(WalletUtils.PATHS.REQUEST_KEY_AUTH).privateKey; | ||
return WalletUtils.signMessage(requestPubKey, priv); | ||
}; | ||
WalletUtils.verifyRequestPubKey = function(requestPubKey, signature, xPubKey) { | ||
var pub = (new Bitcore.HDPublicKey(xPubKey)).derive(WalletUtils.PATHS.REQUEST_KEY_AUTH).publicKey; | ||
return WalletUtils.verifyMessage(requestPubKey, signature, pub.toString()); | ||
}; | ||
module.exports = WalletUtils; |
@@ -5,3 +5,3 @@ { | ||
"author": "BitPay Inc", | ||
"version": "0.1.0", | ||
"version": "0.1.1", | ||
"main": "index.js", | ||
@@ -8,0 +8,0 @@ "keywords": [ |
@@ -32,3 +32,3 @@ 'use strict'; | ||
// Amounts in satoshis | ||
// Amounts in satoshis | ||
helpers.generateUtxos = function(publicKeyRing, path, requiredSignatures, amounts) { | ||
@@ -391,2 +391,103 @@ var amounts = [].concat(amounts); | ||
}); | ||
it('should build a tx with provided output scripts', function() { | ||
var hdPrivateKey = new Bitcore.HDPrivateKey('tprv8ZgxMBicQKsPdPLE72pfSo7CvzTsWddGHdwSuMNrcerr8yQZKdaPXiRtP9Ew8ueSe9M7jS6RJsp4DiAVS2xmyxcCC9kZV6X1FMsX7EQX2R5'); | ||
var derivedPrivateKey = hdPrivateKey.derive(WalletUtils.PATHS.BASE_ADDRESS_DERIVATION); | ||
var toAddress = 'msj42CCGruhRsFrGATiUuh25dtxYtnpbTx'; | ||
var changeAddress = 'msj42CCGruhRsFrGATiUuh25dtxYtnpbTx'; | ||
var publicKeyRing = [{ | ||
xPubKey: new Bitcore.HDPublicKey(derivedPrivateKey) | ||
}]; | ||
var utxos = helpers.generateUtxos(publicKeyRing, 'm/1/0', 1, [0.001]); | ||
var txp = { | ||
inputs: utxos, | ||
type: 'external', | ||
outputs: [ | ||
{ | ||
"amount":700, | ||
"script":"512103ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff210314a96cd6f5a20826070173fe5b7e9797f21fc8ca4a55bcb2d2bde99f55dd352352ae" | ||
}, | ||
{ | ||
"amount":600, | ||
"script":"76a9144d5bd54809f846dc6b1a14cbdd0ac87a3c66f76688ac" | ||
}, | ||
{ | ||
"amount":0, | ||
"script":"6a1e43430102fa9213bc243af03857d0f9165e971153586d3915201201201210" | ||
} | ||
], | ||
changeAddress: { | ||
address: changeAddress | ||
}, | ||
requiredSignatures: 1, | ||
outputOrder: [0, 1, 2, 3], | ||
fee: 10000, | ||
}; | ||
var t = WalletUtils.buildTx(txp); | ||
var bitcoreError = t.getSerializationError({ | ||
disableIsFullySigned: true, | ||
}); | ||
should.not.exist(bitcoreError); | ||
t.outputs.length.should.equal(4); | ||
t.outputs[0].script.toHex().should.equal(txp.outputs[0].script); | ||
t.outputs[0].satoshis.should.equal(txp.outputs[0].amount); | ||
t.outputs[1].script.toHex().should.equal(txp.outputs[1].script); | ||
t.outputs[1].satoshis.should.equal(txp.outputs[1].amount); | ||
t.outputs[2].script.toHex().should.equal(txp.outputs[2].script); | ||
t.outputs[2].satoshis.should.equal(txp.outputs[2].amount); | ||
var changeScript = Bitcore.Script.fromAddress(txp.changeAddress.address).toHex(); | ||
t.outputs[3].script.toHex().should.equal(changeScript); | ||
}); | ||
it('should fail if provided output has both toAddress and script', function() { | ||
var hdPrivateKey = new Bitcore.HDPrivateKey('tprv8ZgxMBicQKsPdPLE72pfSo7CvzTsWddGHdwSuMNrcerr8yQZKdaPXiRtP9Ew8ueSe9M7jS6RJsp4DiAVS2xmyxcCC9kZV6X1FMsX7EQX2R5'); | ||
var derivedPrivateKey = hdPrivateKey.derive(WalletUtils.PATHS.BASE_ADDRESS_DERIVATION); | ||
var toAddress = 'msj42CCGruhRsFrGATiUuh25dtxYtnpbTx'; | ||
var changeAddress = 'msj42CCGruhRsFrGATiUuh25dtxYtnpbTx'; | ||
var publicKeyRing = [{ | ||
xPubKey: new Bitcore.HDPublicKey(derivedPrivateKey) | ||
}]; | ||
var utxos = helpers.generateUtxos(publicKeyRing, 'm/1/0', 1, [0.001]); | ||
var txp = { | ||
inputs: utxos, | ||
type: 'external', | ||
outputs: [ | ||
{ | ||
"toAddress":"18433T2TSgajt9jWhcTBw4GoNREA6LpX3E", | ||
"amount":700, | ||
"script":"512103ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff210314a96cd6f5a20826070173fe5b7e9797f21fc8ca4a55bcb2d2bde99f55dd352352ae" | ||
}, | ||
{ | ||
"amount":600, | ||
"script":"76a9144d5bd54809f846dc6b1a14cbdd0ac87a3c66f76688ac" | ||
}, | ||
{ | ||
"amount":0, | ||
"script":"6a1e43430102fa9213bc243af03857d0f9165e971153586d3915201201201210" | ||
} | ||
], | ||
changeAddress: { | ||
address: changeAddress | ||
}, | ||
requiredSignatures: 1, | ||
outputOrder: [0, 1, 2, 3], | ||
fee: 10000, | ||
}; | ||
(function() { | ||
var t = WalletUtils.buildTx(txp); | ||
}).should.throw('Output should have either toAddress or script specified'); | ||
delete txp.outputs[0].toAddress; | ||
var t = WalletUtils.buildTx(txp); | ||
var bitcoreError = t.getSerializationError({ | ||
disableIsFullySigned: true, | ||
}); | ||
should.not.exist(bitcoreError); | ||
}); | ||
}); | ||
@@ -457,2 +558,41 @@ | ||
}); | ||
it('should sign proposal with provided output scripts correctly', function() { | ||
var hdPrivateKey = new Bitcore.HDPrivateKey('tprv8ZgxMBicQKsPdPLE72pfSo7CvzTsWddGHdwSuMNrcerr8yQZKdaPXiRtP9Ew8ueSe9M7jS6RJsp4DiAVS2xmyxcCC9kZV6X1FMsX7EQX2R5'); | ||
var derivedPrivateKey = hdPrivateKey.derive(WalletUtils.PATHS.BASE_ADDRESS_DERIVATION); | ||
var toAddress = 'msj42CCGruhRsFrGATiUuh25dtxYtnpbTx'; | ||
var changeAddress = 'msj42CCGruhRsFrGATiUuh25dtxYtnpbTx'; | ||
var publicKeyRing = [{ | ||
xPubKey: new Bitcore.HDPublicKey(derivedPrivateKey) | ||
}]; | ||
var utxos = helpers.generateUtxos(publicKeyRing, 'm/1/0', 1, [0.001]); | ||
var txp = { | ||
inputs: utxos, | ||
type: 'external', | ||
outputs: [ | ||
{ | ||
"amount":700, | ||
"script":"512103ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff210314a96cd6f5a20826070173fe5b7e9797f21fc8ca4a55bcb2d2bde99f55dd352352ae" | ||
}, | ||
{ | ||
"amount":600, | ||
"script":"76a9144d5bd54809f846dc6b1a14cbdd0ac87a3c66f76688ac" | ||
}, | ||
{ | ||
"amount":0, | ||
"script":"6a1e43430102fa9213bc243af03857d0f9165e971153586d3915201201201210" | ||
} | ||
], | ||
changeAddress: { | ||
address: changeAddress | ||
}, | ||
requiredSignatures: 1, | ||
outputOrder: [0, 1, 2, 3], | ||
fee: 10000, | ||
}; | ||
var signatures = WalletUtils.signTxp(txp, hdPrivateKey); | ||
signatures.length.should.be.equal(utxos.length); | ||
}); | ||
}); | ||
@@ -512,3 +652,27 @@ | ||
}); | ||
describe('#verifyRequestPubKey', function() { | ||
it('should generate and check request pub key', function() { | ||
var reqPubKey = (new Bitcore.PrivateKey).toPublicKey(); | ||
var xPrivKey = new Bitcore.HDPrivateKey(); | ||
var xPubKey = new Bitcore.HDPublicKey(xPrivKey); | ||
var sig = WalletUtils.signRequestPubKey(reqPubKey.toString(), xPrivKey); | ||
var valid = WalletUtils.verifyRequestPubKey(reqPubKey.toString(), sig, xPubKey); | ||
valid.should.be.equal(true); | ||
}); | ||
it('should fail to check a request pub key with wrong key', function() { | ||
var reqPubKey = '02c2c1c6e75cfc50235ff4a2eb848385c2871b8c94e285ee82eaced1dcd5dd568e'; | ||
var xPrivKey = new Bitcore.HDPrivateKey(); | ||
var xPubKey = new Bitcore.HDPublicKey(xPrivKey); | ||
var sig = WalletUtils.signRequestPubKey(reqPubKey, xPrivKey); | ||
var xPrivKey2 = new Bitcore.HDPrivateKey(); | ||
var xPubKey2 = new Bitcore.HDPublicKey(xPrivKey2); | ||
var valid = WalletUtils.verifyRequestPubKey(reqPubKey, sig, xPubKey2); | ||
valid.should.be.equal(false); | ||
}); | ||
}); | ||
}); |
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
38981
12
936