add getFullPublicKey/getOwnPublicKey and use appropriately.
This commit is contained in:
parent
71ea61e467
commit
8a33f2efa1
@ -410,8 +410,11 @@ Pool.prototype._removePeer = function _removePeer(peer) {
|
|||||||
|
|
||||||
Pool.prototype.watch = function watch(id) {
|
Pool.prototype.watch = function watch(id) {
|
||||||
if (id instanceof bcoin.wallet) {
|
if (id instanceof bcoin.wallet) {
|
||||||
this.watch(id.getAddress());
|
// this.watch(id.getAddress());
|
||||||
this.watch(id.getPublicKey());
|
this.watch(id.getFullHash());
|
||||||
|
this.watch(id.getFullPublicKey());
|
||||||
|
this.watch(id.getOwnHash());
|
||||||
|
this.watch(id.getOwnPublicKey());
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -460,8 +463,10 @@ Pool.prototype.unwatch = function unwatch(id) {
|
|||||||
Pool.prototype.addWallet = function addWallet(w, defaultTs) {
|
Pool.prototype.addWallet = function addWallet(w, defaultTs) {
|
||||||
if (this.wallets.indexOf(w) !== -1)
|
if (this.wallets.indexOf(w) !== -1)
|
||||||
return false;
|
return false;
|
||||||
this.watch(w.getHash());
|
this.watch(w.getFullHash());
|
||||||
this.watch(w.getPublicKey());
|
this.watch(w.getFullPublicKey());
|
||||||
|
this.watch(w.getOwnHash());
|
||||||
|
this.watch(w.getOwnPublicKey());
|
||||||
|
|
||||||
var self = this;
|
var self = this;
|
||||||
var e = new EventEmitter();
|
var e = new EventEmitter();
|
||||||
@ -492,8 +497,10 @@ Pool.prototype.removeWallet = function removeWallet(w) {
|
|||||||
if (i == -1)
|
if (i == -1)
|
||||||
return;
|
return;
|
||||||
this.wallets.splice(i, 1);
|
this.wallets.splice(i, 1);
|
||||||
this.unwatch(w.getHash());
|
this.unwatch(w.getFullHash());
|
||||||
this.unwatch(w.getPublicKey());
|
this.unwatch(w.getFullPublicKey());
|
||||||
|
this.unwatch(w.getOwnHash());
|
||||||
|
this.unwatch(w.getOwnPublicKey());
|
||||||
}
|
}
|
||||||
|
|
||||||
Pool.prototype.search = function search(id, range, e) {
|
Pool.prototype.search = function search(id, range, e) {
|
||||||
|
|||||||
@ -593,6 +593,7 @@ script.multisig = function(keys, m, n) {
|
|||||||
// [ [ n ], 'checkmultisig' ]
|
// [ [ n ], 'checkmultisig' ]
|
||||||
// );
|
// );
|
||||||
|
|
||||||
|
// Keys need to be in a predictable order.
|
||||||
keys = keys.sort(function(a, b) {
|
keys = keys.sort(function(a, b) {
|
||||||
return new bn(a).cmp(new bn(b)) > 0;
|
return new bn(a).cmp(new bn(b)) > 0;
|
||||||
});
|
});
|
||||||
|
|||||||
@ -69,7 +69,7 @@ function Wallet(options, passphrase) {
|
|||||||
|
|
||||||
this.multisig(options.multisig || {});
|
this.multisig(options.multisig || {});
|
||||||
|
|
||||||
this.prefix = 'bt/' + this.getAddress() + '/';
|
this.prefix = 'bt/' + this.getOwnAddress() + '/';
|
||||||
this.tx = new bcoin.txPool(this);
|
this.tx = new bcoin.txPool(this);
|
||||||
|
|
||||||
// Just a constants, actually
|
// Just a constants, actually
|
||||||
@ -114,7 +114,7 @@ Wallet.prototype._init = function init() {
|
|||||||
};
|
};
|
||||||
|
|
||||||
Wallet.prototype.multisig = function multisig(options) {
|
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.type = options.type || options.addressType;
|
||||||
options.keys = options.keys || options.sharedKeys;
|
options.keys = options.keys || options.sharedKeys;
|
||||||
@ -185,8 +185,8 @@ Wallet.prototype.getPrivateKey = function getPrivateKey(enc) {
|
|||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
Wallet.prototype.getPublicKey = function getPublicKey(enc) {
|
Wallet.prototype.getFullPublicKey = function getFullPublicKey(enc) {
|
||||||
var pub = this.key.getPublic(this.compressed, 'array');
|
var pub = this.getOwnPublicKey();
|
||||||
|
|
||||||
if (this.addressType === 'p2sh') {
|
if (this.addressType === 'p2sh') {
|
||||||
var keys = this.getPublicKeys();
|
var keys = this.getPublicKeys();
|
||||||
@ -201,7 +201,7 @@ Wallet.prototype.getPublicKey = function getPublicKey(enc) {
|
|||||||
return pub;
|
return pub;
|
||||||
};
|
};
|
||||||
|
|
||||||
Wallet.prototype._getPublicKey = function _getPublicKey(enc) {
|
Wallet.prototype.getOwnPublicKey = function getOwnPublicKey(enc) {
|
||||||
var pub = this.key.getPublic(this.compressed, 'array');
|
var pub = this.key.getPublic(this.compressed, 'array');
|
||||||
|
|
||||||
if (enc === 'base58')
|
if (enc === 'base58')
|
||||||
@ -212,13 +212,22 @@ Wallet.prototype._getPublicKey = function _getPublicKey(enc) {
|
|||||||
return pub;
|
return pub;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
Wallet.prototype.getPublicKey = function getPublicKey(enc) {
|
||||||
|
return this.getFullPublicKey(enc);
|
||||||
|
};
|
||||||
|
|
||||||
Wallet.prototype.getPublicKeys = function() {
|
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);
|
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.push(pub);
|
||||||
|
|
||||||
|
// Keys need to be in a predictable order.
|
||||||
keys = keys.sort(function(a, b) {
|
keys = keys.sort(function(a, b) {
|
||||||
return new bn(a).cmp(new bn(b)) > 0;
|
return new bn(a).cmp(new bn(b)) > 0;
|
||||||
});
|
});
|
||||||
@ -226,12 +235,28 @@ Wallet.prototype.getPublicKeys = function() {
|
|||||||
return keys;
|
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() {
|
Wallet.prototype.getHash = function getHash() {
|
||||||
return utils.ripesha(this.getPublicKey());
|
return utils.ripesha(this.getFullPublicKey());
|
||||||
};
|
};
|
||||||
|
|
||||||
Wallet.prototype.getAddress = function getAddress() {
|
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) {
|
Wallet.hash2addr = function hash2addr(hash, version) {
|
||||||
@ -271,8 +296,9 @@ Wallet.prototype.validateAddress = function validateAddress(addr, version) {
|
|||||||
Wallet.validateAddress = Wallet.prototype.validateAddress;
|
Wallet.validateAddress = Wallet.prototype.validateAddress;
|
||||||
|
|
||||||
Wallet.prototype.ownOutput = function ownOutput(tx, index) {
|
Wallet.prototype.ownOutput = function ownOutput(tx, index) {
|
||||||
var hash = this.getHash();
|
var scriptHash = this.getFullHash();
|
||||||
var key = this.getPublicKey();
|
var hash = this.getOwnHash();
|
||||||
|
var key = this.getOwnPublicKey();
|
||||||
|
|
||||||
var outputs = tx.outputs.filter(function(output, i) {
|
var outputs = tx.outputs.filter(function(output, i) {
|
||||||
if (index !== undefined && index !== i)
|
if (index !== undefined && index !== i)
|
||||||
@ -289,7 +315,7 @@ Wallet.prototype.ownOutput = function ownOutput(tx, index) {
|
|||||||
if (bcoin.script.isMultisig(s, key))
|
if (bcoin.script.isMultisig(s, key))
|
||||||
return true;
|
return true;
|
||||||
|
|
||||||
if (bcoin.script.isScripthash(s, hash))
|
if (bcoin.script.isScripthash(s, scriptHash))
|
||||||
return true;
|
return true;
|
||||||
|
|
||||||
return false;
|
return false;
|
||||||
@ -301,8 +327,9 @@ Wallet.prototype.ownOutput = function ownOutput(tx, index) {
|
|||||||
};
|
};
|
||||||
|
|
||||||
Wallet.prototype.ownInput = function ownInput(tx, index) {
|
Wallet.prototype.ownInput = function ownInput(tx, index) {
|
||||||
var hash = this.getHash();
|
var scriptHash = this.getFullHash();
|
||||||
var key = this.getPublicKey();
|
var hash = this.getOwnHash();
|
||||||
|
var key = this.getOwnPublicKey();
|
||||||
|
|
||||||
var inputs = tx.inputs.filter(function(input, i) {
|
var inputs = tx.inputs.filter(function(input, i) {
|
||||||
if (index !== undefined && index !== i)
|
if (index !== undefined && index !== i)
|
||||||
@ -322,7 +349,7 @@ Wallet.prototype.ownInput = function ownInput(tx, index) {
|
|||||||
if (bcoin.script.isMultisig(s, key))
|
if (bcoin.script.isMultisig(s, key))
|
||||||
return true;
|
return true;
|
||||||
|
|
||||||
if (bcoin.script.isScripthash(s, hash))
|
if (bcoin.script.isScripthash(s, scriptHash))
|
||||||
return true;
|
return true;
|
||||||
|
|
||||||
return false;
|
return false;
|
||||||
@ -337,7 +364,7 @@ Wallet.prototype.sign = function sign(tx, type, inputs) {
|
|||||||
if (!type)
|
if (!type)
|
||||||
type = 'all';
|
type = 'all';
|
||||||
|
|
||||||
var pub = this.getPublicKey();
|
var pub = this.getFullPublicKey();
|
||||||
var key = this.key;
|
var key = this.key;
|
||||||
|
|
||||||
inputs = inputs || tx.inputs;
|
inputs = inputs || tx.inputs;
|
||||||
@ -394,13 +421,15 @@ Wallet.prototype.toJSON = function toJSON() {
|
|||||||
return {
|
return {
|
||||||
v: 1,
|
v: 1,
|
||||||
type: 'wallet',
|
type: 'wallet',
|
||||||
pub: utils.toBase58(this.key.getPublic(this.compressed, 'array')),
|
pub: this.getOwnPublicKey('base58'),
|
||||||
priv: this.getPrivateKey('base58'),
|
priv: this.getPrivateKey('base58'),
|
||||||
tx: this.tx.toJSON(),
|
tx: this.tx.toJSON(),
|
||||||
addressType: this.addressType,
|
multisig: {
|
||||||
sharedKeys: utils.toBase58(this.sharedKeys),
|
type: this.addressType,
|
||||||
m: this.m,
|
keys: this.sharedKeys.map(utils.toBase58),
|
||||||
n: this.n
|
m: this.m,
|
||||||
|
n: this.n
|
||||||
|
}
|
||||||
};
|
};
|
||||||
};
|
};
|
||||||
|
|
||||||
@ -431,14 +460,14 @@ Wallet.fromJSON = function fromJSON(json) {
|
|||||||
compressed = pub[0] !== 0x04;
|
compressed = pub[0] !== 0x04;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if (json.multisig && json.multisig.keys)
|
||||||
|
json.multisig.keys = json.multisig.keys.map(utils.toKeyArray);
|
||||||
|
|
||||||
var w = new Wallet({
|
var w = new Wallet({
|
||||||
priv: priv,
|
priv: priv,
|
||||||
pub: pub,
|
pub: pub,
|
||||||
compressed: compressed,
|
compressed: compressed,
|
||||||
addressType: json.addressType,
|
multisig: json.multisig
|
||||||
sharedKeys: json.sharedKeys,
|
|
||||||
m: json.m,
|
|
||||||
n: json.n
|
|
||||||
});
|
});
|
||||||
|
|
||||||
w.tx.fromJSON(json.tx);
|
w.tx.fromJSON(json.tx);
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user