Skip to content

Commit

Permalink
feat: P-926 support rest 4 bitcoin networks for brc20 contract (#2907)
Browse files Browse the repository at this point in the history
Co-authored-by: higherordertech <higherordertech>
  • Loading branch information
higherordertech authored Jul 19, 2024
1 parent 64c3357 commit 6d7bc4d
Show file tree
Hide file tree
Showing 10 changed files with 94 additions and 31 deletions.
Original file line number Diff line number Diff line change
@@ -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 <https://www.gnu.org/licenses/>.

// 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;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand All @@ -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();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand All @@ -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();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand All @@ -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();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand All @@ -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();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand All @@ -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();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand All @@ -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();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand All @@ -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();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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};

Expand Down Expand Up @@ -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(),
}
}
Expand Down Expand Up @@ -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]
Expand Down
9 changes: 8 additions & 1 deletion tee-worker/litentry/primitives/src/bitcoin_address.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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};
Expand Down Expand Up @@ -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()
}

0 comments on commit 6d7bc4d

Please sign in to comment.