From e1fe6391422dc67ecab57eab4c7ce87b4f0d57af Mon Sep 17 00:00:00 2001 From: Antonio Russo Date: Thu, 16 Jan 2025 17:38:01 +0100 Subject: [PATCH 1/7] feat: support multi-ecosystem derivation and sui addresses --- deposit_addr.go | 58 +++++++++++++++++++------------------------- ecosystem_tag.go | 12 +++++++++ evm_deposit_addr.go | 36 +++++++++++++++++++++++++++ segwit_tweak_test.go | 2 ++ sui_deposit_addr.go | 56 ++++++++++++++++++++++++++++++++++++++++++ tweak_bytes.go | 22 +++++++++++++---- 6 files changed, 148 insertions(+), 38 deletions(-) create mode 100644 ecosystem_tag.go create mode 100644 evm_deposit_addr.go create mode 100644 sui_deposit_addr.go diff --git a/deposit_addr.go b/deposit_addr.go index c62ecc4..e71cb86 100644 --- a/deposit_addr.go +++ b/deposit_addr.go @@ -12,15 +12,6 @@ import ( type Address = eth.Address type Sha256 = hash.Hash -// Chain type tags -// -// These tags are used to distinguish deposit addresses for different chain types -const ( - ChainIdSize int = 32 - EvmTag uint8 = 0 - // TODO define more chain-type identifiers -) - const ( DepositAddrTag = "LombardDepositAddr" ) @@ -44,17 +35,17 @@ func depositHasher() Sha256 { return h } -// EvmDepositTweak Compute the tweak bytes for an EVM deposit address. +// depositTweak Compute the tweak bytes for a deposit address. // -// This is defined as +// This is generally defined as // -// taggedHash( AuxData || EvmTag || ChainId || LBTCAddress || WalletAddress ) +// taggedHash( AuxData || EcosystemTag || ChainId || LBTCAddress || WalletAddress ) // // where 'taggedHash' is a sha256 instance as returned by 'depositHasher()', -// 'EvmTag' is defined above, 'ChainId' is serialized as 32 big-endian bytes, -// LBTCAddress and WalletAddress are 20-byte EVM addresses, and AuxData is a -// 32-byte value encoding chain-agnostic auxiliary data. -func EvmDepositTweak(lbtcContract, wallet Address, chainId, auxData []byte) ([]byte, error) { +// 'EcosystemTag' is defined in the dedicated file as a type, 'ChainId' is serialized as 32 +// big-endian bytes, LBTCAddress and WalletAddress are byte arrays representing the respective addresses +// on the selected chain, and AuxData is a 32-byte value encoding chain-agnostic auxiliary data. +func depositTweak(eTag EcosystemTag, lbtcContract, wallet, chainId, auxData []byte) ([]byte, error) { if len(auxData) != AuxDataSize { return nil, errors.Errorf("wrong size for auxData (got %v, want %v)", len(auxData), AuxDataSize) } @@ -67,31 +58,32 @@ func EvmDepositTweak(lbtcContract, wallet Address, chainId, auxData []byte) ([]b // aux data (32 bytes) h.Write(auxData[:]) - // EVM tag (1 byte) - h.Write([]byte{EvmTag}) + // ecosystem tag (1 byte) + h.Write([]byte{eTag}) - // EVM chain-id (32 bytes) + // chain-id (32 bytes) as defined by Lombard documentation // we zero-pad if `chainId` is less than 32 bytes and error if it is more. h.Write(chainId[:]) - // LBTC contract address (20 bytes) - h.Write(lbtcContract.Bytes()) + // LBTC contract address + h.Write(lbtcContract) - // Destination wallet address (20 bytes) - h.Write(wallet.Bytes()) + // Destination wallet address + h.Write(wallet) return h.Sum(nil), nil } -// EvmDepositSegwitPubkey Compute the segwit public key to be used for an EVM deposit. +// depositSegwitPubkey Compute the segwit public key to be used for a deposit. // +// - 'eTag' is the ecosystem tag to select the // - 'pk' is the base (untweaked) public key to tweak -// - 'lbtcContract' is the EVM address of the destination LBTC bridge contract -// - 'wallet' is the EVM address that will claim this deposit -// - 'chainId' is the chain id for the target EVM chain -func EvmDepositSegwitPubkey(pk *PublicKey, lbtcContract, wallet Address, chainId, auxData []byte) (*PublicKey, error) { +// - 'lbtcContract' is the address of the LBTC contract or object on the destination chain +// - 'wallet' is the address that will claim the deposit on the destination chain +// - 'chainId' is the chain id for the target chain as defined in the Lombard documentation +func depositSegwitPubkey(eTag EcosystemTag, pk *PublicKey, lbtcContract, wallet, chainId, auxData []byte) (*PublicKey, error) { // compute tweak bytes - tweakBytes, err := EvmDepositTweak(lbtcContract, wallet, chainId, auxData) + tweakBytes, err := depositTweak(eTag, lbtcContract, wallet, chainId, auxData) if err != nil { return nil, err } @@ -99,11 +91,11 @@ func EvmDepositSegwitPubkey(pk *PublicKey, lbtcContract, wallet Address, chainId return TweakPublicKey(pk, tweakBytes) } -// EvmDepositSegwitAddr Compute the segwit deposit address to be used for an EVM deposit. -// See EvmDepositSegwitPubkey doc for argument descriptions. -func EvmDepositSegwitAddr(pk *PublicKey, bridge, wallet Address, chainId, auxData []byte, net *chaincfg.Params) (string, error) { +// depositSegwitAddr Compute the segwit deposit address to be used for a deposit on the specified chain. +// See depositSegwitPubkey doc for argument descriptions. +func depositSegwitAddr(eTag EcosystemTag, pk *PublicKey, bridge, wallet, chainId, auxData []byte, net *chaincfg.Params) (string, error) { // compute the pubkey - tpk, err := EvmDepositSegwitPubkey(pk, bridge, wallet, chainId, auxData) + tpk, err := depositSegwitPubkey(eTag, pk, bridge, wallet, chainId, auxData) if err != nil { return "", err } diff --git a/ecosystem_tag.go b/ecosystem_tag.go new file mode 100644 index 0000000..4c39261 --- /dev/null +++ b/ecosystem_tag.go @@ -0,0 +1,12 @@ +package deposit_address + +type EcosystemTag = uint8 + +// Ecosystem type tags +// +// These tags are used to distinguish deposit addresses for different chain types +const ( + EcosystemTagEvm EcosystemTag = 0 + EcosystemTagSui EcosystemTag = 1 + // here more chain-type identifiers when supported +) diff --git a/evm_deposit_addr.go b/evm_deposit_addr.go new file mode 100644 index 0000000..6e4a48f --- /dev/null +++ b/evm_deposit_addr.go @@ -0,0 +1,36 @@ +package deposit_address + +import ( + "github.com/btcsuite/btcd/chaincfg" + eth "github.com/ethereum/go-ethereum/common" +) + +// EvmDepositTweak Compute the tweak bytes for an EVM deposit address. +// +// This is defined as +// +// taggedHash( AuxData || EvmTag || ChainId || LBTCAddress || WalletAddress ) +// +// where 'taggedHash' is a sha256 instance as returned by 'depositHasher()', +// 'EvmTag' is defined above, 'ChainId' is serialized as 32 big-endian bytes, +// LBTCAddress and WalletAddress are 20-byte EVM addresses, and AuxData is a +// 32-byte value encoding chain-agnostic auxiliary data. +func EvmDepositTweak(lbtcContract, wallet eth.Address, chainId, auxData []byte) ([]byte, error) { + return depositTweak(EcosystemTagEvm, lbtcContract.Bytes(), wallet.Bytes(), chainId, auxData) +} + +// EvmDepositSegwitPubkey Compute the segwit public key to be used for an EVM deposit. +// +// - 'pk' is the base (untweaked) public key to tweak +// - 'lbtcContract' is the EVM address of the destination LBTC contract +// - 'wallet' is the EVM address that will claim this deposit +// - 'chainId' is the chain id for the target chain as defined in the Lombard documentation +func EvmDepositSegwitPubkey(pk *PublicKey, lbtcContract, wallet Address, chainId, auxData []byte) (*PublicKey, error) { + return depositSegwitPubkey(EcosystemTagEvm, pk, lbtcContract.Bytes(), wallet.Bytes(), chainId, auxData) +} + +// EvmDepositSegwitAddr Compute the segwit deposit address to be used for an EVM deposit. +// See EvmDepositSegwitPubkey doc for argument descriptions. +func EvmDepositSegwitAddr(pk *PublicKey, bridge, wallet Address, chainId, auxData []byte, net *chaincfg.Params) (string, error) { + return depositSegwitAddr(EcosystemTagEvm, pk, bridge.Bytes(), wallet.Bytes(), chainId, auxData, net) +} diff --git a/segwit_tweak_test.go b/segwit_tweak_test.go index 9b04ae9..284395f 100644 --- a/segwit_tweak_test.go +++ b/segwit_tweak_test.go @@ -5,6 +5,8 @@ import ( "encoding/hex" "fmt" "testing" + + "github.com/decred/dcrd/dcrec/secp256k1/v4" ) // known-answer test values generated from reference rust impl diff --git a/sui_deposit_addr.go b/sui_deposit_addr.go new file mode 100644 index 0000000..30b8091 --- /dev/null +++ b/sui_deposit_addr.go @@ -0,0 +1,56 @@ +package deposit_address + +import ( + "github.com/btcsuite/btcd/chaincfg" +) + +const SuiAddressLength int = 32 + +type SuiAddress [SuiAddressLength]byte + +// SetBytes sets the address to the value of b. +// If b is larger than len(a), b is cropped from the left. +func (a SuiAddress) SetBytes(b []byte) { + if len(b) > len(a) { + b = b[len(b)-SuiAddressLength:] + } + copy(a[SuiAddressLength-len(b):], b) +} + +// BytesToSuiAddress returns Address with value b. +// If b is larger than `SuiAddressLength`, b is cropped from the left. +func BytesToSuiAddress(b []byte) SuiAddress { + var a SuiAddress + a.SetBytes(b) + return a +} + +// SuiDepositTweak Compute the tweak bytes for a Sui deposit address. +// +// This is defined as +// +// taggedHash( AuxData || SuiTag || ChainId || LBTCAddress || WalletAddress ) +// +// where 'taggedHash' is a sha256 instance as returned by 'depositHasher()', +// 'SuiTag' is 0x01, 'ChainId' is the lombard chain identifier and it is serialized as 32 big-endian +// bytes, LBTCAddress is the LBTC on-chain object, WalletAddress is the Sui addres to receive +// LBTC after deposit, and AuxData is a 32-byte value encoding chain-agnostic auxiliary data. +func SuiDepositTweak(lbtcContract, wallet SuiAddress, chainId, auxData []byte) ([]byte, error) { + return depositTweak(EcosystemTagSui, lbtcContract[:], wallet[:], chainId, auxData) +} + +// SuiDepositSegwitPubkey Compute the segwit public key to be used for a Sui deposit. +// +// - 'pk' is the base (untweaked) public key to tweak +// - 'lbtcContract' is the Sui address of the destination LBTC on-chain object +// - 'wallet' is the Sui address that will claim this deposit +// - 'chainId' is the chain id for the target chain as defined in the Lombard documentation +func SuiDepositSegwitPubkey(pk *PublicKey, lbtcContract, wallet SuiAddress, chainId, auxData []byte) (*PublicKey, error) { + return depositSegwitPubkey(EcosystemTagSui, pk, lbtcContract[:], wallet[:], chainId, auxData) +} + +// SuiDepositSegwitAddr Compute the segwit deposit address to be used for a deposit. +// See SuiDepositSegwitPubkey doc for argument descriptions. +func SuiDepositSegwitAddr(pk *PublicKey, bridge, wallet SuiAddress, chainId, auxData []byte, net *chaincfg.Params) (string, error) { + return depositSegwitAddr(EcosystemTagSui, pk, bridge[:], wallet[:], chainId, auxData, net) +} diff --git a/tweak_bytes.go b/tweak_bytes.go index bbdb8c5..b73e2df 100644 --- a/tweak_bytes.go +++ b/tweak_bytes.go @@ -9,29 +9,41 @@ type BlockchainType string const ( BlockchainTypeEvm BlockchainType = "evm" + BlockchainTypeSui BlockchainType = "sui" + ChainIdSize int = 32 ) // CalcTweakBytes Compute the tweakBytes for a given request, dispatching on `blockchainType` func CalcTweakBytes( blockchainType BlockchainType, - chainId [32]byte, + chainId [ChainIdSize]byte, toAddress, lbtcAddress, auxData []byte, ) ([]byte, error) { switch blockchainType { case BlockchainTypeEvm: // evm chain uses 20-byte address - if len(lbtcAddress) != 20 { - return nil, errors.Errorf("bad LbtcAddress (got %d bytes, expected 20)", len(lbtcAddress)) + if len(lbtcAddress) != eth.AddressLength { + return nil, errors.Errorf("bad LbtcAddress (got %d bytes, expected %d)", len(lbtcAddress), eth.AddressLength) } lbtcAddr := eth.BytesToAddress(lbtcAddress) - if len(toAddress) != 20 { - return nil, errors.Errorf("bad ToAddress (got %d bytes, expected 20)", len(toAddress)) + if len(toAddress) != eth.AddressLength { + return nil, errors.Errorf("bad ToAddress (got %d bytes, expected %d)", len(toAddress), eth.AddressLength) } depositAddr := eth.BytesToAddress(toAddress) return EvmDepositTweak(lbtcAddr, depositAddr, chainId[:], auxData) + case BlockchainTypeSui: + if len(lbtcAddress) != SuiAddressLength { + return nil, errors.Errorf("bad LbtcAddress (got %d bytes, expected %d)", len(lbtcAddress), SuiAddressLength) + } + + if len(toAddress) != SuiAddressLength { + return nil, errors.Errorf("bad ToAddress (got %d bytes, expected %d)", len(toAddress), SuiAddressLength) + } + + return SuiDepositTweak(BytesToSuiAddress(lbtcAddress), BytesToSuiAddress(toAddress), chainId[:], auxData) default: return nil, errors.Errorf("unsupported blockchain type: %s", blockchainType) } From 9a46df3831af2f255b3498e9d982ec7e422e09af Mon Sep 17 00:00:00 2001 From: Antonio Russo Date: Thu, 16 Jan 2025 19:28:53 +0100 Subject: [PATCH 2/7] feat: simplify logic by using lombard chain identifiers for ecosystems --- chain_id.go | 34 +++++++++++++++++++++++++++ deposit_addr.go | 36 ++++++++++++++-------------- deposit_addr_test.go | 9 +++---- ecosystem_tag.go | 12 ---------- evm_deposit_addr.go | 36 ---------------------------- sui_deposit_addr.go | 56 -------------------------------------------- tweak_bytes.go | 30 +++++++----------------- 7 files changed, 65 insertions(+), 148 deletions(-) create mode 100644 chain_id.go delete mode 100644 ecosystem_tag.go delete mode 100644 evm_deposit_addr.go delete mode 100644 sui_deposit_addr.go diff --git a/chain_id.go b/chain_id.go new file mode 100644 index 0000000..a7a975e --- /dev/null +++ b/chain_id.go @@ -0,0 +1,34 @@ +package deposit_address + +import "fmt" + +const ChainIdSize = 32 + +type ChainIdEcosystem uint8 + +const ( + ChainIdEcosystemEVM ChainIdEcosystem = iota + ChainIdEcosystemSui +) + +func (c ChainIdEcosystem) String() string { + switch c { + case ChainIdEcosystemEVM: + return "evm" + case ChainIdEcosystemSui: + return "sui" + default: + return fmt.Sprintf("unsupported(%d)", c) + } +} + +// Chain Id according to the Lombard documentation +type ChainId [ChainIdSize]byte + +func (c ChainId) Bytes() []byte { + return c[:] +} + +func (c ChainId) Ecosystem() ChainIdEcosystem { + return ChainIdEcosystem(c[0]) +} diff --git a/deposit_addr.go b/deposit_addr.go index e71cb86..1a7376f 100644 --- a/deposit_addr.go +++ b/deposit_addr.go @@ -13,7 +13,8 @@ type Address = eth.Address type Sha256 = hash.Hash const ( - DepositAddrTag = "LombardDepositAddr" + DepositAddrTag = "LombardDepositAddr" + DeprecatedChainTag = byte(0) ) // Create a tagged hasher used to compute Lombard deposit addresses @@ -35,17 +36,17 @@ func depositHasher() Sha256 { return h } -// depositTweak Compute the tweak bytes for a deposit address. +// DepositTweak Compute the tweak bytes for a deposit address. // // This is generally defined as // -// taggedHash( AuxData || EcosystemTag || ChainId || LBTCAddress || WalletAddress ) +// taggedHash( AuxData || ChainId || LBTCAddress || WalletAddress ) // -// where 'taggedHash' is a sha256 instance as returned by 'depositHasher()', -// 'EcosystemTag' is defined in the dedicated file as a type, 'ChainId' is serialized as 32 -// big-endian bytes, LBTCAddress and WalletAddress are byte arrays representing the respective addresses -// on the selected chain, and AuxData is a 32-byte value encoding chain-agnostic auxiliary data. -func depositTweak(eTag EcosystemTag, lbtcContract, wallet, chainId, auxData []byte) ([]byte, error) { +// where 'taggedHash' is a sha256 instance as returned by 'depositHasher()', 'ChainId' is a 32 bytes +// big-endian identifier of the chain, LBTCAddress and WalletAddress are byte arrays representing +// the respective addresses on the selected chain, and AuxData is a 32-byte value encoding +// chain-agnostic auxiliary data. +func DepositTweak(lbtcContract, wallet, chainId, auxData []byte) ([]byte, error) { if len(auxData) != AuxDataSize { return nil, errors.Errorf("wrong size for auxData (got %v, want %v)", len(auxData), AuxDataSize) } @@ -58,11 +59,11 @@ func depositTweak(eTag EcosystemTag, lbtcContract, wallet, chainId, auxData []by // aux data (32 bytes) h.Write(auxData[:]) - // ecosystem tag (1 byte) - h.Write([]byte{eTag}) + // 1 byte tag previously used to select chain, now deprecated and constant + // for backward compatibility + h.Write([]byte{DeprecatedChainTag}) // chain-id (32 bytes) as defined by Lombard documentation - // we zero-pad if `chainId` is less than 32 bytes and error if it is more. h.Write(chainId[:]) // LBTC contract address @@ -74,16 +75,15 @@ func depositTweak(eTag EcosystemTag, lbtcContract, wallet, chainId, auxData []by return h.Sum(nil), nil } -// depositSegwitPubkey Compute the segwit public key to be used for a deposit. +// DepositSegwitPubkey Compute the segwit public key to be used for a deposit. // -// - 'eTag' is the ecosystem tag to select the // - 'pk' is the base (untweaked) public key to tweak // - 'lbtcContract' is the address of the LBTC contract or object on the destination chain // - 'wallet' is the address that will claim the deposit on the destination chain // - 'chainId' is the chain id for the target chain as defined in the Lombard documentation -func depositSegwitPubkey(eTag EcosystemTag, pk *PublicKey, lbtcContract, wallet, chainId, auxData []byte) (*PublicKey, error) { +func DepositSegwitPubkey(pk *PublicKey, lbtcContract, wallet, chainId, auxData []byte) (*PublicKey, error) { // compute tweak bytes - tweakBytes, err := depositTweak(eTag, lbtcContract, wallet, chainId, auxData) + tweakBytes, err := DepositTweak(lbtcContract, wallet, chainId, auxData) if err != nil { return nil, err } @@ -91,11 +91,11 @@ func depositSegwitPubkey(eTag EcosystemTag, pk *PublicKey, lbtcContract, wallet, return TweakPublicKey(pk, tweakBytes) } -// depositSegwitAddr Compute the segwit deposit address to be used for a deposit on the specified chain. +// DepositSegwitAddr Compute the segwit deposit address to be used for a deposit on the specified chain. // See depositSegwitPubkey doc for argument descriptions. -func depositSegwitAddr(eTag EcosystemTag, pk *PublicKey, bridge, wallet, chainId, auxData []byte, net *chaincfg.Params) (string, error) { +func DepositSegwitAddr(pk *PublicKey, bridge, wallet, chainId, auxData []byte, net *chaincfg.Params) (string, error) { // compute the pubkey - tpk, err := depositSegwitPubkey(eTag, pk, bridge, wallet, chainId, auxData) + tpk, err := DepositSegwitPubkey(pk, bridge, wallet, chainId, auxData) if err != nil { return "", err } diff --git a/deposit_addr_test.go b/deposit_addr_test.go index d9e1d6d..bca3593 100644 --- a/deposit_addr_test.go +++ b/deposit_addr_test.go @@ -5,9 +5,10 @@ import ( "encoding/binary" "encoding/hex" "fmt" - "github.com/decred/dcrd/dcrec/secp256k1/v4" "testing" + "github.com/decred/dcrd/dcrec/secp256k1/v4" + "github.com/btcsuite/btcd/chaincfg" eth "github.com/ethereum/go-ethereum/common" ) @@ -202,21 +203,21 @@ func TestEthTweakValueRustKat(t *testing.T) { auxData := v4 // check tweak result - tweak, err := EvmDepositTweak(lbtcContractAddr, walletAddr, chainId[:], auxData[:]) + tweak, err := DepositTweak(lbtcContractAddr.Bytes(), walletAddr.Bytes(), chainId[:], auxData[:]) if err != nil { panic(fmt.Sprintf("error computing deposit tweak: %v", err)) } tweakString := hex.EncodeToString(tweak) // check deposit pubkey result - tpk, err := EvmDepositSegwitPubkey(pk, lbtcContractAddr, walletAddr, chainId[:], auxData[:]) + tpk, err := DepositSegwitPubkey(pk, lbtcContractAddr.Bytes(), walletAddr.Bytes(), chainId[:], auxData[:]) if err != nil { panic(fmt.Sprintf("error tweaking pubkey: %v", err)) } tpkString := hex.EncodeToString(tpk.SerializeCompressed()) // check segwit address - segwitAddr, err := EvmDepositSegwitAddr(pk, lbtcContractAddr, walletAddr, chainId[:], auxData[:], params) + segwitAddr, err := DepositSegwitAddr(pk, lbtcContractAddr.Bytes(), walletAddr.Bytes(), chainId[:], auxData[:], params) if err != nil { panic(fmt.Sprintf("error tweaking addr: %v", err)) } diff --git a/ecosystem_tag.go b/ecosystem_tag.go deleted file mode 100644 index 4c39261..0000000 --- a/ecosystem_tag.go +++ /dev/null @@ -1,12 +0,0 @@ -package deposit_address - -type EcosystemTag = uint8 - -// Ecosystem type tags -// -// These tags are used to distinguish deposit addresses for different chain types -const ( - EcosystemTagEvm EcosystemTag = 0 - EcosystemTagSui EcosystemTag = 1 - // here more chain-type identifiers when supported -) diff --git a/evm_deposit_addr.go b/evm_deposit_addr.go deleted file mode 100644 index 6e4a48f..0000000 --- a/evm_deposit_addr.go +++ /dev/null @@ -1,36 +0,0 @@ -package deposit_address - -import ( - "github.com/btcsuite/btcd/chaincfg" - eth "github.com/ethereum/go-ethereum/common" -) - -// EvmDepositTweak Compute the tweak bytes for an EVM deposit address. -// -// This is defined as -// -// taggedHash( AuxData || EvmTag || ChainId || LBTCAddress || WalletAddress ) -// -// where 'taggedHash' is a sha256 instance as returned by 'depositHasher()', -// 'EvmTag' is defined above, 'ChainId' is serialized as 32 big-endian bytes, -// LBTCAddress and WalletAddress are 20-byte EVM addresses, and AuxData is a -// 32-byte value encoding chain-agnostic auxiliary data. -func EvmDepositTweak(lbtcContract, wallet eth.Address, chainId, auxData []byte) ([]byte, error) { - return depositTweak(EcosystemTagEvm, lbtcContract.Bytes(), wallet.Bytes(), chainId, auxData) -} - -// EvmDepositSegwitPubkey Compute the segwit public key to be used for an EVM deposit. -// -// - 'pk' is the base (untweaked) public key to tweak -// - 'lbtcContract' is the EVM address of the destination LBTC contract -// - 'wallet' is the EVM address that will claim this deposit -// - 'chainId' is the chain id for the target chain as defined in the Lombard documentation -func EvmDepositSegwitPubkey(pk *PublicKey, lbtcContract, wallet Address, chainId, auxData []byte) (*PublicKey, error) { - return depositSegwitPubkey(EcosystemTagEvm, pk, lbtcContract.Bytes(), wallet.Bytes(), chainId, auxData) -} - -// EvmDepositSegwitAddr Compute the segwit deposit address to be used for an EVM deposit. -// See EvmDepositSegwitPubkey doc for argument descriptions. -func EvmDepositSegwitAddr(pk *PublicKey, bridge, wallet Address, chainId, auxData []byte, net *chaincfg.Params) (string, error) { - return depositSegwitAddr(EcosystemTagEvm, pk, bridge.Bytes(), wallet.Bytes(), chainId, auxData, net) -} diff --git a/sui_deposit_addr.go b/sui_deposit_addr.go deleted file mode 100644 index 30b8091..0000000 --- a/sui_deposit_addr.go +++ /dev/null @@ -1,56 +0,0 @@ -package deposit_address - -import ( - "github.com/btcsuite/btcd/chaincfg" -) - -const SuiAddressLength int = 32 - -type SuiAddress [SuiAddressLength]byte - -// SetBytes sets the address to the value of b. -// If b is larger than len(a), b is cropped from the left. -func (a SuiAddress) SetBytes(b []byte) { - if len(b) > len(a) { - b = b[len(b)-SuiAddressLength:] - } - copy(a[SuiAddressLength-len(b):], b) -} - -// BytesToSuiAddress returns Address with value b. -// If b is larger than `SuiAddressLength`, b is cropped from the left. -func BytesToSuiAddress(b []byte) SuiAddress { - var a SuiAddress - a.SetBytes(b) - return a -} - -// SuiDepositTweak Compute the tweak bytes for a Sui deposit address. -// -// This is defined as -// -// taggedHash( AuxData || SuiTag || ChainId || LBTCAddress || WalletAddress ) -// -// where 'taggedHash' is a sha256 instance as returned by 'depositHasher()', -// 'SuiTag' is 0x01, 'ChainId' is the lombard chain identifier and it is serialized as 32 big-endian -// bytes, LBTCAddress is the LBTC on-chain object, WalletAddress is the Sui addres to receive -// LBTC after deposit, and AuxData is a 32-byte value encoding chain-agnostic auxiliary data. -func SuiDepositTweak(lbtcContract, wallet SuiAddress, chainId, auxData []byte) ([]byte, error) { - return depositTweak(EcosystemTagSui, lbtcContract[:], wallet[:], chainId, auxData) -} - -// SuiDepositSegwitPubkey Compute the segwit public key to be used for a Sui deposit. -// -// - 'pk' is the base (untweaked) public key to tweak -// - 'lbtcContract' is the Sui address of the destination LBTC on-chain object -// - 'wallet' is the Sui address that will claim this deposit -// - 'chainId' is the chain id for the target chain as defined in the Lombard documentation -func SuiDepositSegwitPubkey(pk *PublicKey, lbtcContract, wallet SuiAddress, chainId, auxData []byte) (*PublicKey, error) { - return depositSegwitPubkey(EcosystemTagSui, pk, lbtcContract[:], wallet[:], chainId, auxData) -} - -// SuiDepositSegwitAddr Compute the segwit deposit address to be used for a deposit. -// See SuiDepositSegwitPubkey doc for argument descriptions. -func SuiDepositSegwitAddr(pk *PublicKey, bridge, wallet SuiAddress, chainId, auxData []byte, net *chaincfg.Params) (string, error) { - return depositSegwitAddr(EcosystemTagSui, pk, bridge[:], wallet[:], chainId, auxData, net) -} diff --git a/tweak_bytes.go b/tweak_bytes.go index b73e2df..964f873 100644 --- a/tweak_bytes.go +++ b/tweak_bytes.go @@ -5,46 +5,32 @@ import ( "github.com/pkg/errors" ) -type BlockchainType string - -const ( - BlockchainTypeEvm BlockchainType = "evm" - BlockchainTypeSui BlockchainType = "sui" - ChainIdSize int = 32 -) +const SuiAddressLength = 32 // CalcTweakBytes Compute the tweakBytes for a given request, dispatching on `blockchainType` func CalcTweakBytes( - blockchainType BlockchainType, - chainId [ChainIdSize]byte, + chainId ChainId, toAddress, lbtcAddress, auxData []byte, ) ([]byte, error) { - switch blockchainType { - case BlockchainTypeEvm: - // evm chain uses 20-byte address + switch chainId.Ecosystem() { + case ChainIdEcosystemEVM: if len(lbtcAddress) != eth.AddressLength { return nil, errors.Errorf("bad LbtcAddress (got %d bytes, expected %d)", len(lbtcAddress), eth.AddressLength) } - - lbtcAddr := eth.BytesToAddress(lbtcAddress) if len(toAddress) != eth.AddressLength { return nil, errors.Errorf("bad ToAddress (got %d bytes, expected %d)", len(toAddress), eth.AddressLength) } - - depositAddr := eth.BytesToAddress(toAddress) - return EvmDepositTweak(lbtcAddr, depositAddr, chainId[:], auxData) - case BlockchainTypeSui: + return DepositTweak(lbtcAddress, toAddress, chainId[:], auxData) + case ChainIdEcosystemSui: if len(lbtcAddress) != SuiAddressLength { return nil, errors.Errorf("bad LbtcAddress (got %d bytes, expected %d)", len(lbtcAddress), SuiAddressLength) } - if len(toAddress) != SuiAddressLength { return nil, errors.Errorf("bad ToAddress (got %d bytes, expected %d)", len(toAddress), SuiAddressLength) } - - return SuiDepositTweak(BytesToSuiAddress(lbtcAddress), BytesToSuiAddress(toAddress), chainId[:], auxData) + return DepositTweak(lbtcAddress, toAddress, chainId[:], auxData) default: - return nil, errors.Errorf("unsupported blockchain type: %s", blockchainType) + return nil, errors.Errorf("unsupported blockchain type: %s", chainId.Ecosystem().String()) } } From 0e8ac00b6b0051af86ecc6999268f0cf3af74110 Mon Sep 17 00:00:00 2001 From: Antonio Russo Date: Fri, 7 Feb 2025 13:16:01 +0100 Subject: [PATCH 3/7] feat: integrate chain-id package and include sui examples --- chain_id.go | 34 -------------- deposit_addr.go | 12 +++-- deposit_addr_test.go | 105 ++++++++++++++++++++++++++++++++++++++++--- go.mod | 1 + go.sum | 6 +++ tweak_bytes.go | 13 +++--- 6 files changed, 119 insertions(+), 52 deletions(-) delete mode 100644 chain_id.go diff --git a/chain_id.go b/chain_id.go deleted file mode 100644 index a7a975e..0000000 --- a/chain_id.go +++ /dev/null @@ -1,34 +0,0 @@ -package deposit_address - -import "fmt" - -const ChainIdSize = 32 - -type ChainIdEcosystem uint8 - -const ( - ChainIdEcosystemEVM ChainIdEcosystem = iota - ChainIdEcosystemSui -) - -func (c ChainIdEcosystem) String() string { - switch c { - case ChainIdEcosystemEVM: - return "evm" - case ChainIdEcosystemSui: - return "sui" - default: - return fmt.Sprintf("unsupported(%d)", c) - } -} - -// Chain Id according to the Lombard documentation -type ChainId [ChainIdSize]byte - -func (c ChainId) Bytes() []byte { - return c[:] -} - -func (c ChainId) Ecosystem() ChainIdEcosystem { - return ChainIdEcosystem(c[0]) -} diff --git a/deposit_addr.go b/deposit_addr.go index 1a7376f..9df4f33 100644 --- a/deposit_addr.go +++ b/deposit_addr.go @@ -6,6 +6,7 @@ import ( "github.com/btcsuite/btcd/chaincfg" eth "github.com/ethereum/go-ethereum/common" + "github.com/lombard-finance/chain/chainid" "github.com/pkg/errors" ) @@ -46,13 +47,10 @@ func depositHasher() Sha256 { // big-endian identifier of the chain, LBTCAddress and WalletAddress are byte arrays representing // the respective addresses on the selected chain, and AuxData is a 32-byte value encoding // chain-agnostic auxiliary data. -func DepositTweak(lbtcContract, wallet, chainId, auxData []byte) ([]byte, error) { +func DepositTweak(lbtcContract, wallet []byte, chainId chainid.ChainId, auxData []byte) ([]byte, error) { if len(auxData) != AuxDataSize { return nil, errors.Errorf("wrong size for auxData (got %v, want %v)", len(auxData), AuxDataSize) } - if len(chainId) != ChainIdSize { - return nil, errors.Errorf("wrong size for chainId (got %v, want %v)", len(chainId), ChainIdSize) - } h := depositHasher() @@ -64,7 +62,7 @@ func DepositTweak(lbtcContract, wallet, chainId, auxData []byte) ([]byte, error) h.Write([]byte{DeprecatedChainTag}) // chain-id (32 bytes) as defined by Lombard documentation - h.Write(chainId[:]) + h.Write(chainId.Bytes()) // LBTC contract address h.Write(lbtcContract) @@ -81,7 +79,7 @@ func DepositTweak(lbtcContract, wallet, chainId, auxData []byte) ([]byte, error) // - 'lbtcContract' is the address of the LBTC contract or object on the destination chain // - 'wallet' is the address that will claim the deposit on the destination chain // - 'chainId' is the chain id for the target chain as defined in the Lombard documentation -func DepositSegwitPubkey(pk *PublicKey, lbtcContract, wallet, chainId, auxData []byte) (*PublicKey, error) { +func DepositSegwitPubkey(pk *PublicKey, lbtcContract, wallet []byte, chainId chainid.ChainId, auxData []byte) (*PublicKey, error) { // compute tweak bytes tweakBytes, err := DepositTweak(lbtcContract, wallet, chainId, auxData) if err != nil { @@ -93,7 +91,7 @@ func DepositSegwitPubkey(pk *PublicKey, lbtcContract, wallet, chainId, auxData [ // DepositSegwitAddr Compute the segwit deposit address to be used for a deposit on the specified chain. // See depositSegwitPubkey doc for argument descriptions. -func DepositSegwitAddr(pk *PublicKey, bridge, wallet, chainId, auxData []byte, net *chaincfg.Params) (string, error) { +func DepositSegwitAddr(pk *PublicKey, bridge, wallet []byte, chainId chainid.ChainId, auxData []byte, net *chaincfg.Params) (string, error) { // compute the pubkey tpk, err := DepositSegwitPubkey(pk, bridge, wallet, chainId, auxData) if err != nil { diff --git a/deposit_addr_test.go b/deposit_addr_test.go index bca3593..12f536d 100644 --- a/deposit_addr_test.go +++ b/deposit_addr_test.go @@ -8,6 +8,8 @@ import ( "testing" "github.com/decred/dcrd/dcrec/secp256k1/v4" + "github.com/lombard-finance/chain/chainid" + "github.com/stretchr/testify/require" "github.com/btcsuite/btcd/chaincfg" eth "github.com/ethereum/go-ethereum/common" @@ -198,26 +200,27 @@ func TestEthTweakValueRustKat(t *testing.T) { lbtcContractAddr := eth.BytesToAddress(v1[:20]) walletAddr := eth.BytesToAddress(v2[:20]) chainIdU64 := binary.BigEndian.Uint64(v3[:8]) - var chainId [32]byte - binary.BigEndian.PutUint64(chainId[24:], chainIdU64) + var chainIdBytes [32]byte + binary.BigEndian.PutUint64(chainIdBytes[24:], chainIdU64) + chainId := chainid.NewUnsafeChainId(chainIdBytes[:]) auxData := v4 // check tweak result - tweak, err := DepositTweak(lbtcContractAddr.Bytes(), walletAddr.Bytes(), chainId[:], auxData[:]) + tweak, err := DepositTweak(lbtcContractAddr.Bytes(), walletAddr.Bytes(), chainId, auxData[:]) if err != nil { panic(fmt.Sprintf("error computing deposit tweak: %v", err)) } tweakString := hex.EncodeToString(tweak) // check deposit pubkey result - tpk, err := DepositSegwitPubkey(pk, lbtcContractAddr.Bytes(), walletAddr.Bytes(), chainId[:], auxData[:]) + tpk, err := DepositSegwitPubkey(pk, lbtcContractAddr.Bytes(), walletAddr.Bytes(), chainId, auxData[:]) if err != nil { panic(fmt.Sprintf("error tweaking pubkey: %v", err)) } tpkString := hex.EncodeToString(tpk.SerializeCompressed()) // check segwit address - segwitAddr, err := DepositSegwitAddr(pk, lbtcContractAddr.Bytes(), walletAddr.Bytes(), chainId[:], auxData[:], params) + segwitAddr, err := DepositSegwitAddr(pk, lbtcContractAddr.Bytes(), walletAddr.Bytes(), chainId, auxData[:], params) if err != nil { panic(fmt.Sprintf("error tweaking addr: %v", err)) } @@ -238,3 +241,95 @@ func TestEthTweakValueRustKat(t *testing.T) { } } } + +var referenceValues = []struct { + testLabel string + rootDepositKey string + auxData string + lbtcContract string + wallet string + chainId string + expectedTweak string + expectedPubkey string + expectedSegwitAddr string +}{ + { + testLabel: "Sui Testnet - 1", + rootDepositKey: "0x043dcf7a68429b23a0396ca61c1ab243ccbbcc629ff04c59394458d6db5dd2bb159e0b7a71ef07247b59a0a21b1f1eaee61a40064ade423e926f38550065a43587", + auxData: "2137aefeb756a435f07fceff39a061bd2a062b617bd8857e9c32b44ef2596bc8", // ComputeAuxDataV0(0, [32]byte{0...}) + lbtcContract: "54945cd3d15c0012d35a92ed6f1f373157216fc6bdc5bd79b03ee86da3ca455b", + wallet: "0d3c73069aef96e8a1d209e2c96ddefc4b911d025932e414db201be70f0ae15e", + chainId: "010000000000000000000000000000000000000000000000000000004c78adac", + expectedTweak: "c5f14fe401d015c4ea34632e6d775b751e1925fe718e6b339d2a74689b3a0609", + expectedPubkey: "02a2188a8c2449e16c3f50aab677c8be90916a7c7e6ab25a88222fc846257c28fb", + expectedSegwitAddr: "tb1q9sjdz0vpnsshhule5kkxvfggvq8lznpck4tay0", + }, + { + testLabel: "Sui Testnet - 2", + rootDepositKey: "0x043dcf7a68429b23a0396ca61c1ab243ccbbcc629ff04c59394458d6db5dd2bb159e0b7a71ef07247b59a0a21b1f1eaee61a40064ade423e926f38550065a43587", + auxData: "2137aefeb756a435f07fceff39a061bd2a062b617bd8857e9c32b44ef2596bc8", // ComputeAuxDataV0(0, [32]byte{0...}) + lbtcContract: "54945cd3d15c0012d35a92ed6f1f373157216fc6bdc5bd79b03ee86da3ca455b", + wallet: "5e9ae2ae1c76cb14be16cd2d521f8200c95cc94ab30947c61ade11a0a6439d28", + chainId: "010000000000000000000000000000000000000000000000000000004c78adac", + expectedTweak: "a6d4eb9bcfa5c683513b06fd531184792767f5355402ce23c6dab417696f6392", + expectedPubkey: "0393e2e2e1acc9a702d8b62e990ed9eff4e36589c6cd44e48101a2d3c3c58d5abb", + expectedSegwitAddr: "tb1q8uwc6au5765r9jttj949dg6f2qzcqt58svy8c0", + }, + { + testLabel: "Sui Mainnet - 1", + rootDepositKey: "0x043dcf7a68429b23a0396ca61c1ab243ccbbcc629ff04c59394458d6db5dd2bb159e0b7a71ef07247b59a0a21b1f1eaee61a40064ade423e926f38550065a43587", + auxData: "2137aefeb756a435f07fceff39a061bd2a062b617bd8857e9c32b44ef2596bc8", // ComputeAuxDataV0(0, [32]byte{0...}) + lbtcContract: "54945cd3d15c0012d35a92ed6f1f373157216fc6bdc5bd79b03ee86da3ca455b", + wallet: "0d3c73069aef96e8a1d209e2c96ddefc4b911d025932e414db201be70f0ae15e", + chainId: "0100000000000000000000000000000000000000000000000000000035834a8a", + expectedTweak: "35b7205e7d5f1b077091f3164e5daed121f4bb27799c57d9acd976a4044a18bd", + expectedPubkey: "02744518bc0dafc22c494f7dc9ec780fa6d9ae53d3be720ee003f672edfba1063b", + expectedSegwitAddr: "tb1qk5xk9gfr5r8l57ma2euyyvc8h7kc9etlry3a75", + }, + { + testLabel: "Sui Mainnet - 2", + rootDepositKey: "0x043dcf7a68429b23a0396ca61c1ab243ccbbcc629ff04c59394458d6db5dd2bb159e0b7a71ef07247b59a0a21b1f1eaee61a40064ade423e926f38550065a43587", + auxData: "2137aefeb756a435f07fceff39a061bd2a062b617bd8857e9c32b44ef2596bc8", // ComputeAuxDataV0(0, [32]byte{0...}) + lbtcContract: "54945cd3d15c0012d35a92ed6f1f373157216fc6bdc5bd79b03ee86da3ca455b", + wallet: "5e9ae2ae1c76cb14be16cd2d521f8200c95cc94ab30947c61ade11a0a6439d28", + chainId: "0100000000000000000000000000000000000000000000000000000035834a8a", + expectedTweak: "e454f631af7c2f235c372be61a63c58a1b94832e0240cf1a06b9e657df5d9c13", + expectedPubkey: "034a915f6ac8d6c6920a754392338aa2f41a4070d30564d6af3749d80a9b58eb81", + expectedSegwitAddr: "tb1qagvmd7y5x5hkkn6avva5mv0thhlnn6ktk30j67", + }, +} + +func TestWithReferenceValues(t *testing.T) { + // just an initial seed to generate constant data for all tests + hashVal := sha256.Sum256([]byte("segwit_lombard_tweak_test_rs")) + pk := secp256k1.PrivKeyFromBytes(hashVal[:]).PubKey() + params := &chaincfg.SigNetParams + + for _, rf := range referenceValues { + t.Run(rf.testLabel, func(t *testing.T) { + lbtcContractBytes, err := hex.DecodeString(rf.lbtcContract) + require.NoError(t, err) + walletBytes, err := hex.DecodeString(rf.wallet) + require.NoError(t, err) + chainId, err := chainid.NewChainIdFromHex(rf.chainId) + require.NoError(t, err) + auxDataBytes, err := hex.DecodeString(rf.auxData) + require.NoError(t, err) + + // check tweak result + tweak, err := DepositTweak(lbtcContractBytes, walletBytes, chainId, auxDataBytes) + require.NoError(t, err, "error on deposit tweak calculation") + require.Equal(t, rf.expectedTweak, hex.EncodeToString(tweak)) + + // check deposit pubkey result + tpk, err := DepositSegwitPubkey(pk, lbtcContractBytes, walletBytes, chainId, auxDataBytes) + require.NoError(t, err, "error tweaking the public key") + require.Equal(t, rf.expectedPubkey, hex.EncodeToString(tpk.SerializeCompressed())) + + // check segwit address + segwitAddr, err := DepositSegwitAddr(pk, lbtcContractBytes, walletBytes, chainId, auxDataBytes, params) + require.NoError(t, err, "error deriving address") + require.Equal(t, rf.expectedSegwitAddr, segwitAddr) + }) + } +} diff --git a/go.mod b/go.mod index 34159dd..1a26530 100644 --- a/go.mod +++ b/go.mod @@ -16,6 +16,7 @@ require ( github.com/btcsuite/btcd/chaincfg/chainhash v1.1.0 // indirect github.com/davecgh/go-spew v1.1.1 // indirect github.com/holiman/uint256 v1.2.4 // indirect + github.com/lombard-finance/chain v0.0.0-20250207120945-2d26469daa26 // indirect github.com/pmezard/go-difflib v1.0.0 // indirect golang.org/x/crypto v0.22.0 // indirect golang.org/x/sys v0.20.0 // indirect diff --git a/go.sum b/go.sum index 4115256..d78d85b 100644 --- a/go.sum +++ b/go.sum @@ -57,6 +57,12 @@ github.com/jessevdk/go-flags v0.0.0-20141203071132-1679536dcc89/go.mod h1:4FA24M github.com/jessevdk/go-flags v1.4.0/go.mod h1:4FA24M0QyGHXBuZZK/XkWh8h0e1EYbRYJSGM75WSRxI= github.com/jrick/logrotate v1.0.0/go.mod h1:LNinyqDIJnpAur+b8yyulnQw/wDuN1+BYKlTRt3OuAQ= github.com/kkdai/bstream v0.0.0-20161212061736-f391b8402d23/go.mod h1:J+Gs4SYgM6CZQHDETBtE9HaSEkGmuNXF86RwHhHUvq4= +github.com/lombard-finance/chain v0.0.0-20250204210857-e6c20ba69b01 h1:IC52aY17/XdtTDp/w+e/M7qYmtwWm51siGbmu6MtHWI= +github.com/lombard-finance/chain v0.0.0-20250204210857-e6c20ba69b01/go.mod h1:lZGCpGc0TcJk6bz6Y8b4oKHQFelh5Yulq7qWvYOqK4Q= +github.com/lombard-finance/chain v0.0.0-20250205154603-6b313aec67dc h1:SzKftIzvWPKyvS801EczHzgD33lU10LaD+oPoLLnbv0= +github.com/lombard-finance/chain v0.0.0-20250205154603-6b313aec67dc/go.mod h1:lZGCpGc0TcJk6bz6Y8b4oKHQFelh5Yulq7qWvYOqK4Q= +github.com/lombard-finance/chain v0.0.0-20250207120945-2d26469daa26 h1:cqJpG+WYSnKWVw2DCb4Yy5Rf3whDfTGSbuPxhudFo78= +github.com/lombard-finance/chain v0.0.0-20250207120945-2d26469daa26/go.mod h1:lZGCpGc0TcJk6bz6Y8b4oKHQFelh5Yulq7qWvYOqK4Q= github.com/nxadm/tail v1.4.4/go.mod h1:kenIhsEOeOJmVchQTgglprH7qJGnHDVpk1VPCcaMI8A= github.com/onsi/ginkgo v1.6.0/go.mod h1:lLunBs/Ym6LB5Z9jYTR76FiuTmxDTDusOGeTQH+WWjE= github.com/onsi/ginkgo v1.7.0/go.mod h1:lLunBs/Ym6LB5Z9jYTR76FiuTmxDTDusOGeTQH+WWjE= diff --git a/tweak_bytes.go b/tweak_bytes.go index 964f873..e033331 100644 --- a/tweak_bytes.go +++ b/tweak_bytes.go @@ -2,6 +2,7 @@ package deposit_address import ( eth "github.com/ethereum/go-ethereum/common" + "github.com/lombard-finance/chain/chainid" "github.com/pkg/errors" ) @@ -9,27 +10,27 @@ const SuiAddressLength = 32 // CalcTweakBytes Compute the tweakBytes for a given request, dispatching on `blockchainType` func CalcTweakBytes( - chainId ChainId, + chainId chainid.ChainId, toAddress, lbtcAddress, auxData []byte, ) ([]byte, error) { - switch chainId.Ecosystem() { - case ChainIdEcosystemEVM: + switch chainId.(type) { + case chainid.EVMChainId: if len(lbtcAddress) != eth.AddressLength { return nil, errors.Errorf("bad LbtcAddress (got %d bytes, expected %d)", len(lbtcAddress), eth.AddressLength) } if len(toAddress) != eth.AddressLength { return nil, errors.Errorf("bad ToAddress (got %d bytes, expected %d)", len(toAddress), eth.AddressLength) } - return DepositTweak(lbtcAddress, toAddress, chainId[:], auxData) - case ChainIdEcosystemSui: + return DepositTweak(lbtcAddress, toAddress, chainId, auxData) + case chainid.SuiChainId: if len(lbtcAddress) != SuiAddressLength { return nil, errors.Errorf("bad LbtcAddress (got %d bytes, expected %d)", len(lbtcAddress), SuiAddressLength) } if len(toAddress) != SuiAddressLength { return nil, errors.Errorf("bad ToAddress (got %d bytes, expected %d)", len(toAddress), SuiAddressLength) } - return DepositTweak(lbtcAddress, toAddress, chainId[:], auxData) + return DepositTweak(lbtcAddress, toAddress, chainId, auxData) default: return nil, errors.Errorf("unsupported blockchain type: %s", chainId.Ecosystem().String()) } From 2fbf0b846d65ae75f325516661fe16005c5d2185 Mon Sep 17 00:00:00 2001 From: Antonio Russo Date: Wed, 12 Feb 2025 15:01:54 +0100 Subject: [PATCH 4/7] fix: update chain-id package --- deposit_addr.go | 6 +++--- deposit_addr_test.go | 4 ++-- go.mod | 2 +- go.sum | 2 ++ tweak_bytes.go | 6 +++--- 5 files changed, 11 insertions(+), 9 deletions(-) diff --git a/deposit_addr.go b/deposit_addr.go index 9df4f33..3420a14 100644 --- a/deposit_addr.go +++ b/deposit_addr.go @@ -47,7 +47,7 @@ func depositHasher() Sha256 { // big-endian identifier of the chain, LBTCAddress and WalletAddress are byte arrays representing // the respective addresses on the selected chain, and AuxData is a 32-byte value encoding // chain-agnostic auxiliary data. -func DepositTweak(lbtcContract, wallet []byte, chainId chainid.ChainId, auxData []byte) ([]byte, error) { +func DepositTweak(lbtcContract, wallet []byte, chainId chainid.LChainId, auxData []byte) ([]byte, error) { if len(auxData) != AuxDataSize { return nil, errors.Errorf("wrong size for auxData (got %v, want %v)", len(auxData), AuxDataSize) } @@ -79,7 +79,7 @@ func DepositTweak(lbtcContract, wallet []byte, chainId chainid.ChainId, auxData // - 'lbtcContract' is the address of the LBTC contract or object on the destination chain // - 'wallet' is the address that will claim the deposit on the destination chain // - 'chainId' is the chain id for the target chain as defined in the Lombard documentation -func DepositSegwitPubkey(pk *PublicKey, lbtcContract, wallet []byte, chainId chainid.ChainId, auxData []byte) (*PublicKey, error) { +func DepositSegwitPubkey(pk *PublicKey, lbtcContract, wallet []byte, chainId chainid.LChainId, auxData []byte) (*PublicKey, error) { // compute tweak bytes tweakBytes, err := DepositTweak(lbtcContract, wallet, chainId, auxData) if err != nil { @@ -91,7 +91,7 @@ func DepositSegwitPubkey(pk *PublicKey, lbtcContract, wallet []byte, chainId cha // DepositSegwitAddr Compute the segwit deposit address to be used for a deposit on the specified chain. // See depositSegwitPubkey doc for argument descriptions. -func DepositSegwitAddr(pk *PublicKey, bridge, wallet []byte, chainId chainid.ChainId, auxData []byte, net *chaincfg.Params) (string, error) { +func DepositSegwitAddr(pk *PublicKey, bridge, wallet []byte, chainId chainid.LChainId, auxData []byte, net *chaincfg.Params) (string, error) { // compute the pubkey tpk, err := DepositSegwitPubkey(pk, bridge, wallet, chainId, auxData) if err != nil { diff --git a/deposit_addr_test.go b/deposit_addr_test.go index 12f536d..686d667 100644 --- a/deposit_addr_test.go +++ b/deposit_addr_test.go @@ -202,7 +202,7 @@ func TestEthTweakValueRustKat(t *testing.T) { chainIdU64 := binary.BigEndian.Uint64(v3[:8]) var chainIdBytes [32]byte binary.BigEndian.PutUint64(chainIdBytes[24:], chainIdU64) - chainId := chainid.NewUnsafeChainId(chainIdBytes[:]) + chainId := chainid.NewUnsafeLChainId(chainIdBytes[:]) auxData := v4 // check tweak result @@ -311,7 +311,7 @@ func TestWithReferenceValues(t *testing.T) { require.NoError(t, err) walletBytes, err := hex.DecodeString(rf.wallet) require.NoError(t, err) - chainId, err := chainid.NewChainIdFromHex(rf.chainId) + chainId, err := chainid.NewLChainIdFromHex(rf.chainId) require.NoError(t, err) auxDataBytes, err := hex.DecodeString(rf.auxData) require.NoError(t, err) diff --git a/go.mod b/go.mod index 1a26530..15a8115 100644 --- a/go.mod +++ b/go.mod @@ -16,7 +16,7 @@ require ( github.com/btcsuite/btcd/chaincfg/chainhash v1.1.0 // indirect github.com/davecgh/go-spew v1.1.1 // indirect github.com/holiman/uint256 v1.2.4 // indirect - github.com/lombard-finance/chain v0.0.0-20250207120945-2d26469daa26 // indirect + github.com/lombard-finance/chain v0.0.0-20250212131652-4b532e884d03 // indirect github.com/pmezard/go-difflib v1.0.0 // indirect golang.org/x/crypto v0.22.0 // indirect golang.org/x/sys v0.20.0 // indirect diff --git a/go.sum b/go.sum index d78d85b..67b0735 100644 --- a/go.sum +++ b/go.sum @@ -63,6 +63,8 @@ github.com/lombard-finance/chain v0.0.0-20250205154603-6b313aec67dc h1:SzKftIzvW github.com/lombard-finance/chain v0.0.0-20250205154603-6b313aec67dc/go.mod h1:lZGCpGc0TcJk6bz6Y8b4oKHQFelh5Yulq7qWvYOqK4Q= github.com/lombard-finance/chain v0.0.0-20250207120945-2d26469daa26 h1:cqJpG+WYSnKWVw2DCb4Yy5Rf3whDfTGSbuPxhudFo78= github.com/lombard-finance/chain v0.0.0-20250207120945-2d26469daa26/go.mod h1:lZGCpGc0TcJk6bz6Y8b4oKHQFelh5Yulq7qWvYOqK4Q= +github.com/lombard-finance/chain v0.0.0-20250212131652-4b532e884d03 h1:CZ+2ZWDhkO5oZs+rtj267Cp6Gn+LSI4s2Iwa04lTVag= +github.com/lombard-finance/chain v0.0.0-20250212131652-4b532e884d03/go.mod h1:lZGCpGc0TcJk6bz6Y8b4oKHQFelh5Yulq7qWvYOqK4Q= github.com/nxadm/tail v1.4.4/go.mod h1:kenIhsEOeOJmVchQTgglprH7qJGnHDVpk1VPCcaMI8A= github.com/onsi/ginkgo v1.6.0/go.mod h1:lLunBs/Ym6LB5Z9jYTR76FiuTmxDTDusOGeTQH+WWjE= github.com/onsi/ginkgo v1.7.0/go.mod h1:lLunBs/Ym6LB5Z9jYTR76FiuTmxDTDusOGeTQH+WWjE= diff --git a/tweak_bytes.go b/tweak_bytes.go index e033331..41e19fd 100644 --- a/tweak_bytes.go +++ b/tweak_bytes.go @@ -10,12 +10,12 @@ const SuiAddressLength = 32 // CalcTweakBytes Compute the tweakBytes for a given request, dispatching on `blockchainType` func CalcTweakBytes( - chainId chainid.ChainId, + chainId chainid.LChainId, toAddress, lbtcAddress, auxData []byte, ) ([]byte, error) { switch chainId.(type) { - case chainid.EVMChainId: + case chainid.EVMLChainId: if len(lbtcAddress) != eth.AddressLength { return nil, errors.Errorf("bad LbtcAddress (got %d bytes, expected %d)", len(lbtcAddress), eth.AddressLength) } @@ -23,7 +23,7 @@ func CalcTweakBytes( return nil, errors.Errorf("bad ToAddress (got %d bytes, expected %d)", len(toAddress), eth.AddressLength) } return DepositTweak(lbtcAddress, toAddress, chainId, auxData) - case chainid.SuiChainId: + case chainid.SuiLChainId: if len(lbtcAddress) != SuiAddressLength { return nil, errors.Errorf("bad LbtcAddress (got %d bytes, expected %d)", len(lbtcAddress), SuiAddressLength) } From e9f39c9ebbfea00154404142da452df3166d7977 Mon Sep 17 00:00:00 2001 From: Antonio Russo Date: Thu, 13 Feb 2025 14:23:19 +0100 Subject: [PATCH 5/7] feat: use address types for all operations remove go-ethereum dependency --- deposit_addr.go | 13 ++++++------- deposit_addr_test.go | 24 +++++++++++++----------- go.mod | 4 +--- go.sum | 14 ++------------ tweak_bytes.go | 43 +++++++++++++++++++------------------------ 5 files changed, 41 insertions(+), 57 deletions(-) diff --git a/deposit_addr.go b/deposit_addr.go index 3420a14..0f0d793 100644 --- a/deposit_addr.go +++ b/deposit_addr.go @@ -5,12 +5,11 @@ import ( "hash" "github.com/btcsuite/btcd/chaincfg" - eth "github.com/ethereum/go-ethereum/common" + "github.com/lombard-finance/chain/address" "github.com/lombard-finance/chain/chainid" "github.com/pkg/errors" ) -type Address = eth.Address type Sha256 = hash.Hash const ( @@ -47,7 +46,7 @@ func depositHasher() Sha256 { // big-endian identifier of the chain, LBTCAddress and WalletAddress are byte arrays representing // the respective addresses on the selected chain, and AuxData is a 32-byte value encoding // chain-agnostic auxiliary data. -func DepositTweak(lbtcContract, wallet []byte, chainId chainid.LChainId, auxData []byte) ([]byte, error) { +func DepositTweak(lbtcContract, wallet address.Address, chainId chainid.LChainId, auxData []byte) ([]byte, error) { if len(auxData) != AuxDataSize { return nil, errors.Errorf("wrong size for auxData (got %v, want %v)", len(auxData), AuxDataSize) } @@ -65,10 +64,10 @@ func DepositTweak(lbtcContract, wallet []byte, chainId chainid.LChainId, auxData h.Write(chainId.Bytes()) // LBTC contract address - h.Write(lbtcContract) + h.Write(lbtcContract.Bytes()) // Destination wallet address - h.Write(wallet) + h.Write(wallet.Bytes()) return h.Sum(nil), nil } @@ -79,7 +78,7 @@ func DepositTweak(lbtcContract, wallet []byte, chainId chainid.LChainId, auxData // - 'lbtcContract' is the address of the LBTC contract or object on the destination chain // - 'wallet' is the address that will claim the deposit on the destination chain // - 'chainId' is the chain id for the target chain as defined in the Lombard documentation -func DepositSegwitPubkey(pk *PublicKey, lbtcContract, wallet []byte, chainId chainid.LChainId, auxData []byte) (*PublicKey, error) { +func DepositSegwitPubkey(pk *PublicKey, lbtcContract, wallet address.Address, chainId chainid.LChainId, auxData []byte) (*PublicKey, error) { // compute tweak bytes tweakBytes, err := DepositTweak(lbtcContract, wallet, chainId, auxData) if err != nil { @@ -91,7 +90,7 @@ func DepositSegwitPubkey(pk *PublicKey, lbtcContract, wallet []byte, chainId cha // DepositSegwitAddr Compute the segwit deposit address to be used for a deposit on the specified chain. // See depositSegwitPubkey doc for argument descriptions. -func DepositSegwitAddr(pk *PublicKey, bridge, wallet []byte, chainId chainid.LChainId, auxData []byte, net *chaincfg.Params) (string, error) { +func DepositSegwitAddr(pk *PublicKey, bridge, wallet address.Address, chainId chainid.LChainId, auxData []byte, net *chaincfg.Params) (string, error) { // compute the pubkey tpk, err := DepositSegwitPubkey(pk, bridge, wallet, chainId, auxData) if err != nil { diff --git a/deposit_addr_test.go b/deposit_addr_test.go index 686d667..b62a95e 100644 --- a/deposit_addr_test.go +++ b/deposit_addr_test.go @@ -8,11 +8,11 @@ import ( "testing" "github.com/decred/dcrd/dcrec/secp256k1/v4" + "github.com/lombard-finance/chain/address" "github.com/lombard-finance/chain/chainid" "github.com/stretchr/testify/require" "github.com/btcsuite/btcd/chaincfg" - eth "github.com/ethereum/go-ethereum/common" ) type TestVal struct { @@ -197,8 +197,10 @@ func TestEthTweakValueRustKat(t *testing.T) { v4 := sha256.Sum256(v3[:]) hashVal = v4 - lbtcContractAddr := eth.BytesToAddress(v1[:20]) - walletAddr := eth.BytesToAddress(v2[:20]) + lbtcContract, err := address.NewEvmAddress(v1[:20]) + require.NoError(t, err, "error in test configuration for lbtc contract") + wallet, err := address.NewEvmAddress(v2[:20]) + require.NoError(t, err, "error in test configuration for wallet") chainIdU64 := binary.BigEndian.Uint64(v3[:8]) var chainIdBytes [32]byte binary.BigEndian.PutUint64(chainIdBytes[24:], chainIdU64) @@ -206,21 +208,21 @@ func TestEthTweakValueRustKat(t *testing.T) { auxData := v4 // check tweak result - tweak, err := DepositTweak(lbtcContractAddr.Bytes(), walletAddr.Bytes(), chainId, auxData[:]) + tweak, err := DepositTweak(lbtcContract, wallet, chainId, auxData[:]) if err != nil { panic(fmt.Sprintf("error computing deposit tweak: %v", err)) } tweakString := hex.EncodeToString(tweak) // check deposit pubkey result - tpk, err := DepositSegwitPubkey(pk, lbtcContractAddr.Bytes(), walletAddr.Bytes(), chainId, auxData[:]) + tpk, err := DepositSegwitPubkey(pk, lbtcContract, wallet, chainId, auxData[:]) if err != nil { panic(fmt.Sprintf("error tweaking pubkey: %v", err)) } tpkString := hex.EncodeToString(tpk.SerializeCompressed()) // check segwit address - segwitAddr, err := DepositSegwitAddr(pk, lbtcContractAddr.Bytes(), walletAddr.Bytes(), chainId, auxData[:], params) + segwitAddr, err := DepositSegwitAddr(pk, lbtcContract, wallet, chainId, auxData[:], params) if err != nil { panic(fmt.Sprintf("error tweaking addr: %v", err)) } @@ -307,9 +309,9 @@ func TestWithReferenceValues(t *testing.T) { for _, rf := range referenceValues { t.Run(rf.testLabel, func(t *testing.T) { - lbtcContractBytes, err := hex.DecodeString(rf.lbtcContract) + lbtcContract, err := address.NewSuiAddressFromHex(rf.lbtcContract) require.NoError(t, err) - walletBytes, err := hex.DecodeString(rf.wallet) + wallet, err := address.NewSuiAddressFromHex(rf.wallet) require.NoError(t, err) chainId, err := chainid.NewLChainIdFromHex(rf.chainId) require.NoError(t, err) @@ -317,17 +319,17 @@ func TestWithReferenceValues(t *testing.T) { require.NoError(t, err) // check tweak result - tweak, err := DepositTweak(lbtcContractBytes, walletBytes, chainId, auxDataBytes) + tweak, err := DepositTweak(lbtcContract, wallet, chainId, auxDataBytes) require.NoError(t, err, "error on deposit tweak calculation") require.Equal(t, rf.expectedTweak, hex.EncodeToString(tweak)) // check deposit pubkey result - tpk, err := DepositSegwitPubkey(pk, lbtcContractBytes, walletBytes, chainId, auxDataBytes) + tpk, err := DepositSegwitPubkey(pk, lbtcContract, wallet, chainId, auxDataBytes) require.NoError(t, err, "error tweaking the public key") require.Equal(t, rf.expectedPubkey, hex.EncodeToString(tpk.SerializeCompressed())) // check segwit address - segwitAddr, err := DepositSegwitAddr(pk, lbtcContractBytes, walletBytes, chainId, auxDataBytes, params) + segwitAddr, err := DepositSegwitAddr(pk, lbtcContract, wallet, chainId, auxDataBytes, params) require.NoError(t, err, "error deriving address") require.Equal(t, rf.expectedSegwitAddr, segwitAddr) }) diff --git a/go.mod b/go.mod index 15a8115..4654804 100644 --- a/go.mod +++ b/go.mod @@ -6,7 +6,7 @@ require ( github.com/btcsuite/btcd v0.24.0 github.com/btcsuite/btcd/btcutil v1.1.5 github.com/decred/dcrd/dcrec/secp256k1/v4 v4.2.0 - github.com/ethereum/go-ethereum v1.14.5 + github.com/lombard-finance/chain v0.1.0 github.com/pkg/errors v0.9.1 github.com/stretchr/testify v1.9.0 ) @@ -15,8 +15,6 @@ require ( github.com/btcsuite/btcd/btcec/v2 v2.2.0 // indirect github.com/btcsuite/btcd/chaincfg/chainhash v1.1.0 // indirect github.com/davecgh/go-spew v1.1.1 // indirect - github.com/holiman/uint256 v1.2.4 // indirect - github.com/lombard-finance/chain v0.0.0-20250212131652-4b532e884d03 // indirect github.com/pmezard/go-difflib v1.0.0 // indirect golang.org/x/crypto v0.22.0 // indirect golang.org/x/sys v0.20.0 // indirect diff --git a/go.sum b/go.sum index 67b0735..23d416d 100644 --- a/go.sum +++ b/go.sum @@ -34,8 +34,6 @@ github.com/decred/dcrd/dcrec/secp256k1/v4 v4.0.1/go.mod h1:hyedUtir6IdtD/7lIxGeC github.com/decred/dcrd/dcrec/secp256k1/v4 v4.2.0 h1:8UrgZ3GkP4i/CLijOJx79Yu+etlyjdBU4sfcs2WYQMs= github.com/decred/dcrd/dcrec/secp256k1/v4 v4.2.0/go.mod h1:v57UDF4pDQJcEfFUCRop3lJL149eHGSe9Jvczhzjo/0= github.com/decred/dcrd/lru v1.0.0/go.mod h1:mxKOwFd7lFjN2GZYsiz/ecgqR6kkYAl+0pz0tEMk218= -github.com/ethereum/go-ethereum v1.14.5 h1:szuFzO1MhJmweXjoM5nSAeDvjNUH3vIQoMzzQnfvjpw= -github.com/ethereum/go-ethereum v1.14.5/go.mod h1:VEDGGhSxY7IEjn98hJRFXl/uFvpRgbIIf2PpXiyGGgc= github.com/fsnotify/fsnotify v1.4.7/go.mod h1:jwhsz4b93w/PPRr/qN1Yymfu8t87LnFCMoQvtojpjFo= github.com/fsnotify/fsnotify v1.4.9/go.mod h1:znqG4EE+3YCdAaPaxE2ZRY/06pZUdp0tY4IgpuI1SZQ= github.com/golang/protobuf v1.2.0/go.mod h1:6lQm79b+lXiMfvg/cZm0SGofjICqVBUtrP5yJMmIC1U= @@ -50,21 +48,13 @@ github.com/google/go-cmp v0.3.0/go.mod h1:8QqcDgzrUqlUb/G2PQTWiueGozuR1884gddMyw github.com/google/go-cmp v0.3.1/go.mod h1:8QqcDgzrUqlUb/G2PQTWiueGozuR1884gddMywk6iLU= github.com/google/go-cmp v0.4.0/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE= github.com/gorilla/websocket v1.5.0/go.mod h1:YR8l580nyteQvAITg2hZ9XVh4b55+EU/adAjf1fMHhE= -github.com/holiman/uint256 v1.2.4 h1:jUc4Nk8fm9jZabQuqr2JzednajVmBpC+oiTiXZJEApU= -github.com/holiman/uint256 v1.2.4/go.mod h1:EOMSn4q6Nyt9P6efbI3bueV4e1b3dGlUCXeiRV4ng7E= github.com/hpcloud/tail v1.0.0/go.mod h1:ab1qPbhIpdTxEkNHXyeSf5vhxWSCs/tWer42PpOxQnU= github.com/jessevdk/go-flags v0.0.0-20141203071132-1679536dcc89/go.mod h1:4FA24M0QyGHXBuZZK/XkWh8h0e1EYbRYJSGM75WSRxI= github.com/jessevdk/go-flags v1.4.0/go.mod h1:4FA24M0QyGHXBuZZK/XkWh8h0e1EYbRYJSGM75WSRxI= github.com/jrick/logrotate v1.0.0/go.mod h1:LNinyqDIJnpAur+b8yyulnQw/wDuN1+BYKlTRt3OuAQ= github.com/kkdai/bstream v0.0.0-20161212061736-f391b8402d23/go.mod h1:J+Gs4SYgM6CZQHDETBtE9HaSEkGmuNXF86RwHhHUvq4= -github.com/lombard-finance/chain v0.0.0-20250204210857-e6c20ba69b01 h1:IC52aY17/XdtTDp/w+e/M7qYmtwWm51siGbmu6MtHWI= -github.com/lombard-finance/chain v0.0.0-20250204210857-e6c20ba69b01/go.mod h1:lZGCpGc0TcJk6bz6Y8b4oKHQFelh5Yulq7qWvYOqK4Q= -github.com/lombard-finance/chain v0.0.0-20250205154603-6b313aec67dc h1:SzKftIzvWPKyvS801EczHzgD33lU10LaD+oPoLLnbv0= -github.com/lombard-finance/chain v0.0.0-20250205154603-6b313aec67dc/go.mod h1:lZGCpGc0TcJk6bz6Y8b4oKHQFelh5Yulq7qWvYOqK4Q= -github.com/lombard-finance/chain v0.0.0-20250207120945-2d26469daa26 h1:cqJpG+WYSnKWVw2DCb4Yy5Rf3whDfTGSbuPxhudFo78= -github.com/lombard-finance/chain v0.0.0-20250207120945-2d26469daa26/go.mod h1:lZGCpGc0TcJk6bz6Y8b4oKHQFelh5Yulq7qWvYOqK4Q= -github.com/lombard-finance/chain v0.0.0-20250212131652-4b532e884d03 h1:CZ+2ZWDhkO5oZs+rtj267Cp6Gn+LSI4s2Iwa04lTVag= -github.com/lombard-finance/chain v0.0.0-20250212131652-4b532e884d03/go.mod h1:lZGCpGc0TcJk6bz6Y8b4oKHQFelh5Yulq7qWvYOqK4Q= +github.com/lombard-finance/chain v0.1.0 h1:JmsUHd4bvzmyLHw61Fsxs11ifLsuKGAq5HEZPyUM7q8= +github.com/lombard-finance/chain v0.1.0/go.mod h1:lZGCpGc0TcJk6bz6Y8b4oKHQFelh5Yulq7qWvYOqK4Q= github.com/nxadm/tail v1.4.4/go.mod h1:kenIhsEOeOJmVchQTgglprH7qJGnHDVpk1VPCcaMI8A= github.com/onsi/ginkgo v1.6.0/go.mod h1:lLunBs/Ym6LB5Z9jYTR76FiuTmxDTDusOGeTQH+WWjE= github.com/onsi/ginkgo v1.7.0/go.mod h1:lLunBs/Ym6LB5Z9jYTR76FiuTmxDTDusOGeTQH+WWjE= diff --git a/tweak_bytes.go b/tweak_bytes.go index 41e19fd..3c958e9 100644 --- a/tweak_bytes.go +++ b/tweak_bytes.go @@ -1,37 +1,32 @@ package deposit_address import ( - eth "github.com/ethereum/go-ethereum/common" + "github.com/lombard-finance/chain/address" "github.com/lombard-finance/chain/chainid" "github.com/pkg/errors" ) -const SuiAddressLength = 32 - // CalcTweakBytes Compute the tweakBytes for a given request, dispatching on `blockchainType` func CalcTweakBytes( chainId chainid.LChainId, - toAddress, lbtcAddress, auxData []byte, + toAddress, lbtcAddress address.Address, + auxData []byte, ) ([]byte, error) { - - switch chainId.(type) { - case chainid.EVMLChainId: - if len(lbtcAddress) != eth.AddressLength { - return nil, errors.Errorf("bad LbtcAddress (got %d bytes, expected %d)", len(lbtcAddress), eth.AddressLength) - } - if len(toAddress) != eth.AddressLength { - return nil, errors.Errorf("bad ToAddress (got %d bytes, expected %d)", len(toAddress), eth.AddressLength) - } - return DepositTweak(lbtcAddress, toAddress, chainId, auxData) - case chainid.SuiLChainId: - if len(lbtcAddress) != SuiAddressLength { - return nil, errors.Errorf("bad LbtcAddress (got %d bytes, expected %d)", len(lbtcAddress), SuiAddressLength) - } - if len(toAddress) != SuiAddressLength { - return nil, errors.Errorf("bad ToAddress (got %d bytes, expected %d)", len(toAddress), SuiAddressLength) - } - return DepositTweak(lbtcAddress, toAddress, chainId, auxData) - default: - return nil, errors.Errorf("unsupported blockchain type: %s", chainId.Ecosystem().String()) + if chainId.Ecosystem() != toAddress.Ecosystem() { + return nil, errors.Errorf( + "ecosystem mismatch between chain (%s) and to address (%s:%s)", + chainId.Ecosystem().String(), + toAddress.Ecosystem().String(), + toAddress.String(), + ) + } + if chainId.Ecosystem() != lbtcAddress.Ecosystem() { + return nil, errors.Errorf( + "ecosystem mismatch between chain (%s) and LBTC address (%s:%s)", + chainId.Ecosystem().String(), + lbtcAddress.Ecosystem().String(), + lbtcAddress.String(), + ) } + return DepositTweak(lbtcAddress, toAddress, chainId, auxData) } From ec6c5453c2fbb97ce30d68abd6c43782b47fca3f Mon Sep 17 00:00:00 2001 From: Antonio Russo Date: Fri, 14 Feb 2025 18:01:31 +0100 Subject: [PATCH 6/7] test: use mainnet btc conf in mainnet sui addresses --- deposit_addr_test.go | 12 ++++++++---- 1 file changed, 8 insertions(+), 4 deletions(-) diff --git a/deposit_addr_test.go b/deposit_addr_test.go index b62a95e..6b066dc 100644 --- a/deposit_addr_test.go +++ b/deposit_addr_test.go @@ -254,6 +254,7 @@ var referenceValues = []struct { expectedTweak string expectedPubkey string expectedSegwitAddr string + btcParams chaincfg.Params }{ { testLabel: "Sui Testnet - 1", @@ -265,6 +266,7 @@ var referenceValues = []struct { expectedTweak: "c5f14fe401d015c4ea34632e6d775b751e1925fe718e6b339d2a74689b3a0609", expectedPubkey: "02a2188a8c2449e16c3f50aab677c8be90916a7c7e6ab25a88222fc846257c28fb", expectedSegwitAddr: "tb1q9sjdz0vpnsshhule5kkxvfggvq8lznpck4tay0", + btcParams: chaincfg.SigNetParams, }, { testLabel: "Sui Testnet - 2", @@ -276,6 +278,7 @@ var referenceValues = []struct { expectedTweak: "a6d4eb9bcfa5c683513b06fd531184792767f5355402ce23c6dab417696f6392", expectedPubkey: "0393e2e2e1acc9a702d8b62e990ed9eff4e36589c6cd44e48101a2d3c3c58d5abb", expectedSegwitAddr: "tb1q8uwc6au5765r9jttj949dg6f2qzcqt58svy8c0", + btcParams: chaincfg.SigNetParams, }, { testLabel: "Sui Mainnet - 1", @@ -286,7 +289,8 @@ var referenceValues = []struct { chainId: "0100000000000000000000000000000000000000000000000000000035834a8a", expectedTweak: "35b7205e7d5f1b077091f3164e5daed121f4bb27799c57d9acd976a4044a18bd", expectedPubkey: "02744518bc0dafc22c494f7dc9ec780fa6d9ae53d3be720ee003f672edfba1063b", - expectedSegwitAddr: "tb1qk5xk9gfr5r8l57ma2euyyvc8h7kc9etlry3a75", + expectedSegwitAddr: "bc1qk5xk9gfr5r8l57ma2euyyvc8h7kc9etlfz2w98", + btcParams: chaincfg.MainNetParams, }, { testLabel: "Sui Mainnet - 2", @@ -297,7 +301,8 @@ var referenceValues = []struct { chainId: "0100000000000000000000000000000000000000000000000000000035834a8a", expectedTweak: "e454f631af7c2f235c372be61a63c58a1b94832e0240cf1a06b9e657df5d9c13", expectedPubkey: "034a915f6ac8d6c6920a754392338aa2f41a4070d30564d6af3749d80a9b58eb81", - expectedSegwitAddr: "tb1qagvmd7y5x5hkkn6avva5mv0thhlnn6ktk30j67", + expectedSegwitAddr: "bc1qagvmd7y5x5hkkn6avva5mv0thhlnn6ktuh5ppd", + btcParams: chaincfg.MainNetParams, }, } @@ -305,7 +310,6 @@ func TestWithReferenceValues(t *testing.T) { // just an initial seed to generate constant data for all tests hashVal := sha256.Sum256([]byte("segwit_lombard_tweak_test_rs")) pk := secp256k1.PrivKeyFromBytes(hashVal[:]).PubKey() - params := &chaincfg.SigNetParams for _, rf := range referenceValues { t.Run(rf.testLabel, func(t *testing.T) { @@ -329,7 +333,7 @@ func TestWithReferenceValues(t *testing.T) { require.Equal(t, rf.expectedPubkey, hex.EncodeToString(tpk.SerializeCompressed())) // check segwit address - segwitAddr, err := DepositSegwitAddr(pk, lbtcContract, wallet, chainId, auxDataBytes, params) + segwitAddr, err := DepositSegwitAddr(pk, lbtcContract, wallet, chainId, auxDataBytes, &rf.btcParams) require.NoError(t, err, "error deriving address") require.Equal(t, rf.expectedSegwitAddr, segwitAddr) }) From f894e539ddf5335bf7312ae48a3c06f416e23f3c Mon Sep 17 00:00:00 2001 From: Antonio Russo Date: Fri, 14 Feb 2025 19:32:36 +0100 Subject: [PATCH 7/7] feat: rename lombard-finance/chain to ledger-utils --- deposit_addr.go | 4 ++-- deposit_addr_test.go | 6 +++--- go.mod | 2 +- go.sum | 4 ++-- tweak_bytes.go | 4 ++-- 5 files changed, 10 insertions(+), 10 deletions(-) diff --git a/deposit_addr.go b/deposit_addr.go index 0f0d793..738f03d 100644 --- a/deposit_addr.go +++ b/deposit_addr.go @@ -5,8 +5,8 @@ import ( "hash" "github.com/btcsuite/btcd/chaincfg" - "github.com/lombard-finance/chain/address" - "github.com/lombard-finance/chain/chainid" + "github.com/lombard-finance/ledger-utils/address" + "github.com/lombard-finance/ledger-utils/chainid" "github.com/pkg/errors" ) diff --git a/deposit_addr_test.go b/deposit_addr_test.go index 6b066dc..c2806c6 100644 --- a/deposit_addr_test.go +++ b/deposit_addr_test.go @@ -8,8 +8,8 @@ import ( "testing" "github.com/decred/dcrd/dcrec/secp256k1/v4" - "github.com/lombard-finance/chain/address" - "github.com/lombard-finance/chain/chainid" + "github.com/lombard-finance/ledger-utils/address" + "github.com/lombard-finance/ledger-utils/chainid" "github.com/stretchr/testify/require" "github.com/btcsuite/btcd/chaincfg" @@ -204,7 +204,7 @@ func TestEthTweakValueRustKat(t *testing.T) { chainIdU64 := binary.BigEndian.Uint64(v3[:8]) var chainIdBytes [32]byte binary.BigEndian.PutUint64(chainIdBytes[24:], chainIdU64) - chainId := chainid.NewUnsafeLChainId(chainIdBytes[:]) + chainId, _ := chainid.NewLChainId(chainIdBytes[:]) auxData := v4 // check tweak result diff --git a/go.mod b/go.mod index 4654804..087e890 100644 --- a/go.mod +++ b/go.mod @@ -6,7 +6,7 @@ require ( github.com/btcsuite/btcd v0.24.0 github.com/btcsuite/btcd/btcutil v1.1.5 github.com/decred/dcrd/dcrec/secp256k1/v4 v4.2.0 - github.com/lombard-finance/chain v0.1.0 + github.com/lombard-finance/ledger-utils v0.2.0 github.com/pkg/errors v0.9.1 github.com/stretchr/testify v1.9.0 ) diff --git a/go.sum b/go.sum index 23d416d..30bfcb0 100644 --- a/go.sum +++ b/go.sum @@ -53,8 +53,8 @@ github.com/jessevdk/go-flags v0.0.0-20141203071132-1679536dcc89/go.mod h1:4FA24M github.com/jessevdk/go-flags v1.4.0/go.mod h1:4FA24M0QyGHXBuZZK/XkWh8h0e1EYbRYJSGM75WSRxI= github.com/jrick/logrotate v1.0.0/go.mod h1:LNinyqDIJnpAur+b8yyulnQw/wDuN1+BYKlTRt3OuAQ= github.com/kkdai/bstream v0.0.0-20161212061736-f391b8402d23/go.mod h1:J+Gs4SYgM6CZQHDETBtE9HaSEkGmuNXF86RwHhHUvq4= -github.com/lombard-finance/chain v0.1.0 h1:JmsUHd4bvzmyLHw61Fsxs11ifLsuKGAq5HEZPyUM7q8= -github.com/lombard-finance/chain v0.1.0/go.mod h1:lZGCpGc0TcJk6bz6Y8b4oKHQFelh5Yulq7qWvYOqK4Q= +github.com/lombard-finance/ledger-utils v0.2.0 h1:nrIg5g9tu3jH1ViAnvMXqpA00cIs/CjkqU2Rb7Ujqv8= +github.com/lombard-finance/ledger-utils v0.2.0/go.mod h1:a61awHF3EakbFArqyz8UaQt120DUJH3wEOaI671SThs= github.com/nxadm/tail v1.4.4/go.mod h1:kenIhsEOeOJmVchQTgglprH7qJGnHDVpk1VPCcaMI8A= github.com/onsi/ginkgo v1.6.0/go.mod h1:lLunBs/Ym6LB5Z9jYTR76FiuTmxDTDusOGeTQH+WWjE= github.com/onsi/ginkgo v1.7.0/go.mod h1:lLunBs/Ym6LB5Z9jYTR76FiuTmxDTDusOGeTQH+WWjE= diff --git a/tweak_bytes.go b/tweak_bytes.go index 3c958e9..260593d 100644 --- a/tweak_bytes.go +++ b/tweak_bytes.go @@ -1,8 +1,8 @@ package deposit_address import ( - "github.com/lombard-finance/chain/address" - "github.com/lombard-finance/chain/chainid" + "github.com/lombard-finance/ledger-utils/address" + "github.com/lombard-finance/ledger-utils/chainid" "github.com/pkg/errors" )