Skip to content

Commit

Permalink
feat: use address types for all operations
Browse files Browse the repository at this point in the history
remove go-ethereum dependency
  • Loading branch information
russanto committed Feb 13, 2025
1 parent 2fbf0b8 commit e9f39c9
Show file tree
Hide file tree
Showing 5 changed files with 41 additions and 57 deletions.
13 changes: 6 additions & 7 deletions deposit_addr.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 (
Expand Down Expand Up @@ -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)
}
Expand All @@ -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
}
Expand All @@ -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 {
Expand All @@ -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 {
Expand Down
24 changes: 13 additions & 11 deletions deposit_addr_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -197,30 +197,32 @@ 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)
chainId := chainid.NewUnsafeLChainId(chainIdBytes[:])
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))
}
Expand Down Expand Up @@ -307,27 +309,27 @@ 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)
auxDataBytes, err := hex.DecodeString(rf.auxData)
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)
})
Expand Down
4 changes: 1 addition & 3 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -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
)
Expand All @@ -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
Expand Down
14 changes: 2 additions & 12 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -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=
Expand All @@ -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=
Expand Down
43 changes: 19 additions & 24 deletions tweak_bytes.go
Original file line number Diff line number Diff line change
@@ -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)
}

0 comments on commit e9f39c9

Please sign in to comment.