Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
function addressToScriptId(address) {
let script = null
if (address.startsWith('bc') || address.startsWith('tb')) {
// Regtest starts with 'bc' too
let b32res = bech32.decode(address)
let witnessData = bech32.fromWords(b32res.words.slice(1))
let witnessOpcodes = [0, 0x14]
script = Buffer.from(witnessOpcodes.concat(witnessData))
} else {
script = bitcoin.address.toOutputScript(address, network)
console.log('Output Script ID', script.toString('hex'))
}
return bitcoin.crypto.sha256(script).toString('hex')
}
it("send $24, on mainnet, with fallback (P2WSH) address bc1qrp33g0q5c5txsp9arysrx4k6zdkfs4nce4xj0gdcccefvpysxf3qccfmv3", () => {
let input =
"lnbc20m1pvjluezhp58yjmdan79s6qqdhdzgynm4zwqd5d7xmw5fk98klysy043l2ahrqspp5qqqsyqcyq5rqwzqfqqqsyqcyq5rqwzqfqqqsyqcyq5rqwzqfqypqfp4qrp33g0q5c5txsp9arysrx4k6zdkfs4nce4xj0gdcccefvpysxf3q28j0v3rwgy9pvjnd48ee2pl8xrpxysd5g44td63g6xcjcu003j3qe8878hluqlvl3km8rm92f5stamd3jw763n3hck0ct7p8wwj463cql26ava";
let result = decoder.decode(input);
let { words } = bech32.decode(
"bc1qrp33g0q5c5txsp9arysrx4k6zdkfs4nce4xj0gdcccefvpysxf3qccfmv3",
);
let address = Buffer.from(bech32.fromWords(words.slice(1)));
expect(result.network).to.equal("bc");
expect(result.valueSat).to.equal("2000000");
expect(result.timestamp).to.equal(1496314658);
expect(result.fields.length).to.equal(3);
expect(result.hashDesc).to.deep.equal(hashDescription);
expect(result.paymentHash).to.deep.equal(
Buffer.from("0001020304050607080900010203040506070809000102030405060708090102", "hex"),
);
expect(result.fallbackAddresses[0].version).to.equal(0);
expect(result.fallbackAddresses[0].address).to.deep.equal(address);
expect(result.signature.r).to.deep.equal(
Buffer.from("51e4f6446e410a164a6da9f39507e730c26241b4456ab6ea28d1b12c71ef8ca2", "hex"),
);
expect(result.signature.s).to.deep.equal(
Buffer.from("0c9cfe3dffc07d9f8db671ecaa4d20beedb193bda8ce37c59f85f82773a55d47", "hex"),
);
}
if (
decodedResult[key] !== null &&
typeof decodedResult[key] !== 'string' &&
typeof decodedResult[key] !== 'boolean' &&
typeof decodedResult[key] !== 'number'
) {
decodedResult[key] = decodeObject(result[key]);
}
if (key === 'address' && result[key].startsWith('lino')) {
var decodeRes = bech32.decode(result[key]);
if (decodeRes.prefix !== _PREFIX.PrefixAddress) {
throw new Error(`invalid prefix: ${decodeRes.prefix}\n`);
}
const tmpp = bech32.fromWords(decodeRes.words);
decodedResult[key] = encodeToHex(bech32.fromWords(decodeRes.words));
}
}
}
return decodedResult;
}
static fromBech32(bech32Str) {
let ownKey = BECH32.decode(bech32Str);
return Hex.bytesToHex(BECH32.fromWords(ownKey.words)).toUpperCase();
}
MsgSend.prototype.GetMsg = function(){
const BECH32 = require('bech32');
let from_address = BECH32.fromWords(this.FromAddress);
let to_address = BECH32.fromWords(this.ToAddress);
return {
FromAddress: from_address,
ToAddress: to_address,
Amount: this.Amount
}
};
GetMsg() {
const BECH32 = require('bech32');
let delegator_key = BECH32.decode(this.delegator_addr);
let delegator_addr = BECH32.fromWords(delegator_key.words);
let validator_key = BECH32.decode(this.validator_addr);
let validator_addr = BECH32.fromWords(validator_key.words);
return {
delegatorAddr: delegator_addr,
validatorAddr: validator_addr,
}
}
}
}
if (
decodedResult[key] !== null &&
typeof decodedResult[key] !== 'string' &&
typeof decodedResult[key] !== 'boolean' &&
typeof decodedResult[key] !== 'number'
) {
decodedResult[key] = decodeObject(result[key]);
}
if (key === 'address' && result[key].startsWith('lino')) {
var decodeRes = bech32.decode(result[key]);
if (decodeRes.prefix !== _PREFIX.PrefixAddress) {
throw new Error(`invalid prefix: ${decodeRes.prefix}\n`);
}
const tmpp = bech32.fromWords(decodeRes.words);
decodedResult[key] = encodeToHex(bech32.fromWords(decodeRes.words));
}
}
}
return decodedResult;
}
const _address = lazy.value(() => {
const result = bech32.decode(a.address);
const version = result.words.shift();
const data = bech32.fromWords(result.words);
return {
version,
prefix: result.prefix,
data: Buffer.from(data),
};
});
const _rchunks = lazy.value(() => {
GetMsg() {
const BECH32 = require('bech32');
let delegator_key = BECH32.decode(this.delegator_addr);
let delegator_addr = BECH32.fromWords(delegator_key.words);
let validator_key = BECH32.decode(this.validator_addr);
let validator_addr = BECH32.fromWords(validator_key.words);
return {
delegatorAddr: delegator_addr,
validatorAddr: validator_addr,
delegation: this.delegation
}
}
MsgUndelegate.prototype.GetMsg = function(){
const BECH32 = require('bech32');
let delegator_addr = BECH32.fromWords(this.DelegatorAddress);
let validator_addr = BECH32.fromWords(this.ValidatorAddress);
return {
DelegatorAddress: delegator_addr,
ValidatorAddress: validator_addr,
Amount: this.Amount
}
};