refactor.
This commit is contained in:
parent
3fd3d9bcff
commit
7422b46e75
@ -95,7 +95,7 @@ AbstractBlock.prototype.hash = function hash(enc) {
|
||||
var hash = this._hash;
|
||||
|
||||
if (!hash) {
|
||||
hash = utils.dsha256(this.abbr());
|
||||
hash = utils.hash256(this.abbr());
|
||||
if (!this.mutable)
|
||||
this._hash = hash;
|
||||
}
|
||||
|
||||
@ -50,7 +50,7 @@ Address.prototype.fromOptions = function fromOptions(options) {
|
||||
this.version = options.version == null ? -1 : options.version;
|
||||
this.network = bcoin.network.get(options.network).type;
|
||||
|
||||
if (!Buffer.isBuffer(this.hash))
|
||||
if (typeof this.hash === 'string')
|
||||
this.hash = new Buffer(this.hash, 'hex');
|
||||
};
|
||||
|
||||
@ -90,13 +90,7 @@ Address.prototype.toBase58 = function toBase58(network) {
|
||||
*/
|
||||
|
||||
Address.prototype.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.');
|
||||
return Script.fromAddress(this);
|
||||
};
|
||||
|
||||
/**
|
||||
@ -133,7 +127,7 @@ Address.prototype.inspect = function inspect() {
|
||||
Address.toBase58 = function toBase58(hash, type, version, network) {
|
||||
var p, prefix;
|
||||
|
||||
if (!Buffer.isBuffer(hash))
|
||||
if (typeof hash === 'string')
|
||||
hash = new Buffer(hash, 'hex');
|
||||
|
||||
if (!type)
|
||||
@ -178,7 +172,7 @@ Address.toBase58 = function toBase58(hash, type, version, network) {
|
||||
Address.prototype.fromBase58 = function fromBase58(address) {
|
||||
var i, prefix, type, version, hash, network, p;
|
||||
|
||||
if (!Buffer.isBuffer(address))
|
||||
if (typeof address === 'string')
|
||||
address = utils.fromBase58(address);
|
||||
|
||||
p = new BufferReader(address, true);
|
||||
@ -238,7 +232,7 @@ Address.fromBase58 = function fromBase58(address) {
|
||||
Address.prototype.fromScript = function fromScript(script) {
|
||||
var program;
|
||||
|
||||
if (script.isWitnessProgram()) {
|
||||
if (script.isProgram()) {
|
||||
program = script.toProgram();
|
||||
if (program.isUnknown())
|
||||
return;
|
||||
@ -250,7 +244,7 @@ Address.prototype.fromScript = function fromScript(script) {
|
||||
|
||||
// Fast case
|
||||
if (script.isPubkey()) {
|
||||
this.hash = utils.ripesha(script.raw.slice(1, script.raw[0] + 1));
|
||||
this.hash = utils.hash160(script.raw.slice(1, script.raw[0] + 1));
|
||||
this.type = 'pubkeyhash';
|
||||
this.version = -1;
|
||||
return this;
|
||||
@ -272,7 +266,7 @@ Address.prototype.fromScript = function fromScript(script) {
|
||||
|
||||
// Slow case (allow non-minimal data and parse script)
|
||||
if (script.isPubkey(true)) {
|
||||
this.hash = utils.ripesha(script.code[0].data);
|
||||
this.hash = utils.hash160(script.code[0].data);
|
||||
this.type = 'pubkeyhash';
|
||||
this.version = -1;
|
||||
return this;
|
||||
@ -286,7 +280,7 @@ Address.prototype.fromScript = function fromScript(script) {
|
||||
}
|
||||
|
||||
if (script.isMultisig()) {
|
||||
this.hash = utils.ripesha(script.raw);
|
||||
this.hash = utils.hash160(script.raw);
|
||||
this.type = 'scripthash';
|
||||
this.version = -1;
|
||||
return this;
|
||||
@ -302,7 +296,7 @@ Address.prototype.fromScript = function fromScript(script) {
|
||||
|
||||
Address.prototype.fromWitness = function fromWitness(witness) {
|
||||
if (witness.isPubkeyhashInput()) {
|
||||
this.hash = utils.ripesha(witness.items[1]);
|
||||
this.hash = utils.hash160(witness.items[1]);
|
||||
this.type = 'witnesspubkeyhash';
|
||||
this.version = 0;
|
||||
return this;
|
||||
@ -325,14 +319,14 @@ Address.prototype.fromWitness = function fromWitness(witness) {
|
||||
|
||||
Address.prototype.fromInputScript = function fromInputScript(script) {
|
||||
if (script.isPubkeyhashInput()) {
|
||||
this.hash = utils.ripesha(script.code[1].data);
|
||||
this.hash = utils.hash160(script.code[1].data);
|
||||
this.type = 'pubkeyhash';
|
||||
this.version = -1;
|
||||
return this;
|
||||
}
|
||||
|
||||
if (script.isScripthashInput()) {
|
||||
this.hash = utils.ripesha(script.code[script.code.length - 1].data);
|
||||
this.hash = utils.hash160(script.code[script.code.length - 1].data);
|
||||
this.type = 'scripthash';
|
||||
this.version = -1;
|
||||
return this;
|
||||
@ -413,7 +407,7 @@ Address.prototype.fromData = function fromData(data, type, version, network) {
|
||||
if (type === 'witnessscripthash')
|
||||
data = utils.sha256(data);
|
||||
else
|
||||
data = utils.ripesha(data);
|
||||
data = utils.hash160(data);
|
||||
|
||||
return this.fromHash(data, type, version, network);
|
||||
};
|
||||
|
||||
@ -341,7 +341,7 @@ Block.prototype.getCommitmentHash = function getCommitmentHash(enc) {
|
||||
if (!witnessRoot)
|
||||
return;
|
||||
|
||||
commitmentHash = utils.dsha256(Buffer.concat([witnessRoot, witnessNonce]));
|
||||
commitmentHash = utils.hash256(Buffer.concat([witnessRoot, witnessNonce]));
|
||||
|
||||
return enc === 'hex'
|
||||
? commitmentHash.toString('hex')
|
||||
|
||||
@ -303,7 +303,7 @@ Chain.prototype._preload = function _preload(callback) {
|
||||
|
||||
function parseHeader(data) {
|
||||
var p = bcoin.reader(data, true);
|
||||
var hash = utils.dsha256(p.readBytes(80)).toString('hex');
|
||||
var hash = utils.hash256(p.readBytes(80)).toString('hex');
|
||||
p.seek(-80);
|
||||
return {
|
||||
hash: hash,
|
||||
|
||||
@ -432,7 +432,7 @@ ChainEntry.prototype.toRaw = function toRaw(writer) {
|
||||
|
||||
ChainEntry.fromRaw = function fromRaw(chain, buf) {
|
||||
var p = new BufferReader(buf, true);
|
||||
var hash = utils.dsha256(p.readBytes(80));
|
||||
var hash = utils.hash256(p.readBytes(80));
|
||||
|
||||
p.seek(-80);
|
||||
|
||||
|
||||
@ -366,9 +366,9 @@ DeferredCoin.prototype.toCoin = function toCoin(coins, index) {
|
||||
if (prefix === 0)
|
||||
script = bcoin.script.fromRaw(p.readVarBytes());
|
||||
else if (prefix === 1)
|
||||
script = bcoin.script.createPubkeyhash(p.readBytes(20));
|
||||
script = bcoin.script.fromPubkeyhash(p.readBytes(20));
|
||||
else if (prefix === 2)
|
||||
script = bcoin.script.createScripthash(p.readBytes(20));
|
||||
script = bcoin.script.fromScripthash(p.readBytes(20));
|
||||
else
|
||||
assert(false, 'Bad prefix.');
|
||||
|
||||
|
||||
@ -571,7 +571,7 @@ HDPrivateKey.prototype.derive = function derive(index, hardened) {
|
||||
.toArrayLike(Buffer, 'be', 32);
|
||||
|
||||
if (!this.fingerPrint)
|
||||
this.fingerPrint = utils.ripesha(this.publicKey).slice(0, 4);
|
||||
this.fingerPrint = utils.hash160(this.publicKey).slice(0, 4);
|
||||
|
||||
child = new HDPrivateKey({
|
||||
network: this.network,
|
||||
@ -1222,7 +1222,7 @@ HDPublicKey.prototype.derive = function derive(index, hardened) {
|
||||
assert(publicKey.length === 33);
|
||||
|
||||
if (!this.fingerPrint)
|
||||
this.fingerPrint = utils.ripesha(this.publicKey).slice(0, 4);
|
||||
this.fingerPrint = utils.hash160(this.publicKey).slice(0, 4);
|
||||
|
||||
child = new HDPublicKey({
|
||||
network: this.network,
|
||||
|
||||
@ -122,7 +122,7 @@ KeyRing.prototype.getScript = function getScript() {
|
||||
if (!this._script) {
|
||||
assert(this.keys.length === this.n, 'Not all keys have been added.');
|
||||
|
||||
redeem = bcoin.script.createMultisig(this.keys, this.m, this.n);
|
||||
redeem = bcoin.script.fromMultisig(this.m, this.n, this.keys);
|
||||
|
||||
if (redeem.getSize() > 520)
|
||||
throw new Error('Redeem script too large (520 byte limit).');
|
||||
@ -146,11 +146,11 @@ KeyRing.prototype.getProgram = function getProgram() {
|
||||
|
||||
if (!this._program) {
|
||||
if (this.type === 'pubkeyhash') {
|
||||
hash = utils.ripesha(this.getPublicKey());
|
||||
program = bcoin.script.createWitnessProgram(0, hash);
|
||||
hash = utils.hash160(this.getPublicKey());
|
||||
program = bcoin.script.fromProgram(0, hash);
|
||||
} else if (this.type === 'multisig') {
|
||||
hash = utils.sha256(this.getScript().toRaw());
|
||||
program = bcoin.script.createWitnessProgram(0, hash);
|
||||
program = bcoin.script.fromProgram(0, hash);
|
||||
} else {
|
||||
assert(false, 'Unknown address type.');
|
||||
}
|
||||
@ -172,7 +172,7 @@ KeyRing.prototype.getProgramHash = function getProgramHash(enc) {
|
||||
return;
|
||||
|
||||
if (!this._programHash)
|
||||
this._programHash = utils.ripesha(this.getProgram().toRaw());
|
||||
this._programHash = utils.hash160(this.getProgram().toRaw());
|
||||
|
||||
return enc === 'hex'
|
||||
? this._programHash.toString('hex')
|
||||
@ -222,7 +222,7 @@ KeyRing.prototype.getScriptHash160 = function getScriptHash256(enc) {
|
||||
return;
|
||||
|
||||
if (!this._scriptHash160)
|
||||
this._scriptHash160 = utils.ripesha(this.getScript().toRaw());
|
||||
this._scriptHash160 = utils.hash160(this.getScript().toRaw());
|
||||
|
||||
return enc === 'hex'
|
||||
? this._scriptHash160.toString('hex')
|
||||
@ -280,7 +280,7 @@ KeyRing.prototype.getScriptAddress = function getScriptAddress() {
|
||||
|
||||
KeyRing.prototype.getKeyHash = function getKeyHash(enc) {
|
||||
if (!this._hash)
|
||||
this._hash = utils.ripesha(this.getPublicKey());
|
||||
this._hash = utils.hash160(this.getPublicKey());
|
||||
|
||||
return enc === 'hex'
|
||||
? this._hash.toString('hex')
|
||||
|
||||
@ -210,7 +210,7 @@ MerkleBlock.prototype.extractTree = function extractTree() {
|
||||
right = left;
|
||||
}
|
||||
|
||||
return utils.dsha256(Buffer.concat([left, right]));
|
||||
return utils.hash256(Buffer.concat([left, right]));
|
||||
}
|
||||
|
||||
for (p = 0; p < this.hashes.length; p++)
|
||||
@ -493,7 +493,7 @@ MerkleBlock.fromBlock = function fromBlock(block, filter) {
|
||||
else
|
||||
right = left;
|
||||
|
||||
return utils.dsha256(Buffer.concat([left, right]));
|
||||
return utils.hash256(Buffer.concat([left, right]));
|
||||
}
|
||||
|
||||
function traverse(height, pos, leaves, matches) {
|
||||
|
||||
@ -420,7 +420,7 @@ function MinerBlock(options) {
|
||||
if (this.witness) {
|
||||
// Set up the witness nonce and
|
||||
// commitment output for segwit.
|
||||
this.witnessNonce = utils.dsha256(new Buffer(this.tip.hash, 'hex'));
|
||||
this.witnessNonce = utils.hash256(new Buffer(this.tip.hash, 'hex'));
|
||||
this.coinbase.inputs[0].witness.items[0] = this.witnessNonce;
|
||||
this.coinbase.addOutput({
|
||||
script: new bcoin.script(),
|
||||
@ -532,7 +532,7 @@ MinerBlock.prototype.findNonce = function findNonce() {
|
||||
// The heart and soul of the miner: match the target.
|
||||
while (block.nonce <= 0xffffffff) {
|
||||
// Hash and test against the next target
|
||||
if (rcmp(utils.dsha256(data), target) <= 0)
|
||||
if (rcmp(utils.hash256(data), target) <= 0)
|
||||
return true;
|
||||
|
||||
// Increment the nonce to get a different hash
|
||||
|
||||
@ -210,7 +210,7 @@ MTX.prototype.scriptInput = function scriptInput(index, addr) {
|
||||
prev = addr.script;
|
||||
} else if (addr.program.isWitnessPubkeyhash()) {
|
||||
// P2WPKH nested within pay-to-scripthash.
|
||||
prev = Script.createPubkeyhash(addr.keyHash);
|
||||
prev = Script.fromPubkeyhash(addr.keyHash);
|
||||
} else {
|
||||
assert(false, 'Unknown program.');
|
||||
}
|
||||
@ -222,7 +222,7 @@ MTX.prototype.scriptInput = function scriptInput(index, addr) {
|
||||
} else {
|
||||
return false;
|
||||
}
|
||||
} else if (prev.isWitnessProgram()) {
|
||||
} else if (prev.isProgram()) {
|
||||
// Witness program.
|
||||
vector = input.witness;
|
||||
|
||||
@ -238,7 +238,7 @@ MTX.prototype.scriptInput = function scriptInput(index, addr) {
|
||||
if (!utils.equal(prev.get(1), addr.keyHash))
|
||||
return false;
|
||||
|
||||
prev = Script.createPubkeyhash(prev.get(1));
|
||||
prev = Script.fromPubkeyhash(prev.get(1));
|
||||
} else {
|
||||
// Bare... who knows?
|
||||
return false;
|
||||
@ -412,7 +412,7 @@ MTX.prototype.signInput = function signInput(index, addr, key, type) {
|
||||
len = vector.length - 1;
|
||||
version = 1;
|
||||
} else if (prev.isWitnessPubkeyhash()) {
|
||||
prev = Script.createPubkeyhash(prev.get(1));
|
||||
prev = Script.fromPubkeyhash(prev.get(1));
|
||||
vector = input.witness;
|
||||
len = vector.length;
|
||||
version = 1;
|
||||
@ -616,7 +616,7 @@ MTX.prototype.isSigned = function isSigned() {
|
||||
vector = input.witness;
|
||||
len = vector.length - 1;
|
||||
} else if (prev.isWitnessPubkeyhash()) {
|
||||
prev = Script.createPubkeyhash(prev.get(1));
|
||||
prev = Script.fromPubkeyhash(prev.get(1));
|
||||
vector = input.witness;
|
||||
len = vector.length;
|
||||
}
|
||||
@ -694,8 +694,10 @@ MTX.prototype.sign = function sign(index, addr, key, type) {
|
||||
MTX.prototype.addOutput = function addOutput(address, value) {
|
||||
var options, output;
|
||||
|
||||
if ((address instanceof bcoin.wallet) || (address instanceof bcoin.keyring))
|
||||
if ((address instanceof bcoin.wallet)
|
||||
|| (address instanceof bcoin.keyring)) {
|
||||
address = address.getAddress();
|
||||
}
|
||||
|
||||
if (typeof address === 'string') {
|
||||
options = {
|
||||
@ -708,37 +710,14 @@ MTX.prototype.addOutput = function addOutput(address, value) {
|
||||
|
||||
output = bcoin.output(options, true);
|
||||
|
||||
if (options.address)
|
||||
output.script = Script.fromAddress(options.address);
|
||||
|
||||
this.outputs.push(output);
|
||||
|
||||
this.scriptOutput(this.outputs.length - 1, options);
|
||||
|
||||
return this;
|
||||
};
|
||||
|
||||
/**
|
||||
* Build output script (called automatically from {@link MTX#addOutput}).
|
||||
* @param {Number} index - Output index.
|
||||
* @param {Object} options - See {@link Script.createOutputScript}.
|
||||
*/
|
||||
|
||||
MTX.prototype.scriptOutput = function scriptOutput(index, options) {
|
||||
var output;
|
||||
|
||||
if (options instanceof bcoin.output)
|
||||
return;
|
||||
|
||||
if (typeof index !== 'number')
|
||||
index = this.outputs.indexOf(index);
|
||||
|
||||
output = this.outputs[index];
|
||||
assert(output);
|
||||
|
||||
if (options.script)
|
||||
output.script = Script(options.script);
|
||||
else
|
||||
output.script = Script.createOutputScript(options);
|
||||
};
|
||||
|
||||
/**
|
||||
* Test whether the transaction at least
|
||||
* has all script templates built.
|
||||
@ -855,7 +834,7 @@ MTX.prototype.maxSize = function maxSize(options, force) {
|
||||
}
|
||||
}
|
||||
|
||||
if (prev.isWitnessProgram()) {
|
||||
if (prev.isProgram()) {
|
||||
witness = true;
|
||||
|
||||
// Now calculating vsize.
|
||||
@ -885,7 +864,7 @@ MTX.prototype.maxSize = function maxSize(options, force) {
|
||||
size += sz;
|
||||
}
|
||||
} else if (prev.isWitnessPubkeyhash()) {
|
||||
prev = Script.createPubkeyhash(prev.get(1));
|
||||
prev = Script.fromPubkeyhash(prev.get(1));
|
||||
}
|
||||
}
|
||||
|
||||
@ -1062,12 +1041,11 @@ MTX.prototype.selectCoins = function selectCoins(coins, options) {
|
||||
// Add dummy output (for `change`) to
|
||||
// calculate maximum TX size.
|
||||
tx.addOutput({
|
||||
address: options.changeAddress,
|
||||
// In case we don't have a change address,
|
||||
// use a fake p2pkh output to gauge size.
|
||||
script: !options.changeAddress
|
||||
? Script.createPubkeyhash(HASH160)
|
||||
: null,
|
||||
script: options.changeAddress
|
||||
? Script.fromAddress(options.changeAddress)
|
||||
: Script.fromPubkeyhash(HASH160),
|
||||
value: 0
|
||||
});
|
||||
|
||||
|
||||
@ -60,7 +60,7 @@ Framer.prototype.header = function header(cmd, payload, checksum) {
|
||||
h.writeUInt32LE(payload.length, 16, true);
|
||||
|
||||
if (!checksum)
|
||||
checksum = utils.dsha256(payload);
|
||||
checksum = utils.hash256(payload);
|
||||
|
||||
// Checksum
|
||||
checksum.copy(h, 20, 0, 4);
|
||||
@ -828,13 +828,13 @@ Framer.alert = function alert(data, key, writer) {
|
||||
|
||||
if (!data.signature) {
|
||||
assert(key, 'No key or signature.');
|
||||
hash = utils.dsha256(payload);
|
||||
hash = utils.hash256(payload);
|
||||
data.signature = bcoin.ec.sign(hash, key);
|
||||
}
|
||||
|
||||
if (!data.hash) {
|
||||
if (!hash)
|
||||
hash = utils.dsha256(payload);
|
||||
hash = utils.hash256(payload);
|
||||
data.hash = hash.toString('hex');
|
||||
}
|
||||
|
||||
|
||||
@ -775,7 +775,7 @@ Parser.parseAlert = function parseAlert(p) {
|
||||
reserved = p.readVarString('ascii');
|
||||
|
||||
return {
|
||||
hash: utils.dsha256(payload).toString('hex'),
|
||||
hash: utils.hash256(payload).toString('hex'),
|
||||
version: version,
|
||||
relayUntil: relayUntil,
|
||||
expiration: expiration,
|
||||
|
||||
@ -1747,13 +1747,13 @@ Script.prototype.execute = function execute(stack, flags, tx, index, version) {
|
||||
case opcodes.OP_HASH160: {
|
||||
if (stack.length === 0)
|
||||
throw new ScriptError('INVALID_STACK_OPERATION', op, ip);
|
||||
stack.push(utils.ripesha(stack.pop()));
|
||||
stack.push(utils.hash160(stack.pop()));
|
||||
break;
|
||||
}
|
||||
case opcodes.OP_HASH256: {
|
||||
if (stack.length === 0)
|
||||
throw new ScriptError('INVALID_STACK_OPERATION', op, ip);
|
||||
stack.push(utils.dsha256(stack.pop()));
|
||||
stack.push(utils.hash256(stack.pop()));
|
||||
break;
|
||||
}
|
||||
case opcodes.OP_CODESEPARATOR: {
|
||||
@ -2335,7 +2335,7 @@ Script.isCode = function isCode(raw) {
|
||||
* @returns {Script}
|
||||
*/
|
||||
|
||||
Script.createPubkey = function createPubkey(key) {
|
||||
Script.fromPubkey = function fromPubkey(key) {
|
||||
assert(key.length >= 33);
|
||||
return Script.fromArray([key, opcodes.OP_CHECKSIG]);
|
||||
};
|
||||
@ -2346,7 +2346,7 @@ Script.createPubkey = function createPubkey(key) {
|
||||
* @returns {Script}
|
||||
*/
|
||||
|
||||
Script.createPubkeyhash = function createPubkeyhash(hash) {
|
||||
Script.fromPubkeyhash = function fromPubkeyhash(hash) {
|
||||
assert(hash.length === 20);
|
||||
return Script.fromArray([
|
||||
opcodes.OP_DUP,
|
||||
@ -2365,7 +2365,7 @@ Script.createPubkeyhash = function createPubkeyhash(hash) {
|
||||
* @returns {Script}
|
||||
*/
|
||||
|
||||
Script.createMultisig = function createMultisig(keys, m, n) {
|
||||
Script.fromMultisig = function fromMultisig(m, n, keys) {
|
||||
var code = [];
|
||||
var i;
|
||||
|
||||
@ -2392,7 +2392,7 @@ Script.createMultisig = function createMultisig(keys, m, n) {
|
||||
* @returns {Script}
|
||||
*/
|
||||
|
||||
Script.createScripthash = function createScripthash(hash) {
|
||||
Script.fromScripthash = function fromScripthash(hash) {
|
||||
assert(hash.length === 20);
|
||||
return Script.fromArray([
|
||||
opcodes.OP_HASH160,
|
||||
@ -2407,7 +2407,7 @@ Script.createScripthash = function createScripthash(hash) {
|
||||
* @returns {Script}
|
||||
*/
|
||||
|
||||
Script.createNulldata = function createNulldata(flags) {
|
||||
Script.fromNulldata = function fromNulldata(flags) {
|
||||
assert(Buffer.isBuffer(flags));
|
||||
assert(flags.length <= constants.script.MAX_OP_RETURN, 'Nulldata too large.');
|
||||
return Script.fromArray([
|
||||
@ -2423,12 +2423,34 @@ Script.createNulldata = function createNulldata(flags) {
|
||||
* @returns {Script}
|
||||
*/
|
||||
|
||||
Script.createWitnessProgram = function createWitnessProgram(version, data) {
|
||||
Script.fromProgram = function fromProgram(version, data) {
|
||||
assert(typeof version === 'number' && version >= 0 && version <= 16);
|
||||
assert(data.length >= 2 && data.length <= 32);
|
||||
return Script.fromArray([version === 0 ? 0 : version + 0x50, data]);
|
||||
};
|
||||
|
||||
/**
|
||||
* Create an output script from an address.
|
||||
* @param {Address|Base58Address} address
|
||||
* @returns {Script}
|
||||
*/
|
||||
|
||||
Script.fromAddress = function fromAddress(address) {
|
||||
if (typeof address === 'string')
|
||||
address = bcoin.address.fromBase58(address);
|
||||
|
||||
if (address.type === 'pubkeyhash')
|
||||
return Script.fromPubkeyhash(address.hash);
|
||||
|
||||
if (address.type === 'scripthash')
|
||||
return Script.fromScripthash(address.hash);
|
||||
|
||||
if (address.version !== -1)
|
||||
return Script.fromProgram(address.version, address.hash);
|
||||
|
||||
assert(false, 'Bad type.');
|
||||
};
|
||||
|
||||
/**
|
||||
* Create a witness block commitment.
|
||||
* @param {Buffer} hash
|
||||
@ -2488,7 +2510,7 @@ Script.prototype.getRedeem = function getRedeem() {
|
||||
*/
|
||||
|
||||
Script.prototype.getType = function getType() {
|
||||
if (this.isWitnessProgram()) {
|
||||
if (this.isProgram()) {
|
||||
if (this.isWitnessPubkeyhash())
|
||||
return 'witnesspubkeyhash';
|
||||
if (this.isWitnessScripthash())
|
||||
@ -2744,7 +2766,7 @@ Script.prototype.getCommitmentHash = function getCommitmentHash() {
|
||||
* @return {Boolean}
|
||||
*/
|
||||
|
||||
Script.prototype.isWitnessProgram = function isWitnessProgram() {
|
||||
Script.prototype.isProgram = function isProgram() {
|
||||
if (!(this.raw.length >= 4 && this.raw.length <= 42))
|
||||
return false;
|
||||
|
||||
@ -2767,7 +2789,7 @@ Script.prototype.isWitnessProgram = function isWitnessProgram() {
|
||||
Script.prototype.toProgram = function toProgram() {
|
||||
var version, data;
|
||||
|
||||
if (!this.isWitnessProgram())
|
||||
if (!this.isProgram())
|
||||
return;
|
||||
|
||||
version = Script.getSmall(this.raw[0]);
|
||||
@ -2831,64 +2853,6 @@ Script.prototype.isUnknownInput = function isUnknownInput() {
|
||||
return this.getInputType() === 'unknown';
|
||||
};
|
||||
|
||||
/**
|
||||
* Automatically build an output script from any number of options.
|
||||
* @example
|
||||
* Script.createOutputScript({ address: '1HT7xU2Ngenf7D4yocz2SAcnNLW7rK8d4E' });
|
||||
* @param {Object} options
|
||||
* @param {(Address|Base58Address)?} options.address - Address to send to.
|
||||
* @param {Buffer?} options.flags - Nulldata flags.
|
||||
* @param {Buffer?} options.key - Key for pay-to-pubkey.
|
||||
* @param {Buffer?} options.keyHash - Key has for pay-to-pubkeyhash.
|
||||
* @param {Buffer[]?} options.keys - Keys for pay-to-multisig.
|
||||
* @param {Buffer} options.scriptHash - Whether to create a scripthash
|
||||
* @returns {Script}
|
||||
*/
|
||||
|
||||
Script.createOutputScript = function createOutputScript(options) {
|
||||
var m, n, flags, address;
|
||||
|
||||
if (!options)
|
||||
options = {};
|
||||
|
||||
if (options.address) {
|
||||
address = options.address;
|
||||
if (typeof address === 'string')
|
||||
address = bcoin.address.fromBase58(address);
|
||||
return address.toScript();
|
||||
}
|
||||
|
||||
if (options.flags) {
|
||||
flags = options.flags;
|
||||
if (typeof flags === 'string')
|
||||
flags = new Buffer(flags, 'utf8');
|
||||
return Script.createNulldata(flags);
|
||||
}
|
||||
|
||||
if (options.key)
|
||||
return Script.createPubkey(options.key);
|
||||
|
||||
if (options.keyHash) {
|
||||
if (options.version != null)
|
||||
return Script.createWitnessProgram(options.version, options.keyHash);
|
||||
return Script.createPubkeyhash(options.keyHash);
|
||||
}
|
||||
|
||||
if (options.keys) {
|
||||
m = options.m;
|
||||
n = options.n || options.keys.length;
|
||||
return Script.createMultisig(options.keys, m, n);
|
||||
}
|
||||
|
||||
if (options.scriptHash) {
|
||||
if (options.version != null)
|
||||
return Script.createWitnessProgram(options.version, options.scriptHash);
|
||||
return Script.createScripthash(options.scriptHash);
|
||||
}
|
||||
|
||||
return new Script();
|
||||
};
|
||||
|
||||
/**
|
||||
* "Guess" whether the input script is pay-to-pubkey.
|
||||
* This method is not 100% reliable.
|
||||
@ -3795,7 +3759,7 @@ Script.getWitnessSigops = function getWitnessSigops(input, output, witness, flag
|
||||
|
||||
assert((flags & constants.flags.VERIFY_P2SH) !== 0);
|
||||
|
||||
if (output.isWitnessProgram())
|
||||
if (output.isProgram())
|
||||
return Script.witnessSigops(output.toProgram(), witness, flags);
|
||||
|
||||
// This is a unique situation in terms of consensus
|
||||
@ -3807,7 +3771,7 @@ Script.getWitnessSigops = function getWitnessSigops(input, output, witness, flag
|
||||
// does not check the return value of GetOp.
|
||||
if (output.isScripthash() && input.isPushOnly()) {
|
||||
redeem = input.getRedeem();
|
||||
if (redeem && redeem.isWitnessProgram())
|
||||
if (redeem && redeem.isProgram())
|
||||
return Script.witnessSigops(redeem.toProgram(), witness, flags);
|
||||
}
|
||||
|
||||
@ -3943,7 +3907,7 @@ Script.verify = function verify(input, witness, output, tx, i, flags) {
|
||||
if (stack.length === 0 || !Script.bool(stack.pop()))
|
||||
throw new ScriptError('EVAL_FALSE');
|
||||
|
||||
if ((flags & constants.flags.VERIFY_WITNESS) && output.isWitnessProgram()) {
|
||||
if ((flags & constants.flags.VERIFY_WITNESS) && output.isProgram()) {
|
||||
hadWitness = true;
|
||||
|
||||
// Input script must be empty.
|
||||
@ -3981,7 +3945,7 @@ Script.verify = function verify(input, witness, output, tx, i, flags) {
|
||||
if (stack.length === 0 || !Script.bool(stack.pop()))
|
||||
throw new ScriptError('EVAL_FALSE');
|
||||
|
||||
if ((flags & constants.flags.VERIFY_WITNESS) && redeem.isWitnessProgram()) {
|
||||
if ((flags & constants.flags.VERIFY_WITNESS) && redeem.isProgram()) {
|
||||
hadWitness = true;
|
||||
|
||||
// Input script must be exactly one push of the redeem script.
|
||||
@ -4049,7 +4013,7 @@ Script.verifyProgram = function verifyProgram(witness, output, flags, tx, i) {
|
||||
if (stack.length !== 2)
|
||||
throw new ScriptError('WITNESS_PROGRAM_MISMATCH');
|
||||
|
||||
redeem = Script.createPubkeyhash(program.data);
|
||||
redeem = Script.fromPubkeyhash(program.data);
|
||||
} else {
|
||||
// Failure on version=0 (bad program data length)
|
||||
throw new ScriptError('WITNESS_PROGRAM_WRONG_LENGTH');
|
||||
|
||||
@ -195,7 +195,7 @@ TX.prototype.hash = function _hash(enc) {
|
||||
var hash = this._hash;
|
||||
|
||||
if (!hash) {
|
||||
hash = utils.dsha256(this.toNormal());
|
||||
hash = utils.hash256(this.toNormal());
|
||||
if (!this.mutable)
|
||||
this._hash = hash;
|
||||
}
|
||||
@ -225,7 +225,7 @@ TX.prototype.witnessHash = function witnessHash(enc) {
|
||||
return this.hash(enc);
|
||||
|
||||
if (!hash) {
|
||||
hash = utils.dsha256(this.toWitness());
|
||||
hash = utils.hash256(this.toWitness());
|
||||
if (!this.mutable)
|
||||
this._whash = hash;
|
||||
}
|
||||
@ -524,7 +524,7 @@ TX.prototype.signatureHashV0 = function signatureHashV0(index, prev, type) {
|
||||
copy.toRaw(p);
|
||||
p.writeU32(type);
|
||||
|
||||
return utils.dsha256(p.render());
|
||||
return utils.hash256(p.render());
|
||||
};
|
||||
|
||||
TX.prototype.signatureHashV1 = function signatureHashV1(index, prev, type) {
|
||||
@ -547,7 +547,7 @@ TX.prototype.signatureHashV1 = function signatureHashV1(index, prev, type) {
|
||||
hashPrevouts = new BufferWriter();
|
||||
for (i = 0; i < this.inputs.length; i++)
|
||||
this.inputs[i].prevout.toRaw(hashPrevouts);
|
||||
hashPrevouts = utils.dsha256(hashPrevouts.render());
|
||||
hashPrevouts = utils.hash256(hashPrevouts.render());
|
||||
if (!this.mutable)
|
||||
this._hashPrevouts = hashPrevouts;
|
||||
}
|
||||
@ -564,7 +564,7 @@ TX.prototype.signatureHashV1 = function signatureHashV1(index, prev, type) {
|
||||
hashSequence = new BufferWriter();
|
||||
for (i = 0; i < this.inputs.length; i++)
|
||||
hashSequence.writeU32(this.inputs[i].sequence);
|
||||
hashSequence = utils.dsha256(hashSequence.render());
|
||||
hashSequence = utils.hash256(hashSequence.render());
|
||||
if (!this.mutable)
|
||||
this._hashSequence = hashSequence;
|
||||
}
|
||||
@ -580,13 +580,13 @@ TX.prototype.signatureHashV1 = function signatureHashV1(index, prev, type) {
|
||||
hashOutputs = new BufferWriter();
|
||||
for (i = 0; i < this.outputs.length; i++)
|
||||
this.outputs[i].toRaw(hashOutputs);
|
||||
hashOutputs = utils.dsha256(hashOutputs.render());
|
||||
hashOutputs = utils.hash256(hashOutputs.render());
|
||||
if (!this.mutable)
|
||||
this._hashOutputs = hashOutputs;
|
||||
}
|
||||
} else if ((type & 0x1f) === constants.hashType.SINGLE && index < this.outputs.length) {
|
||||
hashOutputs = this.outputs[index].toRaw();
|
||||
hashOutputs = utils.dsha256(hashOutputs);
|
||||
hashOutputs = utils.hash256(hashOutputs);
|
||||
} else {
|
||||
hashOutputs = utils.copy(constants.ZERO_HASH);
|
||||
}
|
||||
@ -603,7 +603,7 @@ TX.prototype.signatureHashV1 = function signatureHashV1(index, prev, type) {
|
||||
p.writeU32(this.locktime);
|
||||
p.writeU32(type);
|
||||
|
||||
return utils.dsha256(p.render());
|
||||
return utils.hash256(p.render());
|
||||
};
|
||||
|
||||
/**
|
||||
|
||||
@ -257,7 +257,7 @@ utils.sha1 = function sha1(data, enc) {
|
||||
|
||||
utils.sha256 = function sha256(data, enc) {
|
||||
if (supersha) {
|
||||
if (!Buffer.isBuffer(data))
|
||||
if (typeof data === 'string')
|
||||
data = new Buffer(data, enc);
|
||||
return supersha.sha256(data);
|
||||
}
|
||||
@ -271,7 +271,7 @@ utils.sha256 = function sha256(data, enc) {
|
||||
* @returns {Buffer}
|
||||
*/
|
||||
|
||||
utils.ripesha = function ripesha(data, enc) {
|
||||
utils.hash160 = function hash160(data, enc) {
|
||||
return utils.ripemd160(utils.sha256(data, enc));
|
||||
};
|
||||
|
||||
@ -282,9 +282,9 @@ utils.ripesha = function ripesha(data, enc) {
|
||||
* @returns {Buffer}
|
||||
*/
|
||||
|
||||
utils.dsha256 = function dsha256(data, enc) {
|
||||
utils.hash256 = function hash256(data, enc) {
|
||||
if (supersha) {
|
||||
if (!Buffer.isBuffer(data))
|
||||
if (typeof data === 'string')
|
||||
data = new Buffer(data, enc);
|
||||
return supersha.dsha256(data);
|
||||
}
|
||||
@ -299,7 +299,7 @@ utils.dsha256 = function dsha256(data, enc) {
|
||||
*/
|
||||
|
||||
utils.checksum = function checksum(data, enc) {
|
||||
return utils.dsha256(data, enc).slice(0, 4);
|
||||
return utils.hash256(data, enc).slice(0, 4);
|
||||
};
|
||||
|
||||
/**
|
||||
@ -2106,7 +2106,7 @@ utils.buildMerkleTree = function buildMerkleTree(leaves) {
|
||||
return;
|
||||
}
|
||||
hash = Buffer.concat([tree[j + i], tree[j + i2]]);
|
||||
hash = utils.dsha256(hash);
|
||||
hash = utils.hash256(hash);
|
||||
tree.push(hash);
|
||||
}
|
||||
j += size;
|
||||
@ -2177,9 +2177,9 @@ utils.checkMerkleBranch = function checkMerkleBranch(hash, branch, index) {
|
||||
otherside = branch[i];
|
||||
|
||||
if (index & 1)
|
||||
hash = utils.dsha256(Buffer.concat([otherside, hash]));
|
||||
hash = utils.hash256(Buffer.concat([otherside, hash]));
|
||||
else
|
||||
hash = utils.dsha256(Buffer.concat([hash, otherside]));
|
||||
hash = utils.hash256(Buffer.concat([hash, otherside]));
|
||||
|
||||
index >>>= 1;
|
||||
}
|
||||
|
||||
@ -326,7 +326,7 @@ Wallet.prototype.getID = function getID() {
|
||||
p.writeU8(0x03);
|
||||
p.writeU8(0xbe);
|
||||
p.writeU8(0x04);
|
||||
p.writeBytes(utils.ripesha(key.publicKey));
|
||||
p.writeBytes(utils.hash160(key.publicKey));
|
||||
p.writeChecksum();
|
||||
|
||||
return utils.toBase58(p.render());
|
||||
|
||||
@ -206,7 +206,7 @@ describe('Protocol', function() {
|
||||
p.start();
|
||||
while (p.left()) {
|
||||
var details = bcoin.protocol.parser.parseAlert(p);
|
||||
var hash = utils.dsha256(details.payload);
|
||||
var hash = utils.hash256(details.payload);
|
||||
var signature = details.signature;
|
||||
assert(bcoin.ec.verify(hash, signature, network.alertKey));
|
||||
delete details.payload;
|
||||
|
||||
@ -157,12 +157,15 @@ describe('Wallet', function() {
|
||||
var k2 = bcoin.hd.fromMnemonic().deriveAccount44(0).hdPublicKey;
|
||||
w.addKey(k2, function(err) {
|
||||
assert.ifError(err);
|
||||
// Input transcation
|
||||
var keys = [
|
||||
w.getPublicKey(),
|
||||
k2.derive('m/0/0').publicKey
|
||||
];
|
||||
// Input transaction (bare 1-of-2 multisig)
|
||||
var src = bcoin.mtx({
|
||||
outputs: [{
|
||||
value: 5460 * 2,
|
||||
m: 1,
|
||||
keys: [ w.getPublicKey(), k2.derive('m/0/0').publicKey ]
|
||||
script: bcoin.script.fromMultisig(1, 2, keys)
|
||||
}, {
|
||||
value: 5460 * 2,
|
||||
address: bcoin.address.fromData(new Buffer([])).toBase58()
|
||||
|
||||
Loading…
Reference in New Issue
Block a user