From c776db93406f5a6cee7e6690cb1e6a53c73b6869 Mon Sep 17 00:00:00 2001 From: rajarshimaitra Date: Sat, 13 Apr 2024 12:48:57 +0530 Subject: [PATCH] fix pubkey parsing --- src/swaps/bitcoinv2.rs | 42 +++++++++++------------------------------- src/swaps/liquidv2.rs | 42 +++++++++++------------------------------- tests/bitcoin_v2.rs | 18 +++++++++++------- tests/liquid_v2.rs | 16 ++++++++++------ 4 files changed, 43 insertions(+), 75 deletions(-) diff --git a/src/swaps/bitcoinv2.rs b/src/swaps/bitcoinv2.rs index 2d5f3ab..24f4f62 100644 --- a/src/swaps/bitcoinv2.rs +++ b/src/swaps/bitcoinv2.rs @@ -55,6 +55,7 @@ impl BtcSwapScriptV2 { /// Create the struct from a submarine swap from create swap response. pub fn submarine_from_swap_resp( create_swap_response: &CreateSwapResponse, + our_pubkey: PublicKey, ) -> Result { let claim_script = ScriptBuf::from_hex(&create_swap_response.swap_tree.claim_leaf.output)?; let refund_script = @@ -65,17 +66,13 @@ impl BtcSwapScriptV2 { let mut last_op = OP_0; let mut hashlock = None; - let mut reciever_pubkey = None; let mut timelock = None; - let mut sender_pubkey = None; for instruction in claim_instructions { match instruction { Ok(Instruction::PushBytes(bytes)) => { if bytes.len() == 20 { hashlock = Some(hash160::Hash::from_slice(bytes.as_bytes())?); - } else if bytes.len() == 32 { - reciever_pubkey = Some(PublicKey::from_slice(bytes.as_bytes())?); } else { continue; } @@ -88,9 +85,7 @@ impl BtcSwapScriptV2 { match instruction { Ok(Instruction::Op(opcode)) => last_op = opcode, Ok(Instruction::PushBytes(bytes)) => { - if bytes.len() == 32 { - sender_pubkey = Some(PublicKey::from_slice(bytes.as_bytes())?); - } else if last_op == OP_CHECKSIGVERIFY { + if last_op == OP_CHECKSIGVERIFY { timelock = Some(LockTime::from_consensus(bytes_to_u32_little_endian( &bytes.as_bytes(), ))); @@ -105,15 +100,9 @@ impl BtcSwapScriptV2 { let hashlock = hashlock.ok_or_else(|| Error::Protocol("No hashlock provided".to_string()))?; - let sender_pubkey = sender_pubkey - .ok_or_else(|| Error::Protocol("No sender_pubkey provided".to_string()))?; - let timelock = timelock.ok_or_else(|| Error::Protocol("No timelock provided".to_string()))?; - let receiver_pubkey = reciever_pubkey - .ok_or_else(|| Error::Protocol("No receiver_pubkey provided".to_string()))?; - let funding_addrs = Address::from_str(&create_swap_response.address)?.assume_checked(); Ok(BtcSwapScriptV2 { @@ -121,14 +110,17 @@ impl BtcSwapScriptV2 { // swap_id: create_swap_response.id.clone(), funding_addrs: Some(funding_addrs), hashlock: hashlock, - receiver_pubkey: receiver_pubkey, + receiver_pubkey: create_swap_response.claim_public_key, locktime: timelock, - sender_pubkey: sender_pubkey, + sender_pubkey: our_pubkey, }) } /// Create the struct from a reverse swap create request. - pub fn reverse_from_swap_resp(reverse_response: &ReverseResp) -> Result { + pub fn reverse_from_swap_resp( + reverse_response: &ReverseResp, + our_pubkey: PublicKey, + ) -> Result { let claim_script = ScriptBuf::from_hex(&reverse_response.swap_tree.claim_leaf.output)?; let refund_script = ScriptBuf::from_hex(&reverse_response.swap_tree.refund_leaf.output)?; @@ -137,17 +129,13 @@ impl BtcSwapScriptV2 { let mut last_op = OP_0; let mut hashlock = None; - let mut receiver_pubkey = None; let mut timelock = None; - let mut sender_pubkey = None; for instruction in claim_instructions { match instruction { Ok(Instruction::PushBytes(bytes)) => { if bytes.len() == 20 { hashlock = Some(hash160::Hash::from_slice(bytes.as_bytes())?); - } else if bytes.len() == 32 { - receiver_pubkey = Some(PublicKey::from_slice(bytes.as_bytes())?); } else { continue; } @@ -160,9 +148,7 @@ impl BtcSwapScriptV2 { match instruction { Ok(Instruction::Op(opcode)) => last_op = opcode, Ok(Instruction::PushBytes(bytes)) => { - if bytes.len() == 32 { - sender_pubkey = Some(PublicKey::from_slice(bytes.as_bytes())?); - } else if last_op == OP_CHECKSIGVERIFY { + if last_op == OP_CHECKSIGVERIFY { timelock = Some(LockTime::from_consensus(bytes_to_u32_little_endian( &bytes.as_bytes(), ))); @@ -177,15 +163,9 @@ impl BtcSwapScriptV2 { let hashlock = hashlock.ok_or_else(|| Error::Protocol("No hashlock provided".to_string()))?; - let sender_pubkey = sender_pubkey - .ok_or_else(|| Error::Protocol("No sender_pubkey provided".to_string()))?; - let timelock = timelock.ok_or_else(|| Error::Protocol("No timelock provided".to_string()))?; - let receiver_pubkey = receiver_pubkey - .ok_or_else(|| Error::Protocol("No receiver_pubkey provided".to_string()))?; - let funding_addrs = Address::from_str(&reverse_response.lockup_address)?.assume_checked(); Ok(BtcSwapScriptV2 { @@ -193,9 +173,9 @@ impl BtcSwapScriptV2 { // swap_id: reverse_response.id.clone(), funding_addrs: Some(funding_addrs), hashlock: hashlock, - receiver_pubkey: receiver_pubkey, + receiver_pubkey: our_pubkey, locktime: timelock, - sender_pubkey: sender_pubkey, + sender_pubkey: reverse_response.refund_public_key, }) } diff --git a/src/swaps/liquidv2.rs b/src/swaps/liquidv2.rs index 30f2b04..b778f49 100644 --- a/src/swaps/liquidv2.rs +++ b/src/swaps/liquidv2.rs @@ -63,6 +63,7 @@ impl LBtcSwapScriptV2 { /// Create the struct from a submarine swap from create swap response. pub fn submarine_from_swap_resp( create_swap_response: &CreateSwapResponse, + our_pubkey: PublicKey, ) -> Result { let claim_script = Script::from_str(&create_swap_response.swap_tree.claim_leaf.output)?; let refund_script = Script::from_str(&create_swap_response.swap_tree.refund_leaf.output)?; @@ -72,17 +73,13 @@ impl LBtcSwapScriptV2 { let mut last_op = OP_0NOTEQUAL; let mut hashlock = None; - let mut reciever_pubkey = None; let mut locktime = None; - let mut sender_pubkey = None; for instruction in claim_instructions { match instruction { Ok(Instruction::PushBytes(bytes)) => { if bytes.len() == 20 { hashlock = Some(hash160::Hash::from_slice(bytes)?); - } else if bytes.len() == 32 { - reciever_pubkey = Some(PublicKey::from_slice(bytes)?); } else { continue; } @@ -95,9 +92,7 @@ impl LBtcSwapScriptV2 { match instruction { Ok(Instruction::Op(opcode)) => last_op = opcode, Ok(Instruction::PushBytes(bytes)) => { - if bytes.len() == 32 { - sender_pubkey = Some(PublicKey::from_slice(bytes)?); - } else if last_op == OP_CHECKSIGVERIFY { + if last_op == OP_CHECKSIGVERIFY { locktime = Some(LockTime::from_consensus(bytes_to_u32_little_endian(&bytes))); } else { @@ -111,15 +106,9 @@ impl LBtcSwapScriptV2 { let hashlock = hashlock.ok_or_else(|| Error::Protocol("No hashlock provided".to_string()))?; - let sender_pubkey = sender_pubkey - .ok_or_else(|| Error::Protocol("No sender_pubkey provided".to_string()))?; - let locktime = locktime.ok_or_else(|| Error::Protocol("No timelock provided".to_string()))?; - let receiver_pubkey = reciever_pubkey - .ok_or_else(|| Error::Protocol("No receiver_pubkey provided".to_string()))?; - let funding_addrs = Address::from_str(&create_swap_response.address)?; let blinding_str = create_swap_response @@ -132,15 +121,18 @@ impl LBtcSwapScriptV2 { swap_type: SwapType::Submarine, funding_addrs: Some(funding_addrs), hashlock, - receiver_pubkey, + receiver_pubkey: create_swap_response.claim_public_key, locktime, - sender_pubkey, + sender_pubkey: our_pubkey, blinding_key, }) } /// Create the struct from a reverse swap create request. - pub fn reverse_from_swap_resp(reverse_response: &ReverseResp) -> Result { + pub fn reverse_from_swap_resp( + reverse_response: &ReverseResp, + our_pubkey: PublicKey, + ) -> Result { let claim_script = Script::from_str(&reverse_response.swap_tree.claim_leaf.output)?; let refund_script = Script::from_str(&reverse_response.swap_tree.refund_leaf.output)?; @@ -149,17 +141,13 @@ impl LBtcSwapScriptV2 { let mut last_op = OP_0NOTEQUAL; let mut hashlock = None; - let mut reciever_pubkey = None; let mut locktime = None; - let mut sender_pubkey = None; for instruction in claim_instructions { match instruction { Ok(Instruction::PushBytes(bytes)) => { if bytes.len() == 20 { hashlock = Some(hash160::Hash::from_slice(bytes)?); - } else if bytes.len() == 32 { - reciever_pubkey = Some(PublicKey::from_slice(bytes)?); } else { continue; } @@ -172,9 +160,7 @@ impl LBtcSwapScriptV2 { match instruction { Ok(Instruction::Op(opcode)) => last_op = opcode, Ok(Instruction::PushBytes(bytes)) => { - if bytes.len() == 32 { - sender_pubkey = Some(PublicKey::from_slice(bytes)?); - } else if last_op == OP_CHECKSIGVERIFY { + if last_op == OP_CHECKSIGVERIFY { locktime = Some(LockTime::from_consensus(bytes_to_u32_little_endian(&bytes))); } else { @@ -188,15 +174,9 @@ impl LBtcSwapScriptV2 { let hashlock = hashlock.ok_or_else(|| Error::Protocol("No hashlock provided".to_string()))?; - let sender_pubkey = sender_pubkey - .ok_or_else(|| Error::Protocol("No sender_pubkey provided".to_string()))?; - let locktime = locktime.ok_or_else(|| Error::Protocol("No timelock provided".to_string()))?; - let receiver_pubkey = reciever_pubkey - .ok_or_else(|| Error::Protocol("No receiver_pubkey provided".to_string()))?; - let funding_addrs = Address::from_str(&reverse_response.lockup_address)?; let blinding_str = reverse_response @@ -209,9 +189,9 @@ impl LBtcSwapScriptV2 { swap_type: SwapType::Submarine, funding_addrs: Some(funding_addrs), hashlock, - receiver_pubkey, + receiver_pubkey: our_pubkey, locktime, - sender_pubkey, + sender_pubkey: reverse_response.refund_public_key, blinding_key, }) } diff --git a/tests/bitcoin_v2.rs b/tests/bitcoin_v2.rs index 9be6e00..0ad053f 100644 --- a/tests/bitcoin_v2.rs +++ b/tests/bitcoin_v2.rs @@ -37,7 +37,7 @@ fn bitcoin_v2_submarine() { }; // Set a new invoice string and refund address for each test. - let invoice = "lntb650u1pjut6cfpp5h7dgn6wghmsm8dfky9cjzrlyf5c2xaszk2lxamfqm2w4eurevpwqdq8d3skk6qxqyjw5qcqp2sp5nyk5mtwjf250uv0uf2l2trhyycefndu868dya04zlrvw5gvaev2srzjq2gyp9za7vc7vd8m59fvu63pu00u4pak35n4upuv4mhyw5l586dvkf6vkyqq20gqqqqqqqqpqqqqqzsqqc9qyyssqva5tvj5gxfsdmc84hvreme8djgwj3rqr37kwtsa6qttgwzhe7s0yfy482afyje45ppualmatfwnmlmk2py7wc7l3l849jl7vdpa86aqqxmqmws".to_string(); + let invoice = "lntb500u1pnp5fcppp5cyk7eadg2qvjtvzn7g8mgu53t0ecul5ds6ddwxdn5zc3lzu9w8rsdqgv9ekgumyxqyjw5qcqp2sp5ejghc2nlheeqqdr5cx2euklk3npj8wmmmrmmvlsuq2jrm3h7nw0srzjq2gyp9za7vc7vd8m59fvu63pu00u4pak35n4upuv4mhyw5l586dvkf6vkyqq20gqqqqqqqqpqqqqqzsqqc9qyyssqcpa468v9u58qu32u9lmejca74hueguu6ffgucka4yrk2u6a5gdrkd96lunfdw2ls43y8qpgcj3z5647rq5skxf56vrhyj6jn03zyssspjh4njf".to_string(); let refund_address = "tb1qq20a7gqewc0un9mxxlqyqwn7ut7zjrj9y3d0mu".to_string(); // Initiate the swap with Boltz @@ -56,7 +56,9 @@ fn bitcoin_v2_submarine() { log::debug!("Swap Response: {:?}", create_swap_response); - let swap_script = BtcSwapScriptV2::submarine_from_swap_resp(&create_swap_response).unwrap(); + let swap_script = + BtcSwapScriptV2::submarine_from_swap_resp(&create_swap_response, refund_public_key) + .unwrap(); log::debug!("Created Swap Script. : {:?}", swap_script); @@ -184,6 +186,10 @@ fn bitcoin_v2_reverse() { let preimage = Preimage::new(); let our_keys = Keypair::new(&secp, &mut thread_rng()); let invoice_amount = 100000; + let claim_public_key = PublicKey { + compressed: true, + inner: our_keys.public_key(), + }; // Give a valid claim address or else funds will be lost. let claim_address = "tb1qq20a7gqewc0un9mxxlqyqwn7ut7zjrj9y3d0mu".to_string(); @@ -193,10 +199,7 @@ fn bitcoin_v2_reverse() { from: "BTC".to_string(), to: "BTC".to_string(), preimage_hash: preimage.sha256, - claim_public_key: PublicKey { - compressed: true, - inner: our_keys.public_key(), - }, + claim_public_key, }; let boltz_api_v2 = BoltzApiClientV2::new(BOLTZ_TESTNET_URL); @@ -205,7 +208,8 @@ fn bitcoin_v2_reverse() { log::debug!("Got Reverse swap response: {:?}", reverse_resp); - let swap_script = BtcSwapScriptV2::reverse_from_swap_resp(&reverse_resp).unwrap(); + let swap_script = + BtcSwapScriptV2::reverse_from_swap_resp(&reverse_resp, claim_public_key).unwrap(); // Subscribe to wss status updates let mut socket = boltz_api_v2.connect_ws().unwrap(); diff --git a/tests/liquid_v2.rs b/tests/liquid_v2.rs index 7f85f8b..b42d678 100644 --- a/tests/liquid_v2.rs +++ b/tests/liquid_v2.rs @@ -56,7 +56,9 @@ fn liquid_v2_submarine() { log::debug!("Swap Response: {:?}", create_swap_response); - let swap_script = LBtcSwapScriptV2::submarine_from_swap_resp(&create_swap_response).unwrap(); + let swap_script = + LBtcSwapScriptV2::submarine_from_swap_resp(&create_swap_response, refund_public_key) + .unwrap(); log::debug!("Created Swap Script. : {:?}", swap_script); @@ -182,6 +184,10 @@ fn bitcoin_v2_reverse() { let preimage = Preimage::new(); let our_keys = Keypair::new(&secp, &mut thread_rng()); let invoice_amount = 100000; + let claim_public_key = PublicKey { + compressed: true, + inner: our_keys.public_key(), + }; // Give a valid claim address or else funds will be lost. let claim_address = "tb1qq20a7gqewc0un9mxxlqyqwn7ut7zjrj9y3d0mu".to_string(); @@ -191,10 +197,7 @@ fn bitcoin_v2_reverse() { from: "BTC".to_string(), to: "BTC".to_string(), preimage_hash: preimage.sha256, - claim_public_key: PublicKey { - compressed: true, - inner: our_keys.public_key(), - }, + claim_public_key, }; let boltz_api_v2 = BoltzApiClientV2::new(BOLTZ_TESTNET_URL); @@ -203,7 +206,8 @@ fn bitcoin_v2_reverse() { log::debug!("Got Reverse swap response: {:?}", reverse_resp); - let swap_script = LBtcSwapScriptV2::reverse_from_swap_resp(&reverse_resp).unwrap(); + let swap_script = + LBtcSwapScriptV2::reverse_from_swap_resp(&reverse_resp, claim_public_key).unwrap(); // Subscribe to wss status updates let mut socket = boltz_api_v2.connect_ws().unwrap();