@@ -20,7 +20,9 @@ use crate::encode::{self, Encodable, Decodable};
2020use crate :: hashes:: { hash_newtype, sha256, sha256d, Hash } ;
2121use crate :: fast_merkle_root:: fast_merkle_root;
2222use secp256k1_zkp:: Tag ;
23+ use crate :: genesis:: { commit_to_custom_network_parameters, NetworkParams } ;
2324use crate :: transaction:: OutPoint ;
25+ use crate :: Txid ;
2426
2527/// The zero hash.
2628const ZERO32 : [ u8 ; 32 ] = [
@@ -81,6 +83,14 @@ impl AssetId {
8183 0x3d , 0x1c , 0x04 , 0xed , 0xe9 , 0x79 , 0x02 , 0x6f ,
8284 ] ) ;
8385
86+ /// The asset ID for L-BTC, Bitcoin on the Liquidtestnet network.
87+ pub const LIQUIDTESTNET_BTC : AssetId = AssetId ( [
88+ 0x49 , 0x9a , 0x81 , 0x85 , 0x45 , 0xf6 , 0xba , 0xe3 ,
89+ 0x9f , 0xc0 , 0x3b , 0x63 , 0x7f , 0x2a , 0x4e , 0x1e ,
90+ 0x64 , 0xe5 , 0x90 , 0xca , 0xc1 , 0xbc , 0x3a , 0x6f ,
91+ 0x6d , 0x71 , 0xaa , 0x44 , 0x43 , 0x65 , 0x4c , 0x14 ,
92+ ] ) ;
93+
8494 /// Generate the asset entropy from the issuance prevout and the contract hash.
8595 pub fn generate_asset_entropy (
8696 prevout : OutPoint ,
@@ -137,6 +147,30 @@ impl AssetId {
137147 pub fn into_tag ( self ) -> Tag {
138148 self . 0 . into ( )
139149 }
150+
151+ /// Pegged asset id for given network parameters
152+ pub fn pegged_asset_id_for_network_params ( params : & NetworkParams ) -> AssetId {
153+ match params. network_id . as_str ( ) {
154+ "liquidv1" => Self :: LIQUID_BTC ,
155+ "liquidtestnet" => Self :: LIQUIDTESTNET_BTC ,
156+ _ => {
157+ // Else calculate the asset_id
158+ Self :: pegged_asset_id_for_params_and_parent_chain_hash (
159+ params,
160+ bitcoin:: Network :: Regtest . chain_hash ( ) ,
161+ )
162+ }
163+ }
164+ }
165+
166+ /// Calculate the `AssetId` for the pegged asset for a given set of network parameters assuming
167+ /// a Regtest parent network
168+ fn pegged_asset_id_for_params_and_parent_chain_hash ( params : & NetworkParams , parent_chainhash : bitcoin:: blockdata:: constants:: ChainHash ) -> AssetId {
169+ let commit = commit_to_custom_network_parameters ( params) ;
170+ let asset_outpoint = OutPoint :: new ( Txid :: from_slice ( commit. as_slice ( ) ) . expect ( "txid" ) , 0 ) ;
171+ let asset_entropy = AssetId :: generate_asset_entropy ( asset_outpoint, ContractHash :: from_slice ( parent_chainhash. to_bytes ( ) . as_slice ( ) ) . unwrap ( ) ) ;
172+ AssetId :: from_entropy ( asset_entropy)
173+ }
140174}
141175
142176impl Encodable for AssetId {
@@ -155,6 +189,7 @@ impl Decodable for AssetId {
155189mod test {
156190 use super :: * ;
157191 use std:: str:: FromStr ;
192+ use bitcoin:: constants:: ChainHash ;
158193
159194 #[ test]
160195 fn example_elements_core ( ) {
@@ -259,4 +294,52 @@ mod test {
259294 "6f0279e9ed041c3d710a9f57d0c02928416460c4b722ae3457a11eec381c526d" ,
260295 ) ;
261296 }
297+
298+ #[ test]
299+ fn liquid_asset_ids ( ) {
300+ // Testing the two most common Regtest networks using in Liquid and CLN codebases
301+ let network_params = NetworkParams :: custom_network ( "elementsregtest" . to_string ( ) , None , None , None ) ;
302+ let asset_id = AssetId :: pegged_asset_id_for_params_and_parent_chain_hash (
303+ & network_params,
304+ bitcoin:: Network :: Regtest . chain_hash ( ) ,
305+ ) ;
306+
307+ let elementsregtest_asset_id = AssetId ( [
308+ 0x23 , 0x0f , 0x4f , 0x5d , 0x4b , 0x7c , 0x6f , 0xa8 , 0x45 , 0x80 , 0x6e , 0xe4 ,
309+ 0xf6 , 0x77 , 0x13 , 0x45 , 0x9e , 0x1b , 0x69 , 0xe8 , 0xe6 , 0x0f , 0xce , 0xe2 ,
310+ 0xe4 , 0x94 , 0x0c , 0x7a , 0x0d , 0x5d , 0xe1 , 0xb2 ,
311+ ] ) ;
312+
313+ assert_eq ! ( asset_id, elementsregtest_asset_id) ;
314+
315+ let network_params = NetworkParams :: custom_network ( "liquid-regtest" . to_string ( ) , None , None , None ) ;
316+ let asset_id = AssetId :: pegged_asset_id_for_params_and_parent_chain_hash (
317+ & network_params,
318+ bitcoin:: Network :: Regtest . chain_hash ( ) ,
319+ ) ;
320+
321+ let liquid_regtest_assetid = AssetId ( [
322+ 0x5c , 0xe7 , 0xb9 , 0x63 , 0xd3 , 0x7f , 0x8f , 0x2d , 0x51 , 0xca , 0xfb , 0xba ,
323+ 0x92 , 0x8a , 0xaa , 0x9e , 0x22 , 0x0b , 0x8b , 0xbc , 0x66 , 0x05 , 0x71 , 0x49 ,
324+ 0x9c , 0x03 , 0x62 , 0x8a , 0x38 , 0x51 , 0xb8 , 0xce ,
325+ ] ) ;
326+
327+ assert_eq ! ( asset_id, liquid_regtest_assetid) ;
328+
329+ let liquidv1_params = NetworkParams :: liquidv1 ( ) ;
330+ let asset_id = AssetId :: pegged_asset_id_for_params_and_parent_chain_hash (
331+ & liquidv1_params,
332+ bitcoin:: Network :: Bitcoin . chain_hash ( ) ,
333+ ) ;
334+
335+ assert_eq ! ( asset_id, AssetId :: LIQUID_BTC ) ;
336+
337+ let liquidtestnet_params = NetworkParams :: liquidtestnet ( ) ;
338+ let asset_id = AssetId :: pegged_asset_id_for_params_and_parent_chain_hash (
339+ & liquidtestnet_params,
340+ ChainHash :: from ( [ 0u8 ; 32 ] ) ,
341+ ) ;
342+
343+ assert_eq ! ( asset_id, AssetId :: LIQUIDTESTNET_BTC ) ;
344+ }
262345}
0 commit comments