How to use the bech32.decode 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 bitcoinjs / bolt11 / payreq.js View on Github external
function decode (paymentRequest, network) {
  if (typeof paymentRequest !== 'string') throw new Error('Lightning Payment Request must be string')
  if (paymentRequest.slice(0, 2).toLowerCase() !== 'ln') throw new Error('Not a proper lightning payment request')
  let decoded = bech32.decode(paymentRequest, Number.MAX_SAFE_INTEGER)
  paymentRequest = paymentRequest.toLowerCase()
  let prefix = decoded.prefix
  let words = decoded.words

  // signature is always 104 words on the end
  // cutting off at the beginning helps since there's no way to tell
  // ahead of time how many tags there are.
  let sigWords = words.slice(-104)
  // grabbing a copy of the words for later, words will be sliced as we parse.
  let wordsNoSig = words.slice(0, -104)
  words = words.slice(0, -104)

  let sigBuffer = wordsToBuffer(sigWords, true)
  let recoveryFlag = sigBuffer.slice(-1)[0]
  sigBuffer = sigBuffer.slice(0, -1)
github bitcoinjs / bolt11 / payreq.js View on Github external
// If they are not equal throw an error
  if (nodePublicKey && tagNodePublicKey && !tagNodePublicKey.equals(nodePublicKey)) {
    throw new Error('payee node key tag and payeeNodeKey attribute must match')
  }

  // make sure if either exist they are in nodePublicKey
  nodePublicKey = tagNodePublicKey || nodePublicKey

  let publicKey = secp256k1.publicKeyCreate(privateKey)

  // Check if pubkey matches for private key
  if (nodePublicKey && !publicKey.equals(nodePublicKey)) {
    throw new Error('The private key given is not the private key of the node public key given')
  }

  let words = bech32.decode(payReqObj.wordsTemp, Number.MAX_SAFE_INTEGER).words

  // the preimage for the signing data is the buffer of the prefix concatenated
  // with the buffer conversion of the data words excluding the signature
  // (right padded with 0 bits)
  let toSign = Buffer.concat([Buffer.from(payReqObj.prefix, 'utf8'), wordsToBuffer(words)])
  // single SHA256 hash for the signature
  let payReqHash = sha256(toSign)

  // signature is 64 bytes (32 byte r value and 32 byte s value concatenated)
  // PLUS one extra byte appended to the right with the recoveryID in [0,1,2,3]
  // Then convert to 5 bit words with right padding 0 bits.
  let sigObj = secp256k1.sign(payReqHash, privateKey)
  let sigWords = hexToWord(sigObj.signature.toString('hex') + '0' + sigObj.recovery)

  // append signature words to the words, mark as complete, and add the payreq
  payReqObj.payeeNodeKey = publicKey.toString('hex')
github altangent / lightnode-invoice / src / 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.amount).to.equal(0.02);
      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')
      );
github irisnet / irisnet-crypto / src / chains / cosmos / stake.js View on Github external
static createMsgDelegate(req) {
        let value = {
            denom: req.msg.amount.denom,
            amount: Utils.toString(req.msg.amount.amount),
        };

        let delegator_addr = BECH32.decode(req.from).words;
        let validator_addr = BECH32.decode(req.msg.validator_addr).words;

        return new MsgDelegate({
            DelegatorAddress:delegator_addr,
            ValidatorAddress:validator_addr,
            Amount:value,
        });
    }
github bitcoinjs / bolt11 / payreq.js View on Github external
function unknownEncoder (data) {
  data.words = bech32.decode(data.words, Number.MAX_SAFE_INTEGER).words
  return data
}
github irisnet / irisnet-crypto / src / chains / cosmos / stake.js View on Github external
static createMsgBeginRedelegate(req) {
        let value = {
            denom: req.msg.amount.denom,
            amount: Utils.toString(req.msg.amount.amount),
        };
        let delegator_addr = BECH32.decode(req.from).words;
        let validator_src_addr = BECH32.decode(req.msg.validator_src_addr).words;
        let validator_dst_addr = BECH32.decode(req.msg.validator_dst_addr).words;

        return new MsgBeginRedelegate({
            DelegatorAddress:delegator_addr,
            ValidatorSrcAddress:validator_src_addr,
            ValidatorDstAddress:validator_dst_addr,
            Amount:value,
        });
    }
};
github irisnet / irisnet-crypto / src / chains / cosmos / distribution.js View on Github external
static CreateMsgSetWithdrawAddress(req) {
        let delegator_addr = BECH32.decode(req.from).words;
        let withdraw_addr = BECH32.decode(req.msg.withdraw_addr).words;
        return new MsgSetWithdrawAddress({
            DelegatorAddress:delegator_addr,
            WithdrawAddress:withdraw_addr,
        });
    }
    static CreateMsgWithdrawDelegatorReward(req) {
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);

        return {
            delegatorAddr: delegator_addr,
        }
    }
github irisnet / irisnet-crypto / src / chains / cosmos / stake.js View on Github external
static createMsgBeginRedelegate(req) {
        let value = {
            denom: req.msg.amount.denom,
            amount: Utils.toString(req.msg.amount.amount),
        };
        let delegator_addr = BECH32.decode(req.from).words;
        let validator_src_addr = BECH32.decode(req.msg.validator_src_addr).words;
        let validator_dst_addr = BECH32.decode(req.msg.validator_dst_addr).words;

        return new MsgBeginRedelegate({
            DelegatorAddress:delegator_addr,
            ValidatorSrcAddress:validator_src_addr,
            ValidatorDstAddress:validator_dst_addr,
            Amount:value,
        });
    }
};

bech32

Bech32 encoding / decoding

MIT
Latest version published 4 years ago

Package Health Score

73 / 100
Full package analysis