Skip to content

Commit

Permalink
fix pubkey parsing
Browse files Browse the repository at this point in the history
  • Loading branch information
rajarshimaitra committed Apr 13, 2024
1 parent b618464 commit c776db9
Show file tree
Hide file tree
Showing 4 changed files with 43 additions and 75 deletions.
42 changes: 11 additions & 31 deletions src/swaps/bitcoinv2.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<Self, Error> {
let claim_script = ScriptBuf::from_hex(&create_swap_response.swap_tree.claim_leaf.output)?;
let refund_script =
Expand All @@ -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;
}
Expand All @@ -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(),
)));
Expand All @@ -105,30 +100,27 @@ 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 {
swap_type: SwapType::Submarine,
// 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<Self, Error> {
pub fn reverse_from_swap_resp(
reverse_response: &ReverseResp,
our_pubkey: PublicKey,
) -> Result<Self, Error> {
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)?;

Expand All @@ -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;
}
Expand All @@ -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(),
)));
Expand All @@ -177,25 +163,19 @@ 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 {
swap_type: SwapType::ReverseSubmarine,
// 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,
})
}

Expand Down
42 changes: 11 additions & 31 deletions src/swaps/liquidv2.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<Self, Error> {
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)?;
Expand All @@ -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;
}
Expand All @@ -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 {
Expand All @@ -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
Expand All @@ -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<Self, Error> {
pub fn reverse_from_swap_resp(
reverse_response: &ReverseResp,
our_pubkey: PublicKey,
) -> Result<Self, Error> {
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)?;

Expand All @@ -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;
}
Expand All @@ -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 {
Expand All @@ -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
Expand All @@ -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,
})
}
Expand Down
18 changes: 11 additions & 7 deletions tests/bitcoin_v2.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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);

Expand Down Expand Up @@ -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();
Expand All @@ -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);
Expand All @@ -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();
Expand Down
16 changes: 10 additions & 6 deletions tests/liquid_v2.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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);

Expand Down Expand Up @@ -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();
Expand All @@ -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);
Expand All @@ -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();
Expand Down

0 comments on commit c776db9

Please sign in to comment.