get address working.
This commit is contained in:
parent
6bf5554325
commit
86bc2227a4
347
lib/bcoin/address.js
Normal file
347
lib/bcoin/address.js
Normal file
@ -0,0 +1,347 @@
|
|||||||
|
/*!
|
||||||
|
* address.js - address object for bcoin
|
||||||
|
* Copyright (c) 2014-2015, Fedor Indutny (MIT License)
|
||||||
|
* Copyright (c) 2014-2016, Christopher Jeffrey (MIT License).
|
||||||
|
* https://github.com/indutny/bcoin
|
||||||
|
*/
|
||||||
|
|
||||||
|
var bcoin = require('./env');
|
||||||
|
var bn = require('bn.js');
|
||||||
|
var constants = bcoin.protocol.constants;
|
||||||
|
var networks = bcoin.protocol.network;
|
||||||
|
var utils = require('./utils');
|
||||||
|
var assert = utils.assert;
|
||||||
|
var BufferWriter = require('./writer');
|
||||||
|
var BufferReader = require('./reader');
|
||||||
|
var Script = bcoin.script;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Represents an address.
|
||||||
|
* @exports Address
|
||||||
|
* @constructor
|
||||||
|
* @param {Object} options
|
||||||
|
* @param {String?} options.hash
|
||||||
|
* @param {String?} options.type
|
||||||
|
* @param {String?} options.version
|
||||||
|
* @param {String?} options.network
|
||||||
|
*/
|
||||||
|
|
||||||
|
function Address(options) {
|
||||||
|
if (!(this instanceof Address))
|
||||||
|
return new Address(options);
|
||||||
|
|
||||||
|
this.hash = options.hash;
|
||||||
|
this.type = options.type || 'pubkeyhash';
|
||||||
|
this.version = options.version == null ? -1 : options.version;
|
||||||
|
this.network = bcoin.network.get(options.network).type;
|
||||||
|
|
||||||
|
if (!Buffer.isBuffer(this.hash))
|
||||||
|
this.hash = new Buffer(this.hash, 'hex');
|
||||||
|
}
|
||||||
|
|
||||||
|
Address.prototype.getHash = function getHash(enc) {
|
||||||
|
if (enc === 'hex')
|
||||||
|
return this.hash.toString(enc);
|
||||||
|
return this.hash;
|
||||||
|
};
|
||||||
|
|
||||||
|
Address.prototype.toBase58 = function toBase58(network) {
|
||||||
|
if (!network)
|
||||||
|
network = this.network;
|
||||||
|
|
||||||
|
return Address.toBase58(this.hash, this.type, this.version, network);
|
||||||
|
};
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Compile a hash to an address.
|
||||||
|
* @param {Hash|Buffer} hash
|
||||||
|
* @param {AddressType?} type
|
||||||
|
* @param {Number?} version - Witness version.
|
||||||
|
* @returns {Base58Address}
|
||||||
|
* @throws Error on bad hash/prefix.
|
||||||
|
*/
|
||||||
|
|
||||||
|
Address.toBase58 = function toBase58(hash, type, version, network) {
|
||||||
|
var p, prefix;
|
||||||
|
|
||||||
|
if (!Buffer.isBuffer(hash))
|
||||||
|
hash = new Buffer(hash, 'hex');
|
||||||
|
|
||||||
|
if (!type)
|
||||||
|
type = 'pubkeyhash';
|
||||||
|
|
||||||
|
network = bcoin.network.get(network);
|
||||||
|
|
||||||
|
prefix = network.address.prefixes[type];
|
||||||
|
|
||||||
|
if (!(version >= 0))
|
||||||
|
version = network.address.versions[type];
|
||||||
|
|
||||||
|
assert(prefix != null, 'Not a valid address prefix.');
|
||||||
|
|
||||||
|
if (!(version >= 0))
|
||||||
|
assert(hash.length === 20, 'Hash is the wrong size.');
|
||||||
|
else if (version === 0 && type === 'witnesspubkeyhash')
|
||||||
|
assert(hash.length === 20, 'Hash is the wrong size.');
|
||||||
|
else if (version === 0 && type === 'witnessscripthash')
|
||||||
|
assert(hash.length === 32, 'Hash is the wrong size.');
|
||||||
|
|
||||||
|
p = new BufferWriter();
|
||||||
|
|
||||||
|
p.writeU8(prefix);
|
||||||
|
if (version != null) {
|
||||||
|
p.writeU8(version);
|
||||||
|
p.writeU8(0)
|
||||||
|
}
|
||||||
|
p.writeBytes(hash);
|
||||||
|
p.writeChecksum();
|
||||||
|
|
||||||
|
return utils.toBase58(p.render());
|
||||||
|
};
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Parse a base58 address.
|
||||||
|
* @param {Base58Address} address
|
||||||
|
* @returns {ParsedAddress}
|
||||||
|
* @throws Parse error
|
||||||
|
*/
|
||||||
|
|
||||||
|
Address.parseBase58 = function parseBase58(address) {
|
||||||
|
var i, prefix, type, version, hash, network;
|
||||||
|
|
||||||
|
if (!Buffer.isBuffer(address))
|
||||||
|
address = utils.fromBase58(address);
|
||||||
|
|
||||||
|
p = new BufferReader(address, true);
|
||||||
|
prefix = p.readU8();
|
||||||
|
|
||||||
|
for (i = 0; i < networks.types.length; i++) {
|
||||||
|
network = networks[networks.types[i]];
|
||||||
|
type = network.address.prefixesByVal[prefix];
|
||||||
|
if (type != null)
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
|
||||||
|
assert(type != null, 'Unknown address prefix.');
|
||||||
|
|
||||||
|
version = network.address.versions[type];
|
||||||
|
|
||||||
|
if (version != null) {
|
||||||
|
version = p.readU8();
|
||||||
|
assert(version >= 0 && version <= 16, 'Bad program version.');
|
||||||
|
assert(p.readU8() === 0, 'Address version padding is non-zero.');
|
||||||
|
}
|
||||||
|
|
||||||
|
if (type === 'witnessscripthash')
|
||||||
|
hash = p.readBytes(32);
|
||||||
|
else
|
||||||
|
hash = p.readBytes(20);
|
||||||
|
|
||||||
|
p.verifyChecksum();
|
||||||
|
|
||||||
|
return {
|
||||||
|
network: network.type,
|
||||||
|
type: type,
|
||||||
|
hash: hash,
|
||||||
|
version: version == null ? -1 : version
|
||||||
|
};
|
||||||
|
};
|
||||||
|
|
||||||
|
Address.fromBase58 = function fromBase58(addr) {
|
||||||
|
return new Address(Address.parseBase58(addr));
|
||||||
|
};
|
||||||
|
|
||||||
|
Address.parseScript = function parseScript(script) {
|
||||||
|
var program;
|
||||||
|
|
||||||
|
if (script.isWitnessProgram()) {
|
||||||
|
program = script.getWitnessProgram();
|
||||||
|
if (!program.type || program.type === 'unknown')
|
||||||
|
return;
|
||||||
|
return {
|
||||||
|
hash: program.data,
|
||||||
|
type: program.type,
|
||||||
|
version: program.version
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
if (script.isPubkey()) {
|
||||||
|
hash = utils.ripesha(script.code[0]);
|
||||||
|
return { hash: hash, type: 'pubkeyhash', version: -1 };
|
||||||
|
}
|
||||||
|
|
||||||
|
if (script.isPubkeyhash()) {
|
||||||
|
hash = script.code[2];
|
||||||
|
return { hash: hash, type: 'pubkeyhash', version: -1 };
|
||||||
|
}
|
||||||
|
|
||||||
|
if (script.isMultisig()) {
|
||||||
|
hash = utils.ripesha(script.encode());
|
||||||
|
return { hash: hash, type: 'scripthash', version: -1 };
|
||||||
|
}
|
||||||
|
|
||||||
|
if (script.isScripthash()) {
|
||||||
|
hash = script.code[1];
|
||||||
|
return { hash: hash, type: 'scripthash', version: -1 };
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
Address.parseInput = function parseInput(code, witness) {
|
||||||
|
var hash;
|
||||||
|
|
||||||
|
if (Script.isPubkeyInput(code))
|
||||||
|
return;
|
||||||
|
|
||||||
|
if (Script.isPubkeyhashInput(code)) {
|
||||||
|
hash = utils.ripesha(code[1]);
|
||||||
|
if (witness)
|
||||||
|
return { hash: hash, type: 'witnesspubkeyhash', version: 0 };
|
||||||
|
return { hash: hash, type: 'pubkeyhash', version: -1 };
|
||||||
|
}
|
||||||
|
|
||||||
|
if (Script.isMultisigInput(code, witness))
|
||||||
|
return;
|
||||||
|
|
||||||
|
if (Script.isScripthashInput(code)) {
|
||||||
|
if (witness) {
|
||||||
|
hash = utils.sha256(code[code.length - 1]);
|
||||||
|
return { hash: hash, type: 'witnessscripthash', version: 0 };
|
||||||
|
}
|
||||||
|
hash = utils.ripesha(code[code.length - 1]);
|
||||||
|
return { hash: hash, type: 'scripthash', version: -1 };
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
Address.parseWitness = function parseWitness(witness) {
|
||||||
|
return Address.parseInput(witness.items, true);
|
||||||
|
};
|
||||||
|
|
||||||
|
Address.parseInputScript = function parseInputScript(script) {
|
||||||
|
return Address.parseInput(script.code, false);
|
||||||
|
};
|
||||||
|
|
||||||
|
Address.fromWitness = function fromWitness(witness) {
|
||||||
|
var data = Address.parseWitness(witness);
|
||||||
|
|
||||||
|
if (!data)
|
||||||
|
return;
|
||||||
|
|
||||||
|
return new Address(data);
|
||||||
|
};
|
||||||
|
|
||||||
|
Address.fromInputScript = function fromInputScript(script) {
|
||||||
|
var data = Address.parseInputScript(script);
|
||||||
|
|
||||||
|
if (!data)
|
||||||
|
return;
|
||||||
|
|
||||||
|
return new Address(data);
|
||||||
|
};
|
||||||
|
|
||||||
|
Address.fromScript = function fromScript(script) {
|
||||||
|
var data = Address.parseScript(script);
|
||||||
|
|
||||||
|
if (!data)
|
||||||
|
return;
|
||||||
|
|
||||||
|
return new Address(data);
|
||||||
|
};
|
||||||
|
|
||||||
|
Address.parseHash = function parseHash(hash, type, version) {
|
||||||
|
return {
|
||||||
|
hash: hash,
|
||||||
|
type: type || 'pubkeyhash',
|
||||||
|
version: version == null ? -1 : version
|
||||||
|
};
|
||||||
|
};
|
||||||
|
|
||||||
|
Address.fromHash = function fromHash(hash, type, version) {
|
||||||
|
return new Address(Address.parseHash(hash, type, version));
|
||||||
|
};
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Hash data and compile hash to an address.
|
||||||
|
* @param {Hash|Buffer} hash
|
||||||
|
* @param {AddressType?} type
|
||||||
|
* @param {Number?} version - Witness program version.
|
||||||
|
* @returns {Base58Address}
|
||||||
|
*/
|
||||||
|
|
||||||
|
Address.parseData = function parseData(data, type, version) {
|
||||||
|
if (type === 'witnessscripthash')
|
||||||
|
data = utils.sha256(data);
|
||||||
|
else
|
||||||
|
data = utils.ripesha(data);
|
||||||
|
return Address.parseHash(data, type, version);
|
||||||
|
};
|
||||||
|
|
||||||
|
Address.fromData = function fromData(data, type, version) {
|
||||||
|
return new Address(Address.parseData(data, type, version));
|
||||||
|
};
|
||||||
|
|
||||||
|
Address.toScript = function toScript() {
|
||||||
|
if (this.type === 'pubkeyhash')
|
||||||
|
return Script.createPubkeyhash(this.hash);
|
||||||
|
if (this.type === 'scripthash')
|
||||||
|
return Script.createScripthash(this.hash);
|
||||||
|
if (this.version !== -1)
|
||||||
|
return Script.createWitnessProgram(this.version, this.hash);
|
||||||
|
assert(false, 'Bad type.');
|
||||||
|
};
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Validate an address, optionally test against a type.
|
||||||
|
* @param {String} address - Can be of any type in reality.
|
||||||
|
* @param {AddressType}
|
||||||
|
* @returns {Boolean}
|
||||||
|
*/
|
||||||
|
|
||||||
|
Address.validate = function validate(address, type) {
|
||||||
|
if (!address)
|
||||||
|
return false;
|
||||||
|
|
||||||
|
if (!Buffer.isBuffer(address) && typeof address !== 'string')
|
||||||
|
return false;
|
||||||
|
|
||||||
|
try {
|
||||||
|
address = Address.parseBase58(address);
|
||||||
|
} catch (e) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (type && address.type !== type)
|
||||||
|
return false;
|
||||||
|
|
||||||
|
return true;
|
||||||
|
};
|
||||||
|
|
||||||
|
Address.getHash = function getHash(data) {
|
||||||
|
var hash;
|
||||||
|
|
||||||
|
if (data instanceof Address) {
|
||||||
|
hash = data.hash;
|
||||||
|
} else {
|
||||||
|
try {
|
||||||
|
hash = Address.parseBase58(data).hash;
|
||||||
|
} catch (e) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return hash.toString('hex');
|
||||||
|
};
|
||||||
|
|
||||||
|
// Address.prototype.toString = function toString() {
|
||||||
|
// return this.toBase58();
|
||||||
|
// };
|
||||||
|
|
||||||
|
Address.prototype.inspect = function inspect() {
|
||||||
|
return {
|
||||||
|
hash: this.getHash('hex'),
|
||||||
|
type: this.type,
|
||||||
|
version: this.version,
|
||||||
|
address: this.toBase58()
|
||||||
|
};
|
||||||
|
};
|
||||||
|
|
||||||
|
module.exports = Address;
|
||||||
@ -1110,7 +1110,7 @@ ChainDB.prototype.getCoinsByAddress = function getCoinsByAddress(addresses, call
|
|||||||
addresses = utils.uniq(addresses);
|
addresses = utils.uniq(addresses);
|
||||||
|
|
||||||
utils.forEachSerial(addresses, function(address, next) {
|
utils.forEachSerial(addresses, function(address, next) {
|
||||||
address = bcoin.script.Address.getHash(address);
|
address = bcoin.address.getHash(address);
|
||||||
if (!address)
|
if (!address)
|
||||||
return next();
|
return next();
|
||||||
self.db.lookup({
|
self.db.lookup({
|
||||||
@ -1153,7 +1153,7 @@ ChainDB.prototype.getTXByAddress = function getTXByAddress(addresses, callback)
|
|||||||
addresses = utils.uniq(addresses);
|
addresses = utils.uniq(addresses);
|
||||||
|
|
||||||
utils.forEachSerial(addresses, function(address, next) {
|
utils.forEachSerial(addresses, function(address, next) {
|
||||||
address = bcoin.script.Address.getHash(address);
|
address = bcoin.address.getHash(address);
|
||||||
if (!address)
|
if (!address)
|
||||||
return next();
|
return next();
|
||||||
self.db.lookup({
|
self.db.lookup({
|
||||||
|
|||||||
@ -148,7 +148,7 @@ Input.prototype.getSubtype = function getSubtype() {
|
|||||||
* Get the previous output script's address. Will "guess"
|
* Get the previous output script's address. Will "guess"
|
||||||
* based on the input script and/or witness if coin
|
* based on the input script and/or witness if coin
|
||||||
* is not available.
|
* is not available.
|
||||||
* @returns {ScriptAddress?} address
|
* @returns {Address?} address
|
||||||
*/
|
*/
|
||||||
|
|
||||||
Input.prototype.getAddress = function getAddress() {
|
Input.prototype.getAddress = function getAddress() {
|
||||||
|
|||||||
@ -1,5 +1,5 @@
|
|||||||
/*!
|
/*!
|
||||||
* address.js - address object for bcoin
|
* keyring.js - keyring object for bcoin
|
||||||
* Copyright (c) 2014-2015, Fedor Indutny (MIT License)
|
* Copyright (c) 2014-2015, Fedor Indutny (MIT License)
|
||||||
* Copyright (c) 2014-2016, Christopher Jeffrey (MIT License).
|
* Copyright (c) 2014-2016, Christopher Jeffrey (MIT License).
|
||||||
* https://github.com/indutny/bcoin
|
* https://github.com/indutny/bcoin
|
||||||
@ -183,10 +183,10 @@ KeyRing.prototype.getProgram = function getProgram() {
|
|||||||
|
|
||||||
if (!this._program) {
|
if (!this._program) {
|
||||||
if (this.type === 'pubkeyhash') {
|
if (this.type === 'pubkeyhash') {
|
||||||
hash = KeyRing.hash160(this.getPublicKey());
|
hash = utils.ripesha(this.getPublicKey());
|
||||||
program = bcoin.script.createWitnessProgram(0, hash);
|
program = bcoin.script.createWitnessProgram(0, hash);
|
||||||
} else if (this.type === 'multisig') {
|
} else if (this.type === 'multisig') {
|
||||||
hash = KeyRing.sha256(this.getScript().encode());
|
hash = utils.sha256(this.getScript().encode());
|
||||||
program = bcoin.script.createWitnessProgram(0, hash);
|
program = bcoin.script.createWitnessProgram(0, hash);
|
||||||
} else {
|
} else {
|
||||||
assert(false, 'Unknown address type.');
|
assert(false, 'Unknown address type.');
|
||||||
@ -209,7 +209,7 @@ KeyRing.prototype.getProgramHash = function getProgramHash(enc) {
|
|||||||
return;
|
return;
|
||||||
|
|
||||||
if (!this._programHash)
|
if (!this._programHash)
|
||||||
this._programHash = KeyRing.hash160(this.getProgram().encode());
|
this._programHash = utils.ripesha(this.getProgram().encode());
|
||||||
|
|
||||||
return enc === 'hex'
|
return enc === 'hex'
|
||||||
? this._programHash.toString('hex')
|
? this._programHash.toString('hex')
|
||||||
@ -229,7 +229,7 @@ KeyRing.prototype.getProgramAddress = function getProgramAddress() {
|
|||||||
|
|
||||||
if (!this._programAddress) {
|
if (!this._programAddress) {
|
||||||
hash = this.getProgramHash();
|
hash = this.getProgramHash();
|
||||||
address = this.compileHash(hash, 'scripthash');
|
address = this.compile(hash, 'scripthash');
|
||||||
this._programAddress = address;
|
this._programAddress = address;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -259,7 +259,7 @@ KeyRing.prototype.getScriptHash160 = function getScriptHash256(enc) {
|
|||||||
return;
|
return;
|
||||||
|
|
||||||
if (!this._scriptHash160)
|
if (!this._scriptHash160)
|
||||||
this._scriptHash160 = KeyRing.hash160(this.getScript().encode());
|
this._scriptHash160 = utils.ripesha(this.getScript().encode());
|
||||||
|
|
||||||
return enc === 'hex'
|
return enc === 'hex'
|
||||||
? this._scriptHash160.toString('hex')
|
? this._scriptHash160.toString('hex')
|
||||||
@ -277,7 +277,7 @@ KeyRing.prototype.getScriptHash256 = function getScriptHash256(enc) {
|
|||||||
return;
|
return;
|
||||||
|
|
||||||
if (!this._scriptHash256)
|
if (!this._scriptHash256)
|
||||||
this._scriptHash256 = KeyRing.sha256(this.getScript().encode());
|
this._scriptHash256 = utils.sha256(this.getScript().encode());
|
||||||
|
|
||||||
return enc === 'hex'
|
return enc === 'hex'
|
||||||
? this._scriptHash256.toString('hex')
|
? this._scriptHash256.toString('hex')
|
||||||
@ -298,10 +298,10 @@ KeyRing.prototype.getScriptAddress = function getScriptAddress() {
|
|||||||
if (!this._scriptAddress) {
|
if (!this._scriptAddress) {
|
||||||
if (this.witness) {
|
if (this.witness) {
|
||||||
hash = this.getScriptHash256();
|
hash = this.getScriptHash256();
|
||||||
address = this.compileHash(hash, 'witnessscripthash', 0);
|
address = this.compile(hash, 'witnessscripthash', 0);
|
||||||
} else {
|
} else {
|
||||||
hash = this.getScriptHash160();
|
hash = this.getScriptHash160();
|
||||||
address = this.compileHash(hash, 'scripthash');
|
address = this.compile(hash, 'scripthash');
|
||||||
}
|
}
|
||||||
this._scriptAddress = address;
|
this._scriptAddress = address;
|
||||||
}
|
}
|
||||||
@ -317,7 +317,7 @@ KeyRing.prototype.getScriptAddress = function getScriptAddress() {
|
|||||||
|
|
||||||
KeyRing.prototype.getKeyHash = function getKeyHash(enc) {
|
KeyRing.prototype.getKeyHash = function getKeyHash(enc) {
|
||||||
if (!this._hash)
|
if (!this._hash)
|
||||||
this._hash = KeyRing.hash160(this.getPublicKey());
|
this._hash = utils.ripesha(this.getPublicKey());
|
||||||
|
|
||||||
return enc === 'hex'
|
return enc === 'hex'
|
||||||
? this._hash.toString('hex')
|
? this._hash.toString('hex')
|
||||||
@ -335,15 +335,28 @@ KeyRing.prototype.getKeyAddress = function getKeyAddress() {
|
|||||||
if (!this._address) {
|
if (!this._address) {
|
||||||
hash = this.getKeyHash();
|
hash = this.getKeyHash();
|
||||||
if (this.witness)
|
if (this.witness)
|
||||||
address = this.compileHash(hash, 'witnesspubkeyhash', 0);
|
address = this.compile(hash, 'witnesspubkeyhash', 0);
|
||||||
else
|
else
|
||||||
address = this.compileHash(hash, 'pubkeyhash');
|
address = this.compile(hash, 'pubkeyhash');
|
||||||
this._address = address;
|
this._address = address;
|
||||||
}
|
}
|
||||||
|
|
||||||
return this._address;
|
return this._address;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Compile a hash to an address.
|
||||||
|
* @param {Hash|Buffer} hash
|
||||||
|
* @param {AddressType?} type
|
||||||
|
* @param {Number?} version - Witness version.
|
||||||
|
* @returns {Base58Address}
|
||||||
|
* @throws Error on bad hash/prefix.
|
||||||
|
*/
|
||||||
|
|
||||||
|
KeyRing.prototype.compile = function compile(hash, type, version) {
|
||||||
|
return bcoin.address.toBase58(hash, type, version, this.network);
|
||||||
|
};
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Get hash.
|
* Get hash.
|
||||||
* @param {String?} enc - `"hex"` or `null`.
|
* @param {String?} enc - `"hex"` or `null`.
|
||||||
@ -585,81 +598,6 @@ KeyRing.prototype.__defineGetter__('address', function() {
|
|||||||
return this.getAddress();
|
return this.getAddress();
|
||||||
});
|
});
|
||||||
|
|
||||||
/**
|
|
||||||
* Perform a hash160 (sha256 + ripemd160)
|
|
||||||
* @param {Buffer} key
|
|
||||||
* @returns {Buffer}
|
|
||||||
*/
|
|
||||||
|
|
||||||
KeyRing.hash160 = function hash160(key) {
|
|
||||||
return utils.ripesha(key);
|
|
||||||
};
|
|
||||||
|
|
||||||
/**
|
|
||||||
* @param {Buffer} key
|
|
||||||
* @returns {Buffer}
|
|
||||||
*/
|
|
||||||
|
|
||||||
KeyRing.sha256 = function sha256(key) {
|
|
||||||
return utils.sha256(key);
|
|
||||||
};
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Compile a hash to an address.
|
|
||||||
* @param {Hash|Buffer} hash
|
|
||||||
* @param {AddressType?} type
|
|
||||||
* @param {Number?} version - Witness version.
|
|
||||||
* @returns {Base58Address}
|
|
||||||
* @throws Error on bad hash/prefix.
|
|
||||||
*/
|
|
||||||
|
|
||||||
KeyRing.compileHash = function compileHash(hash, type, version, network) {
|
|
||||||
return bcoin.script.Address.toBase58(hash, type, version, network);
|
|
||||||
};
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Hash data and compile hash to an address.
|
|
||||||
* @param {Hash|Buffer} hash
|
|
||||||
* @param {AddressType?} type
|
|
||||||
* @param {Number?} version - Witness program version.
|
|
||||||
* @returns {Base58Address}
|
|
||||||
*/
|
|
||||||
|
|
||||||
KeyRing.compileData = function compileData(data, type, version, network) {
|
|
||||||
if (type === 'witnessscripthash')
|
|
||||||
data = KeyRing.sha256(data);
|
|
||||||
else
|
|
||||||
data = KeyRing.hash160(data);
|
|
||||||
|
|
||||||
return KeyRing.compileHash(data, type, version, network);
|
|
||||||
};
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Parse a base58 address.
|
|
||||||
* @param {Base58Address} address
|
|
||||||
* @returns {ParsedAddress}
|
|
||||||
* @throws Parse error
|
|
||||||
*/
|
|
||||||
|
|
||||||
KeyRing.parse = function parse(address) {
|
|
||||||
return bcoin.script.Address.parseBase58(address);
|
|
||||||
};
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Validate an address, optionally test against a type.
|
|
||||||
* @param {String} address - Can be of any type in reality.
|
|
||||||
* @param {AddressType}
|
|
||||||
* @returns {Boolean}
|
|
||||||
*/
|
|
||||||
|
|
||||||
KeyRing.validate = function validate(address, type) {
|
|
||||||
return bcoin.script.Address.validate(address, type);
|
|
||||||
};
|
|
||||||
|
|
||||||
KeyRing.prototype.compileHash = function compileHash(hash, type, version) {
|
|
||||||
return KeyRing.compileHash(hash, type, version, this.network);
|
|
||||||
};
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Convert an KeyRing to a more json-friendly object.
|
* Convert an KeyRing to a more json-friendly object.
|
||||||
* @param {String?} passphrase - KeyRing passphrase
|
* @param {String?} passphrase - KeyRing passphrase
|
||||||
|
|||||||
@ -70,7 +70,7 @@ Output.prototype.getType = function getType() {
|
|||||||
|
|
||||||
/**
|
/**
|
||||||
* Get the address.
|
* Get the address.
|
||||||
* @returns {ScriptAddress} address
|
* @returns {Address} address
|
||||||
*/
|
*/
|
||||||
|
|
||||||
Output.prototype.getAddress = function getAddress() {
|
Output.prototype.getAddress = function getAddress() {
|
||||||
|
|||||||
@ -1392,7 +1392,7 @@ Pool.prototype.removeWallet = function removeWallet(wallet) {
|
|||||||
*/
|
*/
|
||||||
|
|
||||||
Pool.prototype.watchAddress = function watchAddress(address) {
|
Pool.prototype.watchAddress = function watchAddress(address) {
|
||||||
var hash = bcoin.script.Address.getHash(address);
|
var hash = bcoin.address.getHash(address);
|
||||||
this.watch(hash);
|
this.watch(hash);
|
||||||
};
|
};
|
||||||
|
|
||||||
@ -1402,7 +1402,7 @@ Pool.prototype.watchAddress = function watchAddress(address) {
|
|||||||
*/
|
*/
|
||||||
|
|
||||||
Pool.prototype.unwatchAddress = function unwatchAddress(address) {
|
Pool.prototype.unwatchAddress = function unwatchAddress(address) {
|
||||||
var hash = bcoin.script.Address.getHash(address);
|
var hash = bcoin.address.getHash(address);
|
||||||
this.unwatch(hash);
|
this.unwatch(hash);
|
||||||
};
|
};
|
||||||
|
|
||||||
@ -1413,7 +1413,7 @@ Pool.prototype.unwatchAddress = function unwatchAddress(address) {
|
|||||||
|
|
||||||
Pool.prototype.watchWallet = function watchWallet(wallet) {
|
Pool.prototype.watchWallet = function watchWallet(wallet) {
|
||||||
Object.keys(wallet.addressMap).forEach(function(address) {
|
Object.keys(wallet.addressMap).forEach(function(address) {
|
||||||
this.watchAddress(address);
|
this.watch(new Buffer(address, 'hex'));
|
||||||
}, this);
|
}, this);
|
||||||
};
|
};
|
||||||
|
|
||||||
@ -1424,7 +1424,7 @@ Pool.prototype.watchWallet = function watchWallet(wallet) {
|
|||||||
|
|
||||||
Pool.prototype.unwatchWallet = function unwatchWallet(wallet) {
|
Pool.prototype.unwatchWallet = function unwatchWallet(wallet) {
|
||||||
Object.keys(wallet.addressMap).forEach(function(address) {
|
Object.keys(wallet.addressMap).forEach(function(address) {
|
||||||
this.unwatchAddress(address);
|
this.unwatch(new Buffer(address, 'hex'));
|
||||||
}, this);
|
}, this);
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|||||||
@ -8,7 +8,6 @@
|
|||||||
var bcoin = require('./env');
|
var bcoin = require('./env');
|
||||||
var bn = require('bn.js');
|
var bn = require('bn.js');
|
||||||
var constants = bcoin.protocol.constants;
|
var constants = bcoin.protocol.constants;
|
||||||
var networks = bcoin.protocol.network;
|
|
||||||
var utils = require('./utils');
|
var utils = require('./utils');
|
||||||
var assert = utils.assert;
|
var assert = utils.assert;
|
||||||
var BufferWriter = require('./writer');
|
var BufferWriter = require('./writer');
|
||||||
@ -118,7 +117,7 @@ Witness.prototype.getInputType = function getInputType() {
|
|||||||
*/
|
*/
|
||||||
|
|
||||||
Witness.prototype.getInputAddress = function getInputAddress(network) {
|
Witness.prototype.getInputAddress = function getInputAddress(network) {
|
||||||
return Address.fromWitness(this);
|
return bcoin.address.fromWitness(this);
|
||||||
};
|
};
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -2356,7 +2355,7 @@ Script.prototype.getSize = function getSize() {
|
|||||||
*/
|
*/
|
||||||
|
|
||||||
Script.prototype.getInputAddress = function getInputAddress() {
|
Script.prototype.getInputAddress = function getInputAddress() {
|
||||||
return Address.fromInputScript(this);
|
return bcoin.address.fromInputScript(this);
|
||||||
};
|
};
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -2367,7 +2366,7 @@ Script.prototype.getInputAddress = function getInputAddress() {
|
|||||||
*/
|
*/
|
||||||
|
|
||||||
Script.prototype.getAddress = function getAddress() {
|
Script.prototype.getAddress = function getAddress() {
|
||||||
return Address.fromScript(this);
|
return bcoin.address.fromScript(this);
|
||||||
};
|
};
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -2696,7 +2695,8 @@ Script.createOutputScript = function createOutputScript(options) {
|
|||||||
options = {};
|
options = {};
|
||||||
|
|
||||||
if (options.address) {
|
if (options.address) {
|
||||||
address = bcoin.script.Address.parseBase58(options.address);
|
if (typeof options.address === 'string')
|
||||||
|
address = bcoin.address.parseBase58(options.address);
|
||||||
|
|
||||||
if (address.type === 'pubkeyhash')
|
if (address.type === 'pubkeyhash')
|
||||||
script = Script.createPubkeyhash(address.hash);
|
script = Script.createPubkeyhash(address.hash);
|
||||||
@ -4214,290 +4214,3 @@ Script.Witness = Witness;
|
|||||||
Script.Stack = Stack;
|
Script.Stack = Stack;
|
||||||
|
|
||||||
module.exports = Script;
|
module.exports = Script;
|
||||||
|
|
||||||
function Address(options) {
|
|
||||||
if (!(this instanceof Address))
|
|
||||||
return new Address(options);
|
|
||||||
|
|
||||||
this.hash = options.hash;
|
|
||||||
this.type = options.type || 'pubkeyhash';
|
|
||||||
this.version = options.version == null ? -1 : options.version;
|
|
||||||
this.network = options.network;
|
|
||||||
|
|
||||||
if (!Buffer.isBuffer(this.hash))
|
|
||||||
this.hash = new Buffer(this.hash, 'hex');
|
|
||||||
}
|
|
||||||
|
|
||||||
Address.prototype.getHash = function getHash(enc) {
|
|
||||||
if (enc === 'hex')
|
|
||||||
return this.hash.toString(enc);
|
|
||||||
return this.hash;
|
|
||||||
};
|
|
||||||
|
|
||||||
Address.prototype.toBase58 = function toBase58(network) {
|
|
||||||
if (!network)
|
|
||||||
network = this.network;
|
|
||||||
|
|
||||||
return Address.toBase58(this.hash, this.type, this.version, network);
|
|
||||||
};
|
|
||||||
|
|
||||||
Address.toBase58 = function toBase58(hash, type, version, network) {
|
|
||||||
var p, prefix;
|
|
||||||
|
|
||||||
if (!Buffer.isBuffer(hash))
|
|
||||||
hash = new Buffer(hash, 'hex');
|
|
||||||
|
|
||||||
if (!type)
|
|
||||||
type = 'pubkeyhash';
|
|
||||||
|
|
||||||
network = bcoin.network.get(network);
|
|
||||||
|
|
||||||
prefix = network.address.prefixes[type];
|
|
||||||
|
|
||||||
if (!(version >= 0))
|
|
||||||
version = network.address.versions[type];
|
|
||||||
|
|
||||||
assert(prefix != null, 'Not a valid address prefix.');
|
|
||||||
|
|
||||||
if (!(version >= 0))
|
|
||||||
assert(hash.length === 20, 'Hash is the wrong size.');
|
|
||||||
else if (version === 0 && type === 'witnesspubkeyhash')
|
|
||||||
assert(hash.length === 20, 'Hash is the wrong size.');
|
|
||||||
else if (version === 0 && type === 'witnessscripthash')
|
|
||||||
assert(hash.length === 32, 'Hash is the wrong size.');
|
|
||||||
|
|
||||||
p = new BufferWriter();
|
|
||||||
|
|
||||||
p.writeU8(prefix);
|
|
||||||
if (version != null) {
|
|
||||||
p.writeU8(version);
|
|
||||||
p.writeU8(0)
|
|
||||||
}
|
|
||||||
p.writeBytes(hash);
|
|
||||||
p.writeChecksum();
|
|
||||||
|
|
||||||
return utils.toBase58(p.render());
|
|
||||||
};
|
|
||||||
|
|
||||||
Address.parseBase58 = function parseBase58(address) {
|
|
||||||
var i, prefix, type, version, hash, network;
|
|
||||||
|
|
||||||
if (!Buffer.isBuffer(address))
|
|
||||||
address = utils.fromBase58(address);
|
|
||||||
|
|
||||||
p = new BufferReader(address, true);
|
|
||||||
prefix = p.readU8();
|
|
||||||
|
|
||||||
for (i = 0; i < networks.types.length; i++) {
|
|
||||||
network = networks[networks.types[i]];
|
|
||||||
type = network.address.prefixesByVal[prefix];
|
|
||||||
if (type != null)
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
|
|
||||||
assert(type != null, 'Unknown address prefix.');
|
|
||||||
|
|
||||||
version = network.address.versions[type];
|
|
||||||
|
|
||||||
if (version != null) {
|
|
||||||
version = p.readU8();
|
|
||||||
assert(version >= 0 && version <= 16, 'Bad program version.');
|
|
||||||
assert(p.readU8() === 0, 'Address version padding is non-zero.');
|
|
||||||
}
|
|
||||||
|
|
||||||
if (type === 'witnessscripthash')
|
|
||||||
hash = p.readBytes(32);
|
|
||||||
else
|
|
||||||
hash = p.readBytes(20);
|
|
||||||
|
|
||||||
p.verifyChecksum();
|
|
||||||
|
|
||||||
return {
|
|
||||||
network: network.type,
|
|
||||||
type: type,
|
|
||||||
hash: hash,
|
|
||||||
version: version == null ? -1 : version
|
|
||||||
};
|
|
||||||
};
|
|
||||||
|
|
||||||
Address.fromBase58 = function fromBase58(addr) {
|
|
||||||
return new Address(Address.parseBase58(addr));
|
|
||||||
};
|
|
||||||
|
|
||||||
Address.parseScript = function parseScript(script) {
|
|
||||||
var program;
|
|
||||||
|
|
||||||
if (script.isWitnessProgram()) {
|
|
||||||
program = script.getWitnessProgram();
|
|
||||||
if (!program.type || program.type === 'unknown')
|
|
||||||
return;
|
|
||||||
return {
|
|
||||||
hash: program.data,
|
|
||||||
type: program.type,
|
|
||||||
version: program.version
|
|
||||||
};
|
|
||||||
}
|
|
||||||
|
|
||||||
if (script.isPubkey()) {
|
|
||||||
hash = utils.ripesha(script.code[0]);
|
|
||||||
return { hash: hash, type: 'pubkeyhash', version: -1 };
|
|
||||||
}
|
|
||||||
|
|
||||||
if (script.isPubkeyhash()) {
|
|
||||||
hash = script.code[2];
|
|
||||||
return { hash: hash, type: 'pubkeyhash', version: -1 };
|
|
||||||
}
|
|
||||||
|
|
||||||
if (script.isMultisig()) {
|
|
||||||
hash = utils.ripesha(script.encode());
|
|
||||||
return { hash: hash, type: 'scripthash', version: -1 };
|
|
||||||
}
|
|
||||||
|
|
||||||
if (script.isScripthash()) {
|
|
||||||
hash = script.code[1];
|
|
||||||
return { hash: hash, type: 'scripthash', version: -1 };
|
|
||||||
}
|
|
||||||
};
|
|
||||||
|
|
||||||
Address.parseInput = function parseInput(code, witness) {
|
|
||||||
var hash;
|
|
||||||
|
|
||||||
if (Script.isPubkeyInput(code))
|
|
||||||
return;
|
|
||||||
|
|
||||||
if (Script.isPubkeyhashInput(code)) {
|
|
||||||
hash = utils.ripesha(code[1]);
|
|
||||||
if (witness)
|
|
||||||
return { hash: hash, type: 'witnesspubkeyhash', version: 0 };
|
|
||||||
return { hash: hash, type: 'pubkeyhash', version: -1 };
|
|
||||||
}
|
|
||||||
|
|
||||||
if (Script.isMultisigInput(code, witness))
|
|
||||||
return;
|
|
||||||
|
|
||||||
if (Script.isScripthashInput(code)) {
|
|
||||||
if (witness) {
|
|
||||||
hash = utils.sha256(code[code.length - 1]);
|
|
||||||
return { hash: hash, type: 'witnessscripthash', version: 0 };
|
|
||||||
}
|
|
||||||
hash = utils.ripesha(code[code.length - 1]);
|
|
||||||
return { hash: hash, type: 'scripthash', version: -1 };
|
|
||||||
}
|
|
||||||
};
|
|
||||||
|
|
||||||
Address.parseWitness = function parseWitness(witness) {
|
|
||||||
return Address.parseInput(witness.items, true);
|
|
||||||
};
|
|
||||||
|
|
||||||
Address.parseInputScript = function parseInputScript(script) {
|
|
||||||
return Address.parseInput(script.code, false);
|
|
||||||
};
|
|
||||||
|
|
||||||
Address.fromWitness = function fromWitness(witness) {
|
|
||||||
var data = Address.parseWitness(witness);
|
|
||||||
|
|
||||||
if (!data)
|
|
||||||
return;
|
|
||||||
|
|
||||||
return new Address(data);
|
|
||||||
};
|
|
||||||
|
|
||||||
Address.fromInputScript = function fromInputScript(script) {
|
|
||||||
var data = Address.parseInputScript(script);
|
|
||||||
|
|
||||||
if (!data)
|
|
||||||
return;
|
|
||||||
|
|
||||||
return new Address(data);
|
|
||||||
};
|
|
||||||
|
|
||||||
Address.fromScript = function fromScript(script) {
|
|
||||||
var data = Address.parseScript(script);
|
|
||||||
|
|
||||||
if (!data)
|
|
||||||
return;
|
|
||||||
|
|
||||||
return new Address(data);
|
|
||||||
};
|
|
||||||
|
|
||||||
Address.parseHash = function parseHash(hash, type, version) {
|
|
||||||
return {
|
|
||||||
hash: hash,
|
|
||||||
type: type || 'pubkeyhash',
|
|
||||||
version: version == null ? -1 : version
|
|
||||||
};
|
|
||||||
};
|
|
||||||
|
|
||||||
Address.fromHash = function fromHash(hash, type, version) {
|
|
||||||
return new Address(Address.parseHash(hash, type, version));
|
|
||||||
};
|
|
||||||
|
|
||||||
Address.parseData = function parseData(data, type, version) {
|
|
||||||
if (type === 'witnessscripthash')
|
|
||||||
data = utils.sha256(data);
|
|
||||||
else
|
|
||||||
data = utils.ripesha(data);
|
|
||||||
return Address.parseHash(data, type, version);
|
|
||||||
};
|
|
||||||
|
|
||||||
Address.fromData = function fromData(data, type, version) {
|
|
||||||
return new Address(Address.parseData(data, type, version));
|
|
||||||
};
|
|
||||||
|
|
||||||
Address.toScript = function toScript() {
|
|
||||||
if (this.type === 'pubkeyhash')
|
|
||||||
return Script.createPubkeyhash(this.hash);
|
|
||||||
if (this.type === 'scripthash')
|
|
||||||
return Script.createScripthash(this.hash);
|
|
||||||
assert(false, 'Bad type.');
|
|
||||||
};
|
|
||||||
|
|
||||||
Address.validate = function validate(address, type) {
|
|
||||||
if (!address)
|
|
||||||
return false;
|
|
||||||
|
|
||||||
if (!Buffer.isBuffer(address) && typeof address !== 'string')
|
|
||||||
return false;
|
|
||||||
|
|
||||||
try {
|
|
||||||
address = Address.parseBase58(address);
|
|
||||||
} catch (e) {
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (type && address.type !== type)
|
|
||||||
return false;
|
|
||||||
|
|
||||||
return true;
|
|
||||||
};
|
|
||||||
|
|
||||||
Address.getHash = function getHash(data) {
|
|
||||||
var hash;
|
|
||||||
|
|
||||||
if (data instanceof Address) {
|
|
||||||
hash = data.hash;
|
|
||||||
} else {
|
|
||||||
try {
|
|
||||||
hash = Address.parseBase58(data).hash;
|
|
||||||
} catch (e) {
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
return hash.toString('hex');
|
|
||||||
};
|
|
||||||
|
|
||||||
// Address.prototype.toString = function toString() {
|
|
||||||
// return this.toBase58();
|
|
||||||
// };
|
|
||||||
|
|
||||||
Address.prototype.inspect = function inspect() {
|
|
||||||
return {
|
|
||||||
hash: this.getHash('hex'),
|
|
||||||
type: this.type,
|
|
||||||
version: this.version,
|
|
||||||
address: this.toBase58()
|
|
||||||
};
|
|
||||||
};
|
|
||||||
|
|
||||||
Script.Address = Address;
|
|
||||||
|
|||||||
@ -61,9 +61,9 @@ describe('Wallet', function() {
|
|||||||
assert.ifError(err);
|
assert.ifError(err);
|
||||||
|
|
||||||
if (witness)
|
if (witness)
|
||||||
assert(bcoin.address.parse(w.getAddress()).type === 'witnesspubkeyhash');
|
assert(bcoin.address.parseBase58(w.getAddress()).type === 'witnesspubkeyhash');
|
||||||
else
|
else
|
||||||
assert(bcoin.address.parse(w.getAddress()).type === 'pubkeyhash');
|
assert(bcoin.address.parseBase58(w.getAddress()).type === 'pubkeyhash');
|
||||||
|
|
||||||
// Input transcation
|
// Input transcation
|
||||||
var src = bcoin.mtx({
|
var src = bcoin.mtx({
|
||||||
@ -74,7 +74,7 @@ describe('Wallet', function() {
|
|||||||
: w.getAddress()
|
: w.getAddress()
|
||||||
}, {
|
}, {
|
||||||
value: 5460 * 2,
|
value: 5460 * 2,
|
||||||
address: bcoin.address.compileData(new Buffer([]))
|
address: bcoin.address.fromData(new Buffer([])).toBase58()
|
||||||
}]
|
}]
|
||||||
});
|
});
|
||||||
|
|
||||||
@ -124,7 +124,7 @@ describe('Wallet', function() {
|
|||||||
keys: [ w.getPublicKey(), k2.derive('m/0/0').publicKey ]
|
keys: [ w.getPublicKey(), k2.derive('m/0/0').publicKey ]
|
||||||
}, {
|
}, {
|
||||||
value: 5460 * 2,
|
value: 5460 * 2,
|
||||||
address: bcoin.address.compileData(new Buffer([]))
|
address: bcoin.address.fromData(new Buffer([])).toBase58()
|
||||||
}]
|
}]
|
||||||
});
|
});
|
||||||
src.addInput(dummyInput);
|
src.addInput(dummyInput);
|
||||||
@ -432,9 +432,9 @@ describe('Wallet', function() {
|
|||||||
var addr = w1.getAddress();
|
var addr = w1.getAddress();
|
||||||
|
|
||||||
if (witness)
|
if (witness)
|
||||||
assert(bcoin.address.parse(addr).type === 'witnessscripthash');
|
assert(bcoin.address.parseBase58(addr).type === 'witnessscripthash');
|
||||||
else
|
else
|
||||||
assert(bcoin.address.parse(addr).type === 'scripthash');
|
assert(bcoin.address.parseBase58(addr).type === 'scripthash');
|
||||||
|
|
||||||
assert.equal(w1.getAddress(), addr);
|
assert.equal(w1.getAddress(), addr);
|
||||||
assert.equal(w2.getAddress(), addr);
|
assert.equal(w2.getAddress(), addr);
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user