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

View File

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