more wallet work.

This commit is contained in:
Christopher Jeffrey 2016-06-24 16:49:02 -07:00
parent f1376e5a99
commit 4c9dca65bd
No known key found for this signature in database
GPG Key ID: 8962AB9DE6666BBD
2 changed files with 122 additions and 92 deletions

View File

@ -30,11 +30,30 @@ var BufferWriter = require('./writer');
*/ */
function KeyRing(options) { function KeyRing(options) {
var i;
if (!(this instanceof KeyRing)) if (!(this instanceof KeyRing))
return new KeyRing(options); return new KeyRing(options);
this.network = null;
this.type = null;
this.m = 1;
this.n = 1;
this.witness = false;
this.name = null;
this.account = 0;
this.change = 0;
this.index = 0;
this.key = null;
this.keys = [];
this.addressMap = null;
if (options)
this.fromOptions(options);
}
KeyRing.prototype.fromOptions = function fromOptions(options) {
var i;
this.network = bcoin.network.get(options.network); this.network = bcoin.network.get(options.network);
this.type = options.type || 'pubkeyhash'; this.type = options.type || 'pubkeyhash';
this.m = options.m || 1; this.m = options.m || 1;
@ -50,8 +69,6 @@ function KeyRing(options) {
if (this.n > 1) if (this.n > 1)
this.type = 'multisig'; this.type = 'multisig';
this.addressMap = null;
assert(this.type === 'pubkeyhash' || this.type === 'multisig'); assert(this.type === 'pubkeyhash' || this.type === 'multisig');
if (this.m < 1 || this.m > this.n) if (this.m < 1 || this.m > this.n)
@ -63,7 +80,13 @@ function KeyRing(options) {
for (i = 0; i < options.keys.length; i++) for (i = 0; i < options.keys.length; i++)
this.addKey(options.keys[i]); this.addKey(options.keys[i]);
} }
}
return this;
};
KeyRing.fromOptions = function fromOptions(options) {
return new KeyRing().fromOptions(options);
};
/** /**
* Add a key to shared keys. * Add a key to shared keys.
@ -553,20 +576,23 @@ KeyRing.prototype.toJSON = function toJSON() {
* @returns {KeyRing} * @returns {KeyRing}
*/ */
KeyRing.prototype.fromJSON = function fromJSON(json) {
this.nework = bcoin.network.get(json.network);
this.type = json.type;
this.m = json.m;
this.n = json.n;
this.witness = json.witness;
this.name = json.name;
this.account = json.account;
this.change = json.change;
this.index = json.index;
this.key = utils.fromBase58(json.key);
this.keys = json.keys.map(utils.fromBase58);
return this;
};
KeyRing.fromJSON = function fromJSON(json) { KeyRing.fromJSON = function fromJSON(json) {
return new KeyRing({ return new KeyRing().fromJSON(json);
nework: json.network,
type: json.type,
m: json.m,
n: json.n,
witness: json.witness,
name: json.name,
account: json.account,
change: json.change,
index: json.index,
key: utils.fromBase58(json.key),
keys: json.keys.map(utils.fromBase58)
});
}; };
/** /**
@ -604,38 +630,32 @@ KeyRing.prototype.toRaw = function toRaw(writer) {
* @returns {KeyRing} * @returns {KeyRing}
*/ */
KeyRing.fromRaw = function fromRaw(data) { KeyRing.prototype.fromRaw = function fromRaw(data) {
var p = new BufferReader(data); var p = new BufferReader(data);
var network = bcoin.network.fromMagic(p.readU32()); var i, count;
var type = p.readU8() === 0 ? 'pubkeyhash' : 'multisig';
var m = p.readU8(); this.network = bcoin.network.fromMagic(p.readU32());
var n = p.readU8(); this.type = p.readU8() === 0 ? 'pubkeyhash' : 'multisig';
var witness = p.readU8() === 1; this.m = p.readU8();
var name = p.readVarString('utf8'); this.n = p.readU8();
var account = p.readU32(); this.witness = p.readU8() === 1;
var change = p.readU32(); this.name = p.readVarString('utf8');
var index = p.readU32(); this.account = p.readU32();
var key = p.readVarBytes(); this.change = p.readU32();
var count = p.readU8(); this.index = p.readU32();
var keys = []; this.key = p.readVarBytes();
var i; this.keys = [];
count = p.readU8();
for (i = 0; i < count; i++) for (i = 0; i < count; i++)
keys.push(p.readVarBytes()); this.keys.push(p.readVarBytes());
return new KeyRing({ return this;
nework: network, };
type: type,
m: m, KeyRing.fromRaw = function fromRaw(data) {
n: n, return new KeyRing().fromRaw(data);
witness: witness,
name: name,
account: account,
change: change,
index: index,
key: key,
keys: keys
});
}; };
/** /**

View File

@ -2286,16 +2286,30 @@ function MasterKey(options) {
if (!(this instanceof MasterKey)) if (!(this instanceof MasterKey))
return new MasterKey(options); return new MasterKey(options);
this.encrypted = false;
this.xprivkey = null;
this.phrase = null;
this.passphrase = null;
this.key = null;
this.timer = null;
this._destroy = this.destroy.bind(this);
}
MasterKey.prototype.fromOptions = function fromOptions(options) {
this.encrypted = !!options.encrypted; this.encrypted = !!options.encrypted;
this.xprivkey = options.xprivkey; this.xprivkey = options.xprivkey;
this.phrase = options.phrase; this.phrase = options.phrase;
this.passphrase = options.passphrase; this.passphrase = options.passphrase;
this.key = options.key || null; this.key = options.key || null;
this.timer = null;
this._destroy = this.destroy.bind(this);
assert(this.encrypted ? !this.key : this.key); assert(this.encrypted ? !this.key : this.key);
}
return this;
};
MasterKey.fromOptions = function fromOptions(options) {
return new MasterKey().fromOptions(options);
};
/** /**
* Decrypt the key and set a timeout to destroy decrypted data. * Decrypt the key and set a timeout to destroy decrypted data.
@ -2444,7 +2458,7 @@ MasterKey.prototype.toRaw = function toRaw(writer) {
p.writeU8(0); p.writeU8(0);
} }
p.writeBytes(this.xprivkey); p.writeVarBytes(this.xprivkey);
if (!writer) if (!writer)
p = p.render(); p = p.render();
@ -2457,29 +2471,26 @@ MasterKey.prototype.toRaw = function toRaw(writer) {
* @returns {MasterKey} * @returns {MasterKey}
*/ */
MasterKey.fromRaw = function fromRaw(raw) { MasterKey.prototype.fromRaw = function fromRaw(raw) {
var p = new BufferReader(raw); var p = new BufferReader(raw);
var encrypted, phrase, passphrase, xprivkey, key;
encrypted = p.readU8() === 1; this.encrypted = p.readU8() === 1;
if (p.readU8() === 1) { if (p.readU8() === 1) {
phrase = p.readVarBytes(); this.phrase = p.readVarBytes();
passphrase = p.readVarBytes(); this.passphrase = p.readVarBytes();
} }
xprivkey = p.readBytes(82); this.xprivkey = p.readVarBytes();
if (!encrypted) if (!this.encrypted)
key = bcoin.hd.fromRaw(xprivkey); this.key = bcoin.hd.fromRaw(this.xprivkey);
return new MasterKey({ return this;
encrypted: encrypted, };
phrase: phrase,
passphrase: passphrase, MasterKey.fromRaw = function fromRaw(raw) {
xprivkey: xprivkey, return new MasterKey().fromRaw(raw);
key: key
});
}; };
/** /**
@ -2488,21 +2499,22 @@ MasterKey.fromRaw = function fromRaw(raw) {
* @returns {MasterKey} * @returns {MasterKey}
*/ */
MasterKey.fromKey = function fromKey(key) { MasterKey.prototype.fromKey = function fromKey(key) {
var phrase, passphrase; this.encrypted = false;
if (key.mnemonic) { if (key.mnemonic) {
phrase = new Buffer(key.mnemonic.phrase, 'utf8'); this.phrase = new Buffer(key.mnemonic.phrase, 'utf8');
passphrase = new Buffer(key.mnemonic.passphrase, 'utf8'); this.passphrase = new Buffer(key.mnemonic.passphrase, 'utf8');
} }
return new MasterKey({ this.xprivkey = key.toRaw();
encrypted: false, this.key = key;
phrase: phrase,
passphrase: passphrase, return this;
xprivkey: key.toRaw(), };
key: key
}); MasterKey.fromKey = function fromKey(key) {
return new MasterKey().fromKey(key);
}; };
/** /**
@ -2540,33 +2552,31 @@ MasterKey.prototype.toJSON = function toJSON() {
* @returns {MasterKey} * @returns {MasterKey}
*/ */
MasterKey.fromJSON = function fromJSON(json) { MasterKey.prototype.fromJSON = function fromJSON(json) {
var phrase, passphrase, xprivkey, key; this.encrypted = json.encrypted;
if (json.encrypted) { if (json.encrypted) {
if (json.phrase) { if (json.phrase) {
phrase = new Buffer(json.phrase, 'hex'); this.phrase = new Buffer(json.phrase, 'hex');
passphrase = new Buffer(json.passphrase, 'hex'); this.passphrase = new Buffer(json.passphrase, 'hex');
} }
xprivkey = new Buffer(json.xprivkey, 'hex'); this.xprivkey = new Buffer(json.xprivkey, 'hex');
} else { } else {
if (json.phrase) { if (json.phrase) {
phrase = new Buffer(json.phrase, 'utf8'); this.phrase = new Buffer(json.phrase, 'utf8');
passphrase = new Buffer(json.passphrase, 'utf8'); this.passphrase = new Buffer(json.passphrase, 'utf8');
} }
xprivkey = utils.fromBase58(json.xprivkey); this.xprivkey = utils.fromBase58(json.xprivkey);
} }
if (!json.encrypted) if (!json.encrypted)
key = bcoin.hd.fromRaw(xprivkey); this.key = bcoin.hd.fromRaw(xprivkey);
return new MasterKey({ return this;
encrypted: json.encrypted, };
phrase: phrase,
passphrase: passphrase, MasterKey.fromJSON = function fromJSON(key) {
xprivkey: xprivkey, return new MasterKey().fromJSON(key);
key: key
});
}; };
/** /**