diff --git a/tee-worker/litentry/core/assertion-build/src/dynamic/contracts/token_holding_amount/brc20/BRC20.sol b/tee-worker/litentry/core/assertion-build/src/dynamic/contracts/token_holding_amount/brc20/BRC20.sol new file mode 100644 index 0000000000..22b4473ccb --- /dev/null +++ b/tee-worker/litentry/core/assertion-build/src/dynamic/contracts/token_holding_amount/brc20/BRC20.sol @@ -0,0 +1,35 @@ +// Copyright 2020-2024 Trust Computing GmbH. +// This file is part of Litentry. +// +// Litentry is free software: you can redistribute it and/or modify +// it under the terms of the GNU General Public License as published by +// the Free Software Foundation, either version 3 of the License, or +// (at your option) any later version. +// +// Litentry is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU General Public License for more details. +// +// You should have received a copy of the GNU General Public License +// along with Litentry. If not, see . + +// SPDX-License-Identifier: GPL-3.0-or-later + +pragma solidity ^0.8.8; + +import "../Constants.sol"; +import "../../libraries/Identities.sol"; + + +library BRC20 { + function getDefaultTokenNetworks() internal pure returns (uint32[] memory) { + uint32[] memory networks = new uint32[](5); + networks[0] = Web3Networks.BitcoinP2tr; + networks[1] = Web3Networks.BitcoinP2pkh; + networks[2] = Web3Networks.BitcoinP2sh; + networks[3] = Web3Networks.BitcoinP2wpkh; + networks[4] = Web3Networks.BitcoinP2wsh; + return networks; + } +} diff --git a/tee-worker/litentry/core/assertion-build/src/dynamic/contracts/token_holding_amount/brc20/Btcs.sol b/tee-worker/litentry/core/assertion-build/src/dynamic/contracts/token_holding_amount/brc20/Btcs.sol index 2f3b309b16..0fbbadb08a 100644 --- a/tee-worker/litentry/core/assertion-build/src/dynamic/contracts/token_holding_amount/brc20/Btcs.sol +++ b/tee-worker/litentry/core/assertion-build/src/dynamic/contracts/token_holding_amount/brc20/Btcs.sol @@ -18,8 +18,9 @@ pragma solidity ^0.8.8; -import "../Constants.sol"; import "../../libraries/Identities.sol"; +import "../Constants.sol"; +import "./BRC20.sol"; library Btcs { function getTokenName() internal pure returns (string memory) { @@ -39,9 +40,8 @@ library Btcs { ranges[8] = 800 * Constants.decimals_factor; return ranges; } + function getTokenNetworks() internal pure returns (uint32[] memory) { - uint32[] memory networks = new uint32[](1); - networks[0] = Web3Networks.BitcoinP2tr; - return networks; + return BRC20.getDefaultTokenNetworks(); } } diff --git a/tee-worker/litentry/core/assertion-build/src/dynamic/contracts/token_holding_amount/brc20/Cats.sol b/tee-worker/litentry/core/assertion-build/src/dynamic/contracts/token_holding_amount/brc20/Cats.sol index 0a4b5cb31f..228025ef8e 100644 --- a/tee-worker/litentry/core/assertion-build/src/dynamic/contracts/token_holding_amount/brc20/Cats.sol +++ b/tee-worker/litentry/core/assertion-build/src/dynamic/contracts/token_holding_amount/brc20/Cats.sol @@ -18,8 +18,9 @@ pragma solidity ^0.8.8; -import "../Constants.sol"; import "../../libraries/Identities.sol"; +import "../Constants.sol"; +import "./BRC20.sol"; library Cats { function getTokenName() internal pure returns (string memory) { @@ -38,9 +39,8 @@ library Cats { ranges[7] = 800000 * Constants.decimals_factor; return ranges; } + function getTokenNetworks() internal pure returns (uint32[] memory) { - uint32[] memory networks = new uint32[](1); - networks[0] = Web3Networks.BitcoinP2tr; - return networks; + return BRC20.getDefaultTokenNetworks(); } } diff --git a/tee-worker/litentry/core/assertion-build/src/dynamic/contracts/token_holding_amount/brc20/Long.sol b/tee-worker/litentry/core/assertion-build/src/dynamic/contracts/token_holding_amount/brc20/Long.sol index 60fbef5852..9d60fe0d54 100644 --- a/tee-worker/litentry/core/assertion-build/src/dynamic/contracts/token_holding_amount/brc20/Long.sol +++ b/tee-worker/litentry/core/assertion-build/src/dynamic/contracts/token_holding_amount/brc20/Long.sol @@ -18,8 +18,9 @@ pragma solidity ^0.8.8; -import "../Constants.sol"; import "../../libraries/Identities.sol"; +import "../Constants.sol"; +import "./BRC20.sol"; library Long { function getTokenName() internal pure returns (string memory) { @@ -39,9 +40,8 @@ library Long { ranges[8] = 3000 * Constants.decimals_factor; return ranges; } + function getTokenNetworks() internal pure returns (uint32[] memory) { - uint32[] memory networks = new uint32[](1); - networks[0] = Web3Networks.BitcoinP2tr; - return networks; + return BRC20.getDefaultTokenNetworks(); } } diff --git a/tee-worker/litentry/core/assertion-build/src/dynamic/contracts/token_holding_amount/brc20/Mmss.sol b/tee-worker/litentry/core/assertion-build/src/dynamic/contracts/token_holding_amount/brc20/Mmss.sol index 37dc39493a..4f515a0a25 100644 --- a/tee-worker/litentry/core/assertion-build/src/dynamic/contracts/token_holding_amount/brc20/Mmss.sol +++ b/tee-worker/litentry/core/assertion-build/src/dynamic/contracts/token_holding_amount/brc20/Mmss.sol @@ -18,8 +18,9 @@ pragma solidity ^0.8.8; -import "../Constants.sol"; import "../../libraries/Identities.sol"; +import "../Constants.sol"; +import "./BRC20.sol"; library Mmss { function getTokenName() internal pure returns (string memory) { @@ -39,9 +40,8 @@ library Mmss { ranges[8] = 2000 * Constants.decimals_factor; return ranges; } + function getTokenNetworks() internal pure returns (uint32[] memory) { - uint32[] memory networks = new uint32[](1); - networks[0] = Web3Networks.BitcoinP2tr; - return networks; + return BRC20.getDefaultTokenNetworks(); } } diff --git a/tee-worker/litentry/core/assertion-build/src/dynamic/contracts/token_holding_amount/brc20/Ordi.sol b/tee-worker/litentry/core/assertion-build/src/dynamic/contracts/token_holding_amount/brc20/Ordi.sol index a9e520015a..aeb0869782 100644 --- a/tee-worker/litentry/core/assertion-build/src/dynamic/contracts/token_holding_amount/brc20/Ordi.sol +++ b/tee-worker/litentry/core/assertion-build/src/dynamic/contracts/token_holding_amount/brc20/Ordi.sol @@ -18,8 +18,9 @@ pragma solidity ^0.8.8; -import "../Constants.sol"; import "../../libraries/Identities.sol"; +import "../Constants.sol"; +import "./BRC20.sol"; library Ordi { function getTokenName() internal pure returns (string memory) { @@ -38,9 +39,8 @@ library Ordi { ranges[7] = 500 * Constants.decimals_factor; return ranges; } + function getTokenNetworks() internal pure returns (uint32[] memory) { - uint32[] memory networks = new uint32[](1); - networks[0] = Web3Networks.BitcoinP2tr; - return networks; + return BRC20.getDefaultTokenNetworks(); } } diff --git a/tee-worker/litentry/core/assertion-build/src/dynamic/contracts/token_holding_amount/brc20/Rats.sol b/tee-worker/litentry/core/assertion-build/src/dynamic/contracts/token_holding_amount/brc20/Rats.sol index 9ea1bd83f3..94b53f3e7d 100644 --- a/tee-worker/litentry/core/assertion-build/src/dynamic/contracts/token_holding_amount/brc20/Rats.sol +++ b/tee-worker/litentry/core/assertion-build/src/dynamic/contracts/token_holding_amount/brc20/Rats.sol @@ -18,8 +18,9 @@ pragma solidity ^0.8.8; -import "../Constants.sol"; import "../../libraries/Identities.sol"; +import "../Constants.sol"; +import "./BRC20.sol"; library Rats { function getTokenName() internal pure returns (string memory) { @@ -39,9 +40,8 @@ library Rats { ranges[8] = 2000000 * Constants.decimals_factor; return ranges; } + function getTokenNetworks() internal pure returns (uint32[] memory) { - uint32[] memory networks = new uint32[](1); - networks[0] = Web3Networks.BitcoinP2tr; - return networks; + return BRC20.getDefaultTokenNetworks(); } } diff --git a/tee-worker/litentry/core/assertion-build/src/dynamic/contracts/token_holding_amount/brc20/Sats.sol b/tee-worker/litentry/core/assertion-build/src/dynamic/contracts/token_holding_amount/brc20/Sats.sol index 27f6772425..39e2de7b45 100644 --- a/tee-worker/litentry/core/assertion-build/src/dynamic/contracts/token_holding_amount/brc20/Sats.sol +++ b/tee-worker/litentry/core/assertion-build/src/dynamic/contracts/token_holding_amount/brc20/Sats.sol @@ -18,8 +18,9 @@ pragma solidity ^0.8.8; -import "../Constants.sol"; import "../../libraries/Identities.sol"; +import "../Constants.sol"; +import "./BRC20.sol"; library Sats { function getTokenName() internal pure returns (string memory) { @@ -39,9 +40,8 @@ library Sats { ranges[8] = 6000000000 * Constants.decimals_factor; return ranges; } + function getTokenNetworks() internal pure returns (uint32[] memory) { - uint32[] memory networks = new uint32[](1); - networks[0] = Web3Networks.BitcoinP2tr; - return networks; + return BRC20.getDefaultTokenNetworks(); } } diff --git a/tee-worker/litentry/core/evm-dynamic-assertions/src/precompiles/identity_to_string.rs b/tee-worker/litentry/core/evm-dynamic-assertions/src/precompiles/identity_to_string.rs index c7745056ae..8dc074f220 100644 --- a/tee-worker/litentry/core/evm-dynamic-assertions/src/precompiles/identity_to_string.rs +++ b/tee-worker/litentry/core/evm-dynamic-assertions/src/precompiles/identity_to_string.rs @@ -17,7 +17,9 @@ use crate::{failure_precompile_output, precompiles::PrecompileResult, success_precompile_output}; use base58::ToBase58; use blake2_rfc::blake2b::Blake2b; -use litentry_primitives::{p2pkh_address, p2sh_address, p2tr_address, p2wpkh_address, Web3Network}; +use litentry_primitives::{ + p2pkh_address, p2sh_address, p2tr_address, p2wpkh_address, p2wsh_address, Web3Network, +}; use ss58_registry::Ss58AddressFormat; use std::{format, string::String, vec, vec::Vec}; @@ -133,7 +135,9 @@ fn pubkey_to_address(network: u8, pubkey: &str) -> String { 11 => p2sh_address(pubkey), // BitcoinP2wpkh 12 => p2wpkh_address(pubkey), - // BitcoinP2wsh and others + // BitcoinP2wsh + 13 => p2wsh_address(pubkey), + // others _ => "".into(), } } @@ -228,6 +232,23 @@ pub mod test { )), result ); + + // BitcoinP2wsh + let encoded = encode(&[ + Token::Uint(Web3Network::BitcoinP2wsh.get_code().into()), + Token::Bytes(decode_hex(address.as_bytes().to_vec()).unwrap()), + ]); + + // when + let result = identity_to_string(encoded).unwrap(); + + // then + assert_eq!( + success_precompile_output(Token::String( + "bc1qvr0n2tgcevl26kx0vu76nujlju6fwkdzllv7qx5pz5ed3y8yf22st9hqmw".into() + )), + result + ); } #[test] diff --git a/tee-worker/litentry/primitives/src/bitcoin_address.rs b/tee-worker/litentry/primitives/src/bitcoin_address.rs index 32dcdcafcb..ae3f58f7cf 100644 --- a/tee-worker/litentry/primitives/src/bitcoin_address.rs +++ b/tee-worker/litentry/primitives/src/bitcoin_address.rs @@ -16,7 +16,8 @@ */ use bitcoin::{ - address::Address, key::PublicKey, network::Network, secp256k1::Secp256k1, XOnlyPublicKey, + address::Address, key::PublicKey, network::Network, secp256k1::Secp256k1, Script, + XOnlyPublicKey, }; use core::str::FromStr; use std::string::{String, ToString}; @@ -55,3 +56,9 @@ pub fn p2pkh_address(pubkey_string: &str) -> String { let address = Address::p2pkh(&pubkey, Network::Bitcoin); address.to_string() } + +pub fn p2wsh_address(pubkey_string: &str) -> String { + let script = Script::from_bytes(pubkey_string.as_bytes()); + let address = Address::p2wsh(script, Network::Bitcoin); + address.to_string() +}