From 468ebddc1e49881ed3a352fecb450f64feae1d45 Mon Sep 17 00:00:00 2001 From: olalonde Date: Mon, 5 May 2014 17:15:39 +0800 Subject: [PATCH] Implemented Armory initializing from seed --- examples/Armory.js | 12 ++++++++++ lib/Armory.js | 58 ++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 70 insertions(+) diff --git a/examples/Armory.js b/examples/Armory.js index beee650..f02553e 100644 --- a/examples/Armory.js +++ b/examples/Armory.js @@ -17,6 +17,8 @@ var chaincode = '84ac14bc4b388b33da099a0b4ee3b507284d99e1476639e36e5ca5e6af86481 //var PublicX = '9df5 23e7 18b9 1f59 a790 2d46 999f 9357 ccf8 7208 24d4 3076 4516 b809 f7ab ce4e' //var PublicY = '66ba 5d21 4682 0dae 401d 9506 8437 2516 79f9 0c56 4186 cc50 07df c6d0 6989 1ff4'; //var pubkey = '04' + PublicX.split(' ').join('') + PublicY.split(' ').join(''); +//console.log(pubkey); + // mmm... can't figure out how to arrive at same pubkey as brainwallet var pubkey = '045a09a3286873a72f164476bde9d1d8e5c2bc044e35aa47eb6e798e325a86417f7c35b61d9905053533e0b4f2a26eca0330aadf21c638969e45aaace50e4c0c87'; @@ -28,3 +30,13 @@ for (var i = 0; i < 5; i++) { armory = armory.next(); console.log(Address.fromPubKey(armory.pubkey).as('base58')); } + +// Derive stuff from root cod +var seed = [ + 'aagh hjfj sihk ietj giik wwai awtd uodh hnji', + 'soss uaku egod utai itos fijj ihgi jhau jtoo' +].join('\n'); + +console.log(seed); + +console.log(Armory.fromSeed(seed)); diff --git a/lib/Armory.js b/lib/Armory.js index 03a7b10..6cc0d85 100644 --- a/lib/Armory.js +++ b/lib/Armory.js @@ -1,4 +1,6 @@ var Point = require('./browser/Point'), + buffertools = require('buffertools'), + sha256 = require('../util').sha256, twoSha256 = require('../util').twoSha256, BigInteger = require('../browser/vendor-bundle.js').BigInteger; @@ -38,4 +40,60 @@ Armory.prototype.next = function () { return new Armory(this.chaincode, next_pubkey); }; +function decode (str) { + var from = '0123456789abcdef'; + var to = 'asdfghjkwertuion'; + var res = ''; + for (var i = 0; i < str.length; i++) + res += from.charAt(to.indexOf(str.charAt(i))); + return res; +} + +Armory.decodeSeed = function (seed) { + var keys = seed.split('\n'); + var lines = []; + + for (var i = 0; i < keys.length; i++) { + var k = keys[i].replace(' ',''); + var raw = new Buffer(decode(k), 'hex'); + var data = raw.slice(0, 16); + lines.push(data); + } + + var privKey = Buffer.concat([ lines[0], lines[1] ]); + var chainCode = (lines.length==4) ? + Buffer.concat([ lines[2], lines[3] ]) : Armory.deriveChaincode(privKey); + + return { + privKey: privKey, + chainCode: chainCode + }; +}; + +// Derive chain code from root key +Armory.fromSeed = function (seed) { + var res = Armory.decodeSeed(seed); + return res; +}; + +Armory.deriveChaincode = function (root) { + var msg = 'Derive Chaincode from Root Key'; + var hash = twoSha256(root); + + var okey = []; + var ikey = []; + for (var i = 0; i < hash.length; i++) { + okey.push(0x5c ^ hash[i]); + ikey.push(0x36 ^ hash[i]); + } + + okey = new Buffer(okey); + ikey = new Buffer(ikey); + + var m = new Buffer(msg, 'utf8'); + var a = sha256(Buffer.concat([ ikey, m ])); + var b = sha256(Buffer.concat([ okey, a ])); + return b; +}; + module.exports = Armory;