Skip to content

Commit

Permalink
[gh-2295] fix move args.
Browse files Browse the repository at this point in the history
  • Loading branch information
Feliciss committed Jul 31, 2024
1 parent a36a7ad commit da38b2c
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 23 deletions.
2 changes: 1 addition & 1 deletion frameworks/rooch-framework/doc/bitcoin_address.md
Original file line number Diff line number Diff line change
Expand Up @@ -339,7 +339,7 @@ Empty address is a special address that is used to if we parse address failed fr



<pre><code><b>public</b> <b>fun</b> <a href="bitcoin_address.md#0x3_bitcoin_address_derive_multisig_xonly_pubkey_from_xonly_pubkeys">derive_multisig_xonly_pubkey_from_xonly_pubkeys</a>(public_keys: &<a href="">vector</a>&lt;<a href="">vector</a>&lt;u8&gt;&gt;, threshold: u64): <a href="">vector</a>&lt;u8&gt;
<pre><code><b>public</b> <b>fun</b> <a href="bitcoin_address.md#0x3_bitcoin_address_derive_multisig_xonly_pubkey_from_xonly_pubkeys">derive_multisig_xonly_pubkey_from_xonly_pubkeys</a>(public_keys: <a href="">vector</a>&lt;<a href="">vector</a>&lt;u8&gt;&gt;, threshold: u64): <a href="">vector</a>&lt;u8&gt;
</code></pre>


Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,7 @@ module rooch_framework::bitcoin_address {
public native fun verify_with_pk(bitcoin_addr: &BitcoinAddress, pk: &vector<u8>): bool;

// derive multisig xonly public key from public keys
public native fun derive_multisig_xonly_pubkey_from_xonly_pubkeys(public_keys: &vector<vector<u8>>, threshold: u64): vector<u8>;
public native fun derive_multisig_xonly_pubkey_from_xonly_pubkeys(public_keys: vector<vector<u8>>, threshold: u64): vector<u8>;

// derive bitcoin taproot address from the multisig xonly public key
public native fun derive_bitcoin_taproot_address_from_multisig_xonly_pubkey(xonly_pubkey: &vector<u8>): BitcoinAddress;
Expand Down Expand Up @@ -161,7 +161,7 @@ module rooch_framework::bitcoin_address {

#[test]
fun test_derive_multisig_xonly_pubkey_from_xonly_pubkeys_success() {
let expected_xonly_pubkey = x"ffa540e2d3df158dfb202fc1a2cbb20c4920ba35e8f75bb11101bfa47d71449a";
let expected_xonly_pubkey = x"7b6474bd9206ad07c0bc6b0ac90d43f6f232235c9e9cbf0c47775bf47ca9c402";
let pk_list = vector::empty<vector<u8>>();

let pk_1 = x"f9308a019258c31049344f85f89d5229b531c845836f99b08601f113bce036f9";
Expand All @@ -170,24 +170,19 @@ module rooch_framework::bitcoin_address {
vector::push_back(&mut pk_list, pk_1);
vector::push_back(&mut pk_list, pk_2);

// TODO: fix MISSING_DEPENDENCY (code 1021) error
let xonly_pubkey = derive_multisig_xonly_pubkey_from_xonly_pubkeys(&pk_list, 2);

std::debug::print(&xonly_pubkey);
std::debug::print(&expected_xonly_pubkey);
let xonly_pubkey = derive_multisig_xonly_pubkey_from_xonly_pubkeys(pk_list, 2);

assert!(expected_xonly_pubkey == xonly_pubkey, E_INVALID_KEY_EGG_CONTEXT);
}

// test covered from https://slowli.github.io/bech32-buffer/ using script version 1
#[test]
fun test_derive_bitcoin_taproot_address_from_multisig_xonly_pubkey_success() {
let xonly_pubkey = x"ffa540e2d3df158dfb202fc1a2cbb20c4920ba35e8f75bb11101bfa47d71449a";
let xonly_pubkey = x"7b6474bd9206ad07c0bc6b0ac90d43f6f232235c9e9cbf0c47775bf47ca9c402";

let bitcoin_addr = derive_bitcoin_taproot_address_from_multisig_xonly_pubkey(&xonly_pubkey);

let expected_bitcoin_addr = BitcoinAddress {
bytes: x"02010ca8363b3e074be0697eb46b5ac5a291d06ecbeb95cf16f685735147753f8b45",
bytes: x"020102f97a0a664c8493bfa28cfcf3450628bdc0ba7b3b0af2b57d4d057f15cb41f9",
};

assert!(expected_bitcoin_addr.bytes == bitcoin_addr.bytes, E_INVALID_XONLY_PUBKEY);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@ use crate::natives::rooch_framework::bitcoin_address::GasParameters;
crate::natives::gas_parameter::native::define_gas_parameters_for_natives!(GasParameters, "bitcoin_address", [
[.new.base, "parse.base", 1000 * MUL],
[.new.per_byte, "parse.per_byte", 30 * MUL],
[.verify_with_pk.base, "verify_bitcoin_address_with_public_key.base", 1000 * MUL],
[.verify_with_pk.per_byte, "verify_bitcoin_address_with_public_key.per_byte", 30 * MUL],
[.verify_with_pk.base, "verify_with_pk.base", 1000 * MUL],
[.verify_with_pk.per_byte, "verify_with_pk.per_byte", 30 * MUL],
[.derive_multisig_xonly_pubkey_from_xonly_pubkeys.base, optional "derive_multisig_xonly_pubkey_from_xonly_pubkeys.base", 1000 * MUL],
[.derive_multisig_xonly_pubkey_from_xonly_pubkeys.per_byte, optional "derive_multisig_xonly_pubkey_from_xonly_pubkeys.per_byte", 30 * MUL],
[.derive_bitcoin_taproot_address_from_multisig_xonly_pubkey.base, optional "derive_bitcoin_taproot_address_from_multisig_xonly_pubkey.base", 1000 * MUL],
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -133,7 +133,8 @@ pub fn derive_multisig_xonly_pubkey_from_xonly_pubkeys(
let threshold_bytes = pop_arg!(args, u64);
let pk_list = pop_arg!(args, Vec<Value>);

let mut cost = gas_params.base.unwrap() + gas_params.per_byte.unwrap() * NumBytes::new(threshold_bytes);
let mut cost =
gas_params.base.unwrap() + gas_params.per_byte.unwrap() * NumBytes::new(threshold_bytes);

if pk_list.len() < threshold_bytes as usize {
return Ok(NativeResult::err(cost, E_INVALID_THRESHOLD));
Expand Down Expand Up @@ -188,7 +189,8 @@ pub fn derive_bitcoin_taproot_address_from_multisig_xonly_pubkey(

let xonly_pubkey_ref = xonly_pubkey_bytes.as_bytes_ref();

let cost = gas_params.base.unwrap() + gas_params.per_byte.unwrap() * NumBytes::new(xonly_pubkey_ref.len() as u64);
let cost = gas_params.base.unwrap()
+ gas_params.per_byte.unwrap() * NumBytes::new(xonly_pubkey_ref.len() as u64);

let internal_key = match XOnlyPublicKey::from_slice(&xonly_pubkey_ref) {
Ok(xonly_pubkey) => xonly_pubkey,
Expand Down Expand Up @@ -250,7 +252,8 @@ impl GasParameters {
Self {
new: FromBytesGasParameters::zeros(),
verify_with_pk: FromBytesGasParameters::zeros(),
derive_multisig_xonly_pubkey_from_xonly_pubkeys: FromBytesGasParametersOptional::zeros(),
derive_multisig_xonly_pubkey_from_xonly_pubkeys: FromBytesGasParametersOptional::zeros(
),
derive_bitcoin_taproot_address_from_multisig_xonly_pubkey:
FromBytesGasParametersOptional::zeros(),
}
Expand All @@ -262,14 +265,15 @@ pub fn make_all(gas_params: GasParameters) -> impl Iterator<Item = (String, Nati
("parse", make_native(gas_params.new, parse)),
(
"verify_with_pk",
make_native(
gas_params.verify_with_pk,
verify_with_pk,
),
make_native(gas_params.verify_with_pk, verify_with_pk),
),
].to_vec();
]
.to_vec();

if !gas_params.derive_multisig_xonly_pubkey_from_xonly_pubkeys.is_empty() {
if !gas_params
.derive_multisig_xonly_pubkey_from_xonly_pubkeys
.is_empty()
{
natives.push((
"derive_multisig_xonly_pubkey_from_xonly_pubkeys",
make_native(
Expand All @@ -279,7 +283,10 @@ pub fn make_all(gas_params: GasParameters) -> impl Iterator<Item = (String, Nati
));
}

if !gas_params.derive_bitcoin_taproot_address_from_multisig_xonly_pubkey.is_empty() {
if !gas_params
.derive_bitcoin_taproot_address_from_multisig_xonly_pubkey
.is_empty()
{
natives.push((
"derive_bitcoin_taproot_address_from_multisig_xonly_pubkey",
make_native(
Expand Down

0 comments on commit da38b2c

Please sign in to comment.