How to use the bech32.fromWords function in bech32

To help you get started, we’ve selected a few bech32 examples, based on popular ways it is used in public projects.

Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.

github CounterpartyXCP / indexd-server / lib / express.js View on Github external
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')
  }
github altangent / lntools / packages / lntools-invoice / __tests__ / decoder.spec.js View on Github external
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"),
      );
github lino-network / lino-js / src / transport / encoder.ts View on Github external
}
      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;
}
github irisnet / irisnet-crypto / util / bech32.js View on Github external
static fromBech32(bech32Str) {
        let ownKey = BECH32.decode(bech32Str);
        return Hex.bytesToHex(BECH32.fromWords(ownKey.words)).toUpperCase();
    }
github irisnet / irisnet-crypto / src / chains / cosmos / bank.js View on Github external
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
    }
};
github irisnet / irisnet-crypto / src / chains / iris / distribution.js View on Github external
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,
        }
    }
github lino-network / lino-js / src / transport / encoder.ts View on Github external
}
      }
      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;
}
github bitcoinjs / bitcoinjs-lib / ts_src / payments / p2wsh.ts View on Github external
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(() => {
github irisnet / irisnet-crypto / src / chains / iris / stake.js View on Github external
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
        }
    }
github irisnet / irisnet-crypto / src / chains / cosmos / stake.js View on Github external
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
    }
};

bech32

Bech32 encoding / decoding

MIT
Latest version published 4 years ago

Package Health Score

73 / 100
Full package analysis