From c2f40f40cea3b9aa33d83d40b66ebd7a6fbac6c4 Mon Sep 17 00:00:00 2001 From: Jeff Garzik Date: Wed, 10 Jul 2013 15:32:38 -0400 Subject: [PATCH 1/2] Rename compiled binding to KeyModule. --- binding.gyp | 2 +- src/eckey.cc | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/binding.gyp b/binding.gyp index 5155a3116..ec529c787 100644 --- a/binding.gyp +++ b/binding.gyp @@ -4,7 +4,7 @@ }, 'targets': [ { - 'target_name': 'Key', + 'target_name': 'KeyModule', 'sources': [ 'src/eckey.cc' ], diff --git a/src/eckey.cc b/src/eckey.cc index a6f1681f0..ed632605d 100644 --- a/src/eckey.cc +++ b/src/eckey.cc @@ -601,4 +601,4 @@ init (Handle target) bitcoin::Key::Init(target); } -NODE_MODULE(native, init) +NODE_MODULE(KeyModule, init) From bf1e16b0dad750bc64e8aad4aa1f7a4eb33f2d4c Mon Sep 17 00:00:00 2001 From: Jeff Garzik Date: Wed, 10 Jul 2013 20:07:14 -0400 Subject: [PATCH 2/2] Add PrivateKey, Key classes. Update test to enc/dec private key test vectors. --- Key.js | 3 +++ PrivateKey.js | 23 +++++++++++++++++++++++ test/basic.js | 44 ++++++++++++++++++++++++++++++++++++++++---- 3 files changed, 66 insertions(+), 4 deletions(-) create mode 100644 Key.js create mode 100644 PrivateKey.js diff --git a/Key.js b/Key.js new file mode 100644 index 000000000..64c2e6cd2 --- /dev/null +++ b/Key.js @@ -0,0 +1,3 @@ + +module.exports = require('bindings')('KeyModule'); + diff --git a/PrivateKey.js b/PrivateKey.js new file mode 100644 index 000000000..2e55af6e6 --- /dev/null +++ b/PrivateKey.js @@ -0,0 +1,23 @@ +require('classtool'); + +function ClassSpec(b) { + var superclass = b.superclass || require('./util/VersionedData').class(); + + function PrivateKey() { + PrivateKey.super(this, arguments); + }; + + PrivateKey.superclass = superclass; + superclass.applyEncodingsTo(PrivateKey); + + PrivateKey.prototype.validate = function() { + this.doAsBinary(function() { + PrivateKey.super(this, 'validate', arguments); + if (this.data.length < 32 || this.data.length > 33) + throw new Error('invalid data length'); + }); + }; + + return PrivateKey; +}; +module.defineClass(ClassSpec); diff --git a/test/basic.js b/test/basic.js index 29fb62e6a..fbca7fb1b 100644 --- a/test/basic.js +++ b/test/basic.js @@ -2,12 +2,32 @@ var assert = require('assert'); var fs = require('fs'); var Address = require('../Address').class(); +var PrivateKey = require('../PrivateKey').class(); var networks = require('../networks'); +var KeyModule = require('../Key'); suite('basic'); -function test_encode_priv(b58, payload, isTestnet) +function test_encode_priv(b58, payload, isTestnet, isCompressed) { + var network = isTestnet ? networks.testnet : networks.livenet; + var version = network.keySecret; + + var buf_pl = new Buffer(payload, 'hex'); + var buf; + if (isCompressed) { + buf = new Buffer(buf_pl.length + 1); + buf_pl.copy(buf); + buf[buf_pl.length] = 1; + } else + buf = buf_pl; + + var key = new KeyModule.Key(); + key.private = buf; + key.compressed = isCompressed; + + var privkey = new PrivateKey(version, buf); + assert.equal(privkey.toString(), b58); } function test_encode_pub(b58, payload, isTestnet, addrType) @@ -20,8 +40,23 @@ function test_encode_pub(b58, payload, isTestnet, addrType) assert.equal(addr.toString(), b58); } -function test_decode_priv(b58, payload, isTestnet) +function test_decode_priv(b58, payload, isTestnet, isCompressed) { + var network = isTestnet ? networks.testnet : networks.livenet; + var version = network.keySecret; + + var buf_pl = new Buffer(payload, 'hex'); + var buf; + if (isCompressed) { + buf = new Buffer(buf_pl.length + 1); + buf_pl.copy(buf); + buf[buf_pl.length] = 1; + } else + buf = buf_pl; + + var privkey = new PrivateKey(b58); + assert.equal(version, privkey.version()); + assert.equal(buf.toString(), privkey.payload().toString()); } function test_decode_pub(b58, payload, isTestnet, addrType) @@ -45,8 +80,9 @@ function is_valid(datum) var isTestnet = obj['isTestnet']; if (isPrivkey) { - test_encode_priv(b58, payload, isTestnet); - test_decode_priv(b58, payload, isTestnet); + var isCompressed = obj['isCompressed']; + test_encode_priv(b58, payload, isTestnet, isCompressed); + test_decode_priv(b58, payload, isTestnet, isCompressed); } else { var addrType = obj['addrType']; test_encode_pub(b58, payload, isTestnet, addrType);