MCPcopy Create free account
hub / github.com/ElementsProject/lightning / json_to_address_scriptpubkey

Function json_to_address_scriptpubkey

common/json_param.c:720–784  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

718}
719
720enum address_parse_result
721json_to_address_scriptpubkey(const tal_t *ctx,
722 const struct chainparams *chainparams,
723 const char *buffer,
724 const jsmntok_t *tok, const u8 **scriptpubkey)
725{
726 struct bitcoin_address destination;
727 int witness_version;
728 /* segwit_addr_net_decode requires a buffer of size 40, and will
729 * not write to the buffer if the address is too long, so a buffer
730 * of fixed size 40 will not overflow. */
731 uint8_t witness_program[40];
732 size_t witness_program_len;
733
734 char *addrz;
735 const char *bip173;
736
737 u8 addr_version;
738
739 if (ripemd160_from_base58(&addr_version, &destination.addr,
740 buffer + tok->start, tok->end - tok->start)) {
741 if (addr_version == chainparams->p2pkh_version) {
742 *scriptpubkey = scriptpubkey_p2pkh(ctx, &destination);
743 return ADDRESS_PARSE_SUCCESS;
744 } else if (addr_version == chainparams->p2sh_version) {
745 *scriptpubkey =
746 scriptpubkey_p2sh_hash(ctx, &destination.addr);
747 return ADDRESS_PARSE_SUCCESS;
748 } else {
749 return ADDRESS_PARSE_WRONG_NETWORK;
750 }
751 /* Insert other parsers that accept pointer+len here. */
752 return ADDRESS_PARSE_UNRECOGNIZED;
753 }
754
755 /* Generate null-terminated address. */
756 addrz = tal_dup_arr(tmpctx, char, buffer + tok->start, tok->end - tok->start, 1);
757 addrz[tok->end - tok->start] = '\0';
758
759 bip173 = segwit_addr_net_decode(&witness_version, witness_program,
760 &witness_program_len, addrz, chainparams);
761 if (bip173) {
762 bool witness_ok;
763
764 /* We know the rules for v0, rest remain undefined */
765 if (witness_version == 0) {
766 witness_ok = (witness_program_len == 20 ||
767 witness_program_len == 32);
768 } else
769 witness_ok = true;
770
771 if (!witness_ok)
772 return ADDRESS_PARSE_UNRECOGNIZED;
773
774 if (!streq(bip173, chainparams->onchain_hrp))
775 return ADDRESS_PARSE_WRONG_NETWORK;
776
777 *scriptpubkey = scriptpubkey_witness_raw(ctx, witness_version,

Callers 6

param_bitcoin_addressFunction · 0.70
param_outputsFunction · 0.50
newaddr_doneFunction · 0.50
after_newaddrFunction · 0.50
param_outputs_arrayFunction · 0.50
mw_after_newaddrFunction · 0.50

Calls 5

ripemd160_from_base58Function · 0.85
scriptpubkey_p2pkhFunction · 0.85
scriptpubkey_p2sh_hashFunction · 0.85
segwit_addr_net_decodeFunction · 0.85
scriptpubkey_witness_rawFunction · 0.85

Tested by

no test coverage detected