Skip to content

Commit

Permalink
feat (app): Add nested-segwit support
Browse files Browse the repository at this point in the history
test nested-segwit digest for 1 inputs and 2 output transactions
test nested-segwit digest for 2 inputs and 2 output transactions
  • Loading branch information
rxbryan committed Aug 8, 2024
1 parent 7ad67f2 commit 4cc3cd3
Show file tree
Hide file tree
Showing 5 changed files with 385 additions and 9 deletions.
11 changes: 10 additions & 1 deletion apps/btc_family/btc_pub_key.c
Original file line number Diff line number Diff line change
Expand Up @@ -204,7 +204,16 @@ static size_t btc_get_address(const uint8_t *seed,
case NON_SEGWIT:
hdnode_get_address(&node, g_btc_app->p2pkh_addr_ver, addr, 35);
break;
// TODO: add support for taproot and segwit
case PURPOSE_SEGWIT:
ecdsa_get_address_segwit_p2sh(node.public_key,
g_btc_app->p2sh_addr_ver,
node.curve->hasher_pubkey,
node.curve->hasher_base58,
addr,
36);
break;

// TODO: add support for taproot
default:
break;
}
Expand Down
18 changes: 17 additions & 1 deletion apps/btc_family/btc_txn.c
Original file line number Diff line number Diff line change
Expand Up @@ -648,11 +648,27 @@ static bool sign_input(scrip_sig_t *signatures) {
status = true;
for (int idx = 0; idx < btc_txn_context->metadata.input_count; idx++) {
// generate the input digest and respective private key
status = btc_digest_input(btc_txn_context, idx, buffer);
memcpy(&t_node, &node, sizeof(HDNode));
hdnode_private_ckd(&t_node, btc_txn_context->inputs[idx].change_index);
hdnode_private_ckd(&t_node, btc_txn_context->inputs[idx].address_index);
hdnode_fill_public_key(&t_node);

// detect input type
btc_sign_txn_input_script_pub_key_t *script =
&btc_txn_context->inputs[idx].script_pub_key;
btc_script_type_e type = btc_get_script_type(script->bytes, script->size);
if (SCRIPT_TYPE_P2SH == type) {
// replace BIP16 scriptpubkey with redeemscript(P2WPKH)
uint8_t buf[22] = {0};
buf[0] = 0; // version byte
buf[1] = 20; // push 20 bytes
ecdsa_get_pubkeyhash(
t_node.public_key, t_node.curve->hasher_pubkey, buf + 2);
memcpy(btc_txn_context->inputs[idx].script_pub_key.bytes, buf, 22);
btc_txn_context->inputs[idx].script_pub_key.size = 22;
}

status = btc_digest_input(btc_txn_context, idx, buffer);
ecdsa_sign_digest(
curve, t_node.private_key, buffer, signatures[idx].bytes, NULL, NULL);
signatures[idx].size = btc_sig_to_script_sig(
Expand Down
1 change: 0 additions & 1 deletion apps/btc_family/btc_txn_helpers.c
Original file line number Diff line number Diff line change
Expand Up @@ -670,7 +670,6 @@ bool btc_digest_input(const btc_txn_context_t *context,
btc_sign_txn_input_script_pub_key_t *script =
&context->inputs[index].script_pub_key;
btc_script_type_e type = btc_get_script_type(script->bytes, script->size);

if (SCRIPT_TYPE_P2WPKH == type) {
// segwit digest calculation; could fail if segwit_cache not filled
status = calculate_p2wpkh_digest(context, index, digest);
Expand Down
Loading

0 comments on commit 4cc3cd3

Please sign in to comment.