add getFullPublicKey/getOwnPublicKey and use appropriately.

This commit is contained in:
Christopher Jeffrey 2015-12-08 18:43:33 -08:00
parent 71ea61e467
commit 8a33f2efa1
3 changed files with 68 additions and 31 deletions

View File

@ -410,8 +410,11 @@ Pool.prototype._removePeer = function _removePeer(peer) {
Pool.prototype.watch = function watch(id) {
if (id instanceof bcoin.wallet) {
this.watch(id.getAddress());
this.watch(id.getPublicKey());
// this.watch(id.getAddress());
this.watch(id.getFullHash());
this.watch(id.getFullPublicKey());
this.watch(id.getOwnHash());
this.watch(id.getOwnPublicKey());
return;
}
@ -460,8 +463,10 @@ Pool.prototype.unwatch = function unwatch(id) {
Pool.prototype.addWallet = function addWallet(w, defaultTs) {
if (this.wallets.indexOf(w) !== -1)
return false;
this.watch(w.getHash());
this.watch(w.getPublicKey());
this.watch(w.getFullHash());
this.watch(w.getFullPublicKey());
this.watch(w.getOwnHash());
this.watch(w.getOwnPublicKey());
var self = this;
var e = new EventEmitter();
@ -492,8 +497,10 @@ Pool.prototype.removeWallet = function removeWallet(w) {
if (i == -1)
return;
this.wallets.splice(i, 1);
this.unwatch(w.getHash());
this.unwatch(w.getPublicKey());
this.unwatch(w.getFullHash());
this.unwatch(w.getFullPublicKey());
this.unwatch(w.getOwnHash());
this.unwatch(w.getOwnPublicKey());
}
Pool.prototype.search = function search(id, range, e) {

View File

@ -593,6 +593,7 @@ script.multisig = function(keys, m, n) {
// [ [ n ], 'checkmultisig' ]
// );
// Keys need to be in a predictable order.
keys = keys.sort(function(a, b) {
return new bn(a).cmp(new bn(b)) > 0;
});

View File

@ -69,7 +69,7 @@ function Wallet(options, passphrase) {
this.multisig(options.multisig || {});
this.prefix = 'bt/' + this.getAddress() + '/';
this.prefix = 'bt/' + this.getOwnAddress() + '/';
this.tx = new bcoin.txPool(this);
// Just a constants, actually
@ -114,7 +114,7 @@ Wallet.prototype._init = function init() {
};
Wallet.prototype.multisig = function multisig(options) {
var pub = this.key.getPublic(this.compressed, 'array');
var pub = this.getOwnPublicKey();
options.type = options.type || options.addressType;
options.keys = options.keys || options.sharedKeys;
@ -185,8 +185,8 @@ Wallet.prototype.getPrivateKey = function getPrivateKey(enc) {
}
};
Wallet.prototype.getPublicKey = function getPublicKey(enc) {
var pub = this.key.getPublic(this.compressed, 'array');
Wallet.prototype.getFullPublicKey = function getFullPublicKey(enc) {
var pub = this.getOwnPublicKey();
if (this.addressType === 'p2sh') {
var keys = this.getPublicKeys();
@ -201,7 +201,7 @@ Wallet.prototype.getPublicKey = function getPublicKey(enc) {
return pub;
};
Wallet.prototype._getPublicKey = function _getPublicKey(enc) {
Wallet.prototype.getOwnPublicKey = function getOwnPublicKey(enc) {
var pub = this.key.getPublic(this.compressed, 'array');
if (enc === 'base58')
@ -212,13 +212,22 @@ Wallet.prototype._getPublicKey = function _getPublicKey(enc) {
return pub;
};
Wallet.prototype.getPublicKey = function getPublicKey(enc) {
return this.getFullPublicKey(enc);
};
Wallet.prototype.getPublicKeys = function() {
var pub = this.getOwnPublicKey();
this.sharedKeys = this.sharedKeys.filter(function(key) {
return !utils.isEqual(key, pub);
});
var keys = this.sharedKeys.slice().map(utils.toKeyArray);
// if (keys.length < this.m) {
var pub = this.key.getPublic(this.compressed, 'array');
keys.push(pub);
// Keys need to be in a predictable order.
keys = keys.sort(function(a, b) {
return new bn(a).cmp(new bn(b)) > 0;
});
@ -226,12 +235,28 @@ Wallet.prototype.getPublicKeys = function() {
return keys;
};
Wallet.prototype.getFullHash = function getFullHash() {
return utils.ripesha(this.getFullPublicKey());
};
Wallet.prototype.getFullAddress = function getFullAddress() {
return Wallet.hash2addr(this.getFullHash(), this.addressType);
};
Wallet.prototype.getOwnHash = function getOwnHash() {
return utils.ripesha(this.getOwnPublicKey());
};
Wallet.prototype.getOwnAddress = function getOwnAddress() {
return Wallet.hash2addr(this.getOwnHash(), this.addressType);
};
Wallet.prototype.getHash = function getHash() {
return utils.ripesha(this.getPublicKey());
return utils.ripesha(this.getFullPublicKey());
};
Wallet.prototype.getAddress = function getAddress() {
return Wallet.hash2addr(this.getHash(), this.addressType);
return Wallet.hash2addr(this.getFullHash(), this.addressType);
};
Wallet.hash2addr = function hash2addr(hash, version) {
@ -271,8 +296,9 @@ Wallet.prototype.validateAddress = function validateAddress(addr, version) {
Wallet.validateAddress = Wallet.prototype.validateAddress;
Wallet.prototype.ownOutput = function ownOutput(tx, index) {
var hash = this.getHash();
var key = this.getPublicKey();
var scriptHash = this.getFullHash();
var hash = this.getOwnHash();
var key = this.getOwnPublicKey();
var outputs = tx.outputs.filter(function(output, i) {
if (index !== undefined && index !== i)
@ -289,7 +315,7 @@ Wallet.prototype.ownOutput = function ownOutput(tx, index) {
if (bcoin.script.isMultisig(s, key))
return true;
if (bcoin.script.isScripthash(s, hash))
if (bcoin.script.isScripthash(s, scriptHash))
return true;
return false;
@ -301,8 +327,9 @@ Wallet.prototype.ownOutput = function ownOutput(tx, index) {
};
Wallet.prototype.ownInput = function ownInput(tx, index) {
var hash = this.getHash();
var key = this.getPublicKey();
var scriptHash = this.getFullHash();
var hash = this.getOwnHash();
var key = this.getOwnPublicKey();
var inputs = tx.inputs.filter(function(input, i) {
if (index !== undefined && index !== i)
@ -322,7 +349,7 @@ Wallet.prototype.ownInput = function ownInput(tx, index) {
if (bcoin.script.isMultisig(s, key))
return true;
if (bcoin.script.isScripthash(s, hash))
if (bcoin.script.isScripthash(s, scriptHash))
return true;
return false;
@ -337,7 +364,7 @@ Wallet.prototype.sign = function sign(tx, type, inputs) {
if (!type)
type = 'all';
var pub = this.getPublicKey();
var pub = this.getFullPublicKey();
var key = this.key;
inputs = inputs || tx.inputs;
@ -394,13 +421,15 @@ Wallet.prototype.toJSON = function toJSON() {
return {
v: 1,
type: 'wallet',
pub: utils.toBase58(this.key.getPublic(this.compressed, 'array')),
pub: this.getOwnPublicKey('base58'),
priv: this.getPrivateKey('base58'),
tx: this.tx.toJSON(),
addressType: this.addressType,
sharedKeys: utils.toBase58(this.sharedKeys),
m: this.m,
n: this.n
multisig: {
type: this.addressType,
keys: this.sharedKeys.map(utils.toBase58),
m: this.m,
n: this.n
}
};
};
@ -431,14 +460,14 @@ Wallet.fromJSON = function fromJSON(json) {
compressed = pub[0] !== 0x04;
}
if (json.multisig && json.multisig.keys)
json.multisig.keys = json.multisig.keys.map(utils.toKeyArray);
var w = new Wallet({
priv: priv,
pub: pub,
compressed: compressed,
addressType: json.addressType,
sharedKeys: json.sharedKeys,
m: json.m,
n: json.n
multisig: json.multisig
});
w.tx.fromJSON(json.tx);