| 52 | } |
| 53 | |
| 54 | bool decode_scriptpubkey_from_addr(const tal_t *ctx, |
| 55 | const struct chainparams *chainparams, |
| 56 | const char *address, |
| 57 | u8 **scriptpubkey) |
| 58 | { |
| 59 | struct bitcoin_address destination; |
| 60 | int witness_version; |
| 61 | /* segwit_addr_net_decode requires a buffer of size 40, and will |
| 62 | * not write to the buffer if the address is too long, so a buffer |
| 63 | * of fixed size 40 will not overflow. */ |
| 64 | uint8_t witness_program[40]; |
| 65 | size_t witness_program_len; |
| 66 | const char *bech32; |
| 67 | u8 addr_version; |
| 68 | |
| 69 | if (ripemd160_from_base58(&addr_version, &destination.addr, |
| 70 | address, strlen(address))) { |
| 71 | if (addr_version == chainparams->p2pkh_version) { |
| 72 | *scriptpubkey = scriptpubkey_p2pkh(ctx, &destination); |
| 73 | return true; |
| 74 | } else if (addr_version == chainparams->p2sh_version) { |
| 75 | *scriptpubkey = |
| 76 | scriptpubkey_p2sh_hash(ctx, &destination.addr); |
| 77 | return true; |
| 78 | } else { |
| 79 | return false; |
| 80 | } |
| 81 | /* Insert other parsers that accept pointer+len here. */ |
| 82 | return false; |
| 83 | } |
| 84 | |
| 85 | bech32 = segwit_addr_net_decode(&witness_version, witness_program, |
| 86 | &witness_program_len, address, |
| 87 | chainparams); |
| 88 | if (bech32) { |
| 89 | bool witness_ok; |
| 90 | |
| 91 | if (witness_version == 0) { |
| 92 | witness_ok = (witness_program_len == 20 || |
| 93 | witness_program_len == 32); |
| 94 | } else if (witness_version == 1) { |
| 95 | witness_ok = (witness_program_len == 32); |
| 96 | } else { |
| 97 | witness_ok = true; |
| 98 | } |
| 99 | |
| 100 | if (!witness_ok) |
| 101 | return false; |
| 102 | |
| 103 | if (!streq(bech32, chainparams->onchain_hrp)) |
| 104 | return false; |
| 105 | |
| 106 | *scriptpubkey = scriptpubkey_witness_raw(ctx, witness_version, |
| 107 | witness_program, |
| 108 | witness_program_len); |
| 109 | return true; |
| 110 | } |
| 111 |
no test coverage detected