wallet key format.
This commit is contained in:
parent
f5a63563e3
commit
d2205241ee
@ -225,25 +225,55 @@ Wallet.prototype.getPrivateKey = function getPrivateKey(enc) {
|
|||||||
if (!enc)
|
if (!enc)
|
||||||
return priv;
|
return priv;
|
||||||
|
|
||||||
if (enc === 'base58') {
|
if (enc === 'base58')
|
||||||
// We'll be using ncompressed public key as an address
|
return Wallet.toSecret(priv, this.compressed);
|
||||||
arr = [ network.prefixes.privkey ];
|
else if (enc === 'hex')
|
||||||
|
return utils.toHex(priv);
|
||||||
|
else
|
||||||
|
return priv;
|
||||||
|
};
|
||||||
|
|
||||||
// 0-pad key
|
Wallet.toSecret = function toSecret(priv, compressed) {
|
||||||
while (arr.length + priv.length < 33)
|
var arr, chk;
|
||||||
arr.push(0);
|
|
||||||
|
|
||||||
arr = arr.concat(priv);
|
// We'll be using ncompressed public key as an address
|
||||||
|
arr = [network.prefixes.privkey];
|
||||||
|
|
||||||
if (this.compressed)
|
// 0-pad key
|
||||||
arr.push(1);
|
while (arr.length + priv.length < 33)
|
||||||
|
arr.push(0);
|
||||||
|
|
||||||
chk = utils.checksum(arr);
|
arr = arr.concat(priv);
|
||||||
|
|
||||||
return utils.toBase58(arr.concat(chk));
|
if (compressed)
|
||||||
|
arr.push(1);
|
||||||
|
|
||||||
|
chk = utils.checksum(arr);
|
||||||
|
|
||||||
|
return utils.toBase58(arr.concat(chk));
|
||||||
|
};
|
||||||
|
|
||||||
|
Wallet.fromSecret = function fromSecret(priv) {
|
||||||
|
var key, compressed;
|
||||||
|
|
||||||
|
key = bcoin.utils.fromBase58(priv);
|
||||||
|
assert(utils.isEqual(key.slice(-4), utils.checksum(key.slice(0, -4))));
|
||||||
|
assert.equal(key[0], network.prefixes.privkey);
|
||||||
|
|
||||||
|
key = key.slice(0, -4);
|
||||||
|
if (key.length === 34) {
|
||||||
|
assert.equal(key[33], 1);
|
||||||
|
priv = key.slice(1, -1);
|
||||||
|
compressed = true;
|
||||||
|
} else {
|
||||||
|
priv = key.slice(1);
|
||||||
|
compressed = false;
|
||||||
}
|
}
|
||||||
|
|
||||||
return priv;
|
return {
|
||||||
|
priv: priv,
|
||||||
|
compressed: compressed
|
||||||
|
};
|
||||||
};
|
};
|
||||||
|
|
||||||
Wallet.prototype.getScript = function getScript() {
|
Wallet.prototype.getScript = function getScript() {
|
||||||
@ -630,7 +660,7 @@ Wallet.prototype.toJSON = function toJSON(encrypt) {
|
|||||||
label: this.label,
|
label: this.label,
|
||||||
address: this.getAddress(),
|
address: this.getAddress(),
|
||||||
balance: utils.toBTC(this.balance()),
|
balance: utils.toBTC(this.balance()),
|
||||||
pub: this.getPublicKey('base58'),
|
pub: this.getPublicKey('hex'),
|
||||||
priv: encrypt
|
priv: encrypt
|
||||||
? encrypt(this.getPrivateKey('base58'))
|
? encrypt(this.getPrivateKey('base58'))
|
||||||
: this.getPrivateKey('base58'),
|
: this.getPrivateKey('base58'),
|
||||||
@ -639,6 +669,7 @@ Wallet.prototype.toJSON = function toJSON(encrypt) {
|
|||||||
: null,
|
: null,
|
||||||
type: this.type,
|
type: this.type,
|
||||||
subtype: this.subtype,
|
subtype: this.subtype,
|
||||||
|
redeem: this.redeem ? utils.toHex(this.redeem) : null,
|
||||||
keys: this.keys.map(utils.toBase58),
|
keys: this.keys.map(utils.toBase58),
|
||||||
m: this.m,
|
m: this.m,
|
||||||
n: this.n,
|
n: this.n,
|
||||||
@ -663,21 +694,11 @@ Wallet.fromJSON = function fromJSON(json, decrypt) {
|
|||||||
if (json.encrypted)
|
if (json.encrypted)
|
||||||
priv = decrypt(priv);
|
priv = decrypt(priv);
|
||||||
|
|
||||||
key = bcoin.utils.fromBase58(priv);
|
key = Wallet.fromSecret(json.priv);
|
||||||
assert(utils.isEqual(key.slice(-4), utils.checksum(key.slice(0, -4))));
|
priv = key.priv;
|
||||||
assert.equal(key[0], network.prefixes.privkey);
|
compressed = key.compressed;
|
||||||
|
|
||||||
key = key.slice(0, -4);
|
|
||||||
if (key.length === 34) {
|
|
||||||
assert.equal(key[33], 1);
|
|
||||||
priv = key.slice(1, -1);
|
|
||||||
compressed = true;
|
|
||||||
} else {
|
|
||||||
priv = key.slice(1);
|
|
||||||
compressed = false;
|
|
||||||
}
|
|
||||||
} else {
|
} else {
|
||||||
pub = bcoin.utils.fromBase58(json.pub);
|
pub = bcoin.utils.toArray(json.pub, 'hex');
|
||||||
compressed = pub[0] !== 0x04;
|
compressed = pub[0] !== 0x04;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -696,6 +717,7 @@ Wallet.fromJSON = function fromJSON(json, decrypt) {
|
|||||||
multisig: multisig,
|
multisig: multisig,
|
||||||
type: json.type,
|
type: json.type,
|
||||||
subtype: json.subtype,
|
subtype: json.subtype,
|
||||||
|
redeem: json.redeem ? utils.toArray(json.redeem, 'hex') : null,
|
||||||
keys: json.keys.map(utils.fromBase58),
|
keys: json.keys.map(utils.fromBase58),
|
||||||
m: json.m,
|
m: json.m,
|
||||||
n: json.n
|
n: json.n
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user