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

Function json_to_address_scriptpubkey

common/json_param.c:822–886  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

820}
821
822enum address_parse_result
823json_to_address_scriptpubkey(const tal_t *ctx,
824 const struct chainparams *chainparams,
825 const char *buffer,
826 const jsmntok_t *tok, const u8 **scriptpubkey)
827{
828 struct bitcoin_address destination;
829 int witness_version;
830 /* segwit_addr_net_decode requires a buffer of size 40, and will
831 * not write to the buffer if the address is too long, so a buffer
832 * of fixed size 40 will not overflow. */
833 uint8_t witness_program[40];
834 size_t witness_program_len;
835
836 char *addrz;
837 const char *bech32;
838
839 u8 addr_version;
840
841 if (ripemd160_from_base58(&addr_version, &destination.addr,
842 buffer + tok->start, tok->end - tok->start)) {
843 if (addr_version == chainparams->p2pkh_version) {
844 *scriptpubkey = scriptpubkey_p2pkh(ctx, &destination);
845 return ADDRESS_PARSE_SUCCESS;
846 } else if (addr_version == chainparams->p2sh_version) {
847 *scriptpubkey =
848 scriptpubkey_p2sh_hash(ctx, &destination.addr);
849 return ADDRESS_PARSE_SUCCESS;
850 } else {
851 return ADDRESS_PARSE_WRONG_NETWORK;
852 }
853 /* Insert other parsers that accept pointer+len here. */
854 return ADDRESS_PARSE_UNRECOGNIZED;
855 }
856
857 /* Generate null-terminated address. */
858 addrz = tal_dup_arr(tmpctx, char, buffer + tok->start, tok->end - tok->start, 1);
859 addrz[tok->end - tok->start] = '\0';
860
861 bech32 = segwit_addr_net_decode(&witness_version, witness_program,
862 &witness_program_len, addrz, chainparams);
863 if (bech32) {
864 bool witness_ok;
865
866 /* Only V0 has restricted lengths of witness programs */
867 if (witness_version == 0) {
868 witness_ok = (witness_program_len == 20 ||
869 witness_program_len == 32);
870 } else
871 witness_ok = true;
872
873 if (!witness_ok)
874 return ADDRESS_PARSE_UNRECOGNIZED;
875
876 if (!streq(bech32, chainparams->onchain_hrp))
877 return ADDRESS_PARSE_WRONG_NETWORK;
878
879 *scriptpubkey = scriptpubkey_witness_raw(ctx, witness_version,

Callers 9

parse_fallbackFunction · 0.85
param_bitcoin_addressFunction · 0.85
fallbacksFunction · 0.85
param_outputsFunction · 0.85
newaddr_sweep_doneFunction · 0.85
param_outputs_arrayFunction · 0.85
mw_after_newaddrFunction · 0.85

Calls 5

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

Tested by 1

fallbacksFunction · 0.68