Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
const {base, witness} = this.getSizes();
const raw = this.encode();
assert(raw.length === base + witness);
// Normal data.
const ndata = raw.slice(0, base);
// Witness data.
const wdata = raw.slice(base, base + witness);
// Left = HASH(normal-data) = normal txid
const hash = blake2b.digest(ndata);
// Right = HASH(witness-data)
const wdhash = blake2b.digest(wdata);
// WTXID = HASH(normal-txid || witness-data-hash)
const whash = blake2b.root(hash, wdhash);
if (!this.mutable) {
this._hash = hash;
this._wdhash = wdhash;
this._whash = whash;
}
return [hash, wdhash, whash];
}
sipkey(hdr) {
const hash = blake2b.digest(hdr, 32);
// Legacy hashing only uses the first 128 bits.
if (this.siphash32 === siphash32)
return hash.slice(0, 16);
return hash;
}
sipkey(hdr) {
const hash = blake2b.digest(hdr, 32);
// Legacy hashing only uses the first 128 bits.
if (this.siphash32 === siphash32)
return hash.slice(0, 16);
return hash;
}
fromWitness(witness) {
const [, pk] = witness.getPubkeyhashInput();
// We're pretty much screwed here
// since we can't get the version.
if (pk) {
this.hash = blake2b.digest(pk, 20);
this.version = 0;
return this;
}
const redeem = witness.getScripthashInput();
if (redeem) {
this.hash = blake2b.digest(redeem);
this.version = 0;
return this;
}
return null;
}
throw new RPCError(errs.TYPE_ERROR, 'Invalid parameters.');
const addr = parseAddress(b58, this.network);
if (addr.version !== 0 || addr.hash.length !== 20)
return false;
const msg = Buffer.from(MAGIC_STRING + str, 'utf8');
const hash = blake2b.digest(msg);
const key = secp256k1.recover(hash, sig, 0, true);
if (!key)
return false;
return safeEqual(blake2b.digest(key, 20), addr.hash);
}
verifyMerkle(expect) {
if (expect == null) {
expect = this.isAddress()
? FAUCET_ROOT
: AIRDROP_ROOT;
}
assert(Buffer.isBuffer(expect));
assert(expect.length === 32);
const {subproof, subindex} = this;
const {proof, index} = this;
const leaf = blake2b.digest(this.key);
if (this.isAddress()) {
const root = merkle.deriveRoot(blake2b, leaf, proof, index);
return root.equals(expect);
}
const subroot = merkle.deriveRoot(blake2b, leaf, subproof, subindex);
const root = merkle.deriveRoot(blake2b, subroot, proof, index);
return root.equals(expect);
}
getToken(nonce) {
if (!this.master.key)
throw new Error('Cannot derive token.');
const key = this.master.key.derive(44, true);
const bw = bio.write(36);
bw.writeBytes(key.privateKey);
bw.writeU32(nonce);
return blake2b.digest(bw.render());
}
rules.blind = function blind(value, nonce) {
assert(Number.isSafeInteger(value) && value >= 0);
assert(Buffer.isBuffer(nonce) && nonce.length === 32);
const bw = bio.write(40);
bw.writeU64(value);
bw.writeBytes(nonce);
return blake2b.digest(bw.render());
};
verifyMerkle(expect) {
if (expect == null) {
expect = this.isAddress()
? FAUCET_ROOT
: AIRDROP_ROOT;
}
assert(Buffer.isBuffer(expect));
assert(expect.length === 32);
const {subproof, subindex} = this;
const {proof, index} = this;
const leaf = blake2b.digest(this.key);
if (this.isAddress()) {
const root = merkle.deriveRoot(blake2b, leaf, proof, index);
return root.equals(expect);
}
const subroot = merkle.deriveRoot(blake2b, leaf, subproof, subindex);
const root = merkle.deriveRoot(blake2b, subroot, proof, index);
return root.equals(expect);
}