From aaa0dff1c8382391a10013fab5ec585a75d8c0cf Mon Sep 17 00:00:00 2001 From: "zhiwei.li" Date: Mon, 17 Jun 2024 15:30:30 +0800 Subject: [PATCH] feat: support for USDC-associated coins --- common/coin.go | 417 ++++++++++++++++++++++-------------------- common/verify.go | 9 +- common/verify_test.go | 2 +- 3 files changed, 229 insertions(+), 199 deletions(-) diff --git a/common/coin.go b/common/coin.go index 6a3ffa5..2648556 100644 --- a/common/coin.go +++ b/common/coin.go @@ -32,7 +32,7 @@ var ( // UTXO "BTC": "BTC", "USDT-OMNI": "BTC", - "BCH": "BCH", + "BCHN": "BCH", "BCHA": "BCHA", "BSV": "BTC", "LTC": "LTC", @@ -46,51 +46,54 @@ var ( "ZEC": "ZEC", // ETH - "ETH": "ETH", - "ETH-ARBITRUM": "ETH", - "ETH-OPTIMISM": "ETH", - "USDT-ERC20": "ETH", - "USDT-POLY": "ETH", - "USDT-AVAXC": "ETH", - "USDT-ARBITRUM": "ETH", - "USDT-OPTIMISM": "ETH", - "USDT-OKC20": "ETH", - "USDC": "ETH", - "POLY-USDC": "ETH", - "USDC-AVAXC": "ETH", - "USDC-ARBITRUM": "ETH", - "USDC-OPTIMISM": "ETH", - "ETC": "ETH", - "OKB-OKC20": "ETH", - "LTCK-OKC20": "ETH", - "FILK-OKC20": "ETH", - "USDC-OKC20": "ETH", - "SHIBK-KIP20": "ETH", - "DOTK-OKC20": "ETH", - "ETCK-KIP20": "ETH", - "XRPK-KIP20": "ETH", - "UNIK-OKC20": "ETH", - "BCHK-KIP20": "ETH", - "BABYDOGE-KIP20": "ETH", - "LINKK-OKC20": "ETH", - "TRXK-KIP20": "ETH", - "BABYDOGE-BSC": "ETH", - "SHIB": "ETH", - "UNI": "ETH", - "LINK": "ETH", - "ETHW": "ETH", - "BLUR": "ETH", - "MATIC": "ETH", - "PEOPLE": "ETH", - "OKT": "ETH", - "OKB": "ETH", - "OPTIMISM": "ETH", - "ETH-LINEA": "ETH", - "BASE": "ETH", - "OKB-X1": "ETH", - "OKB-X1-ETH": "ETH", - "OKB-X1-USDT": "ETH", - "OKB-X1-USDC": "ETH", + "ETH": "ETH", + "ETH-ARBITRUM": "ETH", + "ETH-OPTIMISM": "ETH", + "USDT-ERC20": "ETH", + "USDT-POLY": "ETH", + "USDT-AVAXC": "ETH", + "USDT-ARBITRUM": "ETH", + "USDT-OPTIMISM": "ETH", + "USDT-OKC20": "ETH", + "USDC": "ETH", + "POLY-USDC": "ETH", + "USDC-AVAXC": "ETH", + "USDC-ARBITRUM": "ETH", + "USDC-OPTIMISM": "ETH", + "ETC": "ETH", + "OKB-OKC20": "ETH", + "LTCK-OKC20": "ETH", + "FILK-OKC20": "ETH", + "USDC-OKC20": "ETH", + "SHIBK-KIP20": "ETH", + "DOTK-OKC20": "ETH", + "ETCK-KIP20": "ETH", + "XRPK-KIP20": "ETH", + "UNIK-OKC20": "ETH", + "BCHK-KIP20": "ETH", + "BABYDOGE-KIP20": "ETH", + "LINKK-OKC20": "ETH", + "TRXK-KIP20": "ETH", + "BABYDOGE-BSC": "ETH", + "SHIB": "ETH", + "UNI": "ETH", + "LINK": "ETH", + "ETHW": "ETH", + "BLUR": "ETH", + "MATIC": "ETH", + "PEOPLE": "ETH", + "OKT": "ETH", + "OKB": "ETH", + "OPTIMISM": "ETH", + "ETH-LINEA": "ETH", + "BASE": "ETH", + "OKB-X1": "ETH", + "OKB-X1-ETH": "ETH", + "OKB-X1-USDT": "ETH", + "OKB-X1-USDC": "ETH", + "POLY-USDC-3359": "ETH", + "USDC-OPTIMISM-FF85": "ETH", + "USDC-ARBITRUM-NATIVE": "ETH", // BETH "BETH": "BETH", @@ -108,6 +111,7 @@ var ( // ED25519 "SOL": "SOL", + "USDC-SPL": "SOL", "APTOS": "APTOS", "TONCOIN-NEW": "TON", "DOT": "DOT", @@ -137,37 +141,40 @@ var ( "ZEC": UTXOCoinType, // ETH - "ETH": EvmCoinTye, - "ETH-ARBITRUM": EvmCoinTye, - "ETH-OPTIMISM": EvmCoinTye, - "USDT-ERC20": EvmCoinTye, - "USDT-POLY": EvmCoinTye, - "USDT-AVAXC": EvmCoinTye, - "USDT-ARBITRUM": EvmCoinTye, - "USDT-OPTIMISM": EvmCoinTye, - "USDC": EvmCoinTye, - "POLY-USDC": EvmCoinTye, - "USDC-AVAXC": EvmCoinTye, - "USDC-ARBITRUM": EvmCoinTye, - "USDC-OPTIMISM": EvmCoinTye, - "ETC": EvmCoinTye, - "BABYDOGE-BSC": EvmCoinTye, - "SHIB": EvmCoinTye, - "UNI": EvmCoinTye, - "LINK": EvmCoinTye, - "ETHW": EvmCoinTye, - "BLUR": EvmCoinTye, - "MATIC": EvmCoinTye, - "PEOPLE": EvmCoinTye, - "OKB": EvmCoinTye, - "OPTIMISM": EvmCoinTye, - "FIL-EVM": EvmCoinTye, - "ETH-LINEA": EvmCoinTye, - "BASE": EvmCoinTye, - "OKB-X1": EvmCoinTye, - "OKB-X1-ETH": EvmCoinTye, - "OKB-X1-USDT": EvmCoinTye, - "OKB-X1-USDC": EvmCoinTye, + "ETH": EvmCoinTye, + "ETH-ARBITRUM": EvmCoinTye, + "ETH-OPTIMISM": EvmCoinTye, + "USDT-ERC20": EvmCoinTye, + "USDT-POLY": EvmCoinTye, + "USDT-AVAXC": EvmCoinTye, + "USDT-ARBITRUM": EvmCoinTye, + "USDT-OPTIMISM": EvmCoinTye, + "USDC": EvmCoinTye, + "POLY-USDC": EvmCoinTye, + "USDC-AVAXC": EvmCoinTye, + "USDC-ARBITRUM": EvmCoinTye, + "USDC-OPTIMISM": EvmCoinTye, + "ETC": EvmCoinTye, + "BABYDOGE-BSC": EvmCoinTye, + "SHIB": EvmCoinTye, + "UNI": EvmCoinTye, + "LINK": EvmCoinTye, + "ETHW": EvmCoinTye, + "BLUR": EvmCoinTye, + "MATIC": EvmCoinTye, + "PEOPLE": EvmCoinTye, + "OKB": EvmCoinTye, + "OPTIMISM": EvmCoinTye, + "FIL-EVM": EvmCoinTye, + "ETH-LINEA": EvmCoinTye, + "BASE": EvmCoinTye, + "OKB-X1": EvmCoinTye, + "OKB-X1-ETH": EvmCoinTye, + "OKB-X1-USDT": EvmCoinTye, + "OKB-X1-USDC": EvmCoinTye, + "POLY-USDC-3359": EvmCoinTye, + "USDC-OPTIMISM-FF85": EvmCoinTye, + "USDC-ARBITRUM-NATIVE": EvmCoinTye, // BETH "BETH": BethCoinType, @@ -200,6 +207,7 @@ var ( // ED25519 "SOL": Ed25519CoinType, + "USDC-SPL": Ed25519CoinType, "APTOS": Ed25519CoinType, "TONCOIN-NEW": Ed25519CoinType, "DOT": Ed25519CoinType, @@ -226,37 +234,40 @@ var ( "ZEC": ZecMessageSignatureHeader, // ETH - "ETH": EthMessageSignatureHeader, - "ETH-ARBITRUM": EthMessageSignatureHeader, - "ETH-OPTIMISM": EthMessageSignatureHeader, - "USDT-ERC20": EthMessageSignatureHeader, - "USDT-POLY": EthMessageSignatureHeader, - "USDT-AVAXC": EthMessageSignatureHeader, - "USDT-ARBITRUM": EthMessageSignatureHeader, - "USDT-OPTIMISM": EthMessageSignatureHeader, - "USDC": EthMessageSignatureHeader, - "POLY-USDC": EthMessageSignatureHeader, - "USDC-AVAXC": EthMessageSignatureHeader, - "USDC-ARBITRUM": EthMessageSignatureHeader, - "USDC-OPTIMISM": EthMessageSignatureHeader, - "ETC": EthMessageSignatureHeader, - "BABYDOGE-BSC": EthMessageSignatureHeader, - "SHIB": EthMessageSignatureHeader, - "UNI": EthMessageSignatureHeader, - "LINK": EthMessageSignatureHeader, - "ETHW": EthMessageSignatureHeader, - "BLUR": EthMessageSignatureHeader, - "MATIC": EthMessageSignatureHeader, - "PEOPLE": EthMessageSignatureHeader, - "OKB": EthMessageSignatureHeader, - "OPTIMISM": EthMessageSignatureHeader, - "FIL-EVM": EthMessageSignatureHeader, - "ETH-LINEA": EthMessageSignatureHeader, - "BASE": EthMessageSignatureHeader, - "OKB-X1": EthMessageSignatureHeader, - "OKB-X1-ETH": EthMessageSignatureHeader, - "OKB-X1-USDT": EthMessageSignatureHeader, - "OKB-X1-USDC": EthMessageSignatureHeader, + "ETH": EthMessageSignatureHeader, + "ETH-ARBITRUM": EthMessageSignatureHeader, + "ETH-OPTIMISM": EthMessageSignatureHeader, + "USDT-ERC20": EthMessageSignatureHeader, + "USDT-POLY": EthMessageSignatureHeader, + "USDT-AVAXC": EthMessageSignatureHeader, + "USDT-ARBITRUM": EthMessageSignatureHeader, + "USDT-OPTIMISM": EthMessageSignatureHeader, + "USDC": EthMessageSignatureHeader, + "POLY-USDC": EthMessageSignatureHeader, + "USDC-AVAXC": EthMessageSignatureHeader, + "USDC-ARBITRUM": EthMessageSignatureHeader, + "USDC-OPTIMISM": EthMessageSignatureHeader, + "ETC": EthMessageSignatureHeader, + "BABYDOGE-BSC": EthMessageSignatureHeader, + "SHIB": EthMessageSignatureHeader, + "UNI": EthMessageSignatureHeader, + "LINK": EthMessageSignatureHeader, + "ETHW": EthMessageSignatureHeader, + "BLUR": EthMessageSignatureHeader, + "MATIC": EthMessageSignatureHeader, + "PEOPLE": EthMessageSignatureHeader, + "OKB": EthMessageSignatureHeader, + "OPTIMISM": EthMessageSignatureHeader, + "FIL-EVM": EthMessageSignatureHeader, + "ETH-LINEA": EthMessageSignatureHeader, + "BASE": EthMessageSignatureHeader, + "OKB-X1": EthMessageSignatureHeader, + "OKB-X1-ETH": EthMessageSignatureHeader, + "OKB-X1-USDT": EthMessageSignatureHeader, + "OKB-X1-USDC": EthMessageSignatureHeader, + "POLY-USDC-3359": EthMessageSignatureHeader, + "USDC-OPTIMISM-FF85": EthMessageSignatureHeader, + "USDC-ARBITRUM-NATIVE": EthMessageSignatureHeader, // BETH "BETH": EthMessageSignatureHeader, @@ -289,6 +300,7 @@ var ( // ED25519 "SOL": OKXMessageSignatureHeader, + "USDC-SPL": OKXMessageSignatureHeader, "APTOS": OKXMessageSignatureHeader, "TONCOIN-NEW": OKXMessageSignatureHeader, "DOT": OKXMessageSignatureHeader, @@ -315,52 +327,55 @@ var ( "ZEC": "ZEC", // ETH - "ETH": "ETH", - "ETH-ARBITRUM": "ETH", - "ETH-OPTIMISM": "ETH", - "USDT": "USDT", - "USDT-ERC20": "USDT", - "USDT-POLY": "USDT", - "USDT-AVAXC": "USDT", - "USDT-ARBITRUM": "USDT", - "USDT-OPTIMISM": "USDT", - "USDT-OKC20": "USDT", - "USDC": "USDC", - "POLY-USDC": "USDC", - "USDC-AVAXC": "USDC", - "USDC-ARBITRUM": "USDC", - "USDC-OPTIMISM": "USDC", - "ETC": "ETC", - "OKB-OKC20": "OKB", - "LTCK-OKC20": "LTC", - "FILK-OKC20": "FIL", - "USDC-OKC20": "USDC", - "SHIBK-KIP20": "SHIB", - "DOTK-OKC20": "DOT", - "ETCK-KIP20": "ETC", - "XRPK-KIP20": "RIPPLE", - "UNIK-OKC20": "UNI", - "BCHK-KIP20": "BCH", - "BABYDOGE-KIP20": "BABYDOGE", - "LINKK-OKC20": "LINK", - "TRXK-KIP20": "TRX", - "BABYDOGE-BSC": "BABYDOGE", - "SHIB": "SHIB", - "UNI": "UNI", - "LINK": "LINK", - "ETHW": "ETHW", - "BLUR": "BLUR", - "MATIC": "MATIC", - "PEOPLE": "PEOPLE", - "OKT": "OKT", - "OKB": "OKB", - "OPTIMISM": "OPTIMISM", - "ETH-LINEA": "ETH", - "BASE": "ETH", - "OKB-X1": "ETH", - "OKB-X1-ETH": "ETH", - "OKB-X1-USDT": "ETH", - "OKB-X1-USDC": "ETH", + "ETH": "ETH", + "ETH-ARBITRUM": "ETH", + "ETH-OPTIMISM": "ETH", + "USDT": "USDT", + "USDT-ERC20": "USDT", + "USDT-POLY": "USDT", + "USDT-AVAXC": "USDT", + "USDT-ARBITRUM": "USDT", + "USDT-OPTIMISM": "USDT", + "USDT-OKC20": "USDT", + "USDC": "USDC", + "POLY-USDC": "USDC", + "USDC-AVAXC": "USDC", + "USDC-ARBITRUM": "USDC", + "USDC-OPTIMISM": "USDC", + "ETC": "ETC", + "OKB-OKC20": "OKB", + "LTCK-OKC20": "LTC", + "FILK-OKC20": "FIL", + "USDC-OKC20": "USDC", + "SHIBK-KIP20": "SHIB", + "DOTK-OKC20": "DOT", + "ETCK-KIP20": "ETC", + "XRPK-KIP20": "RIPPLE", + "UNIK-OKC20": "UNI", + "BCHK-KIP20": "BCH", + "BABYDOGE-KIP20": "BABYDOGE", + "LINKK-OKC20": "LINK", + "TRXK-KIP20": "TRX", + "BABYDOGE-BSC": "BABYDOGE", + "SHIB": "SHIB", + "UNI": "UNI", + "LINK": "LINK", + "ETHW": "ETHW", + "BLUR": "BLUR", + "MATIC": "MATIC", + "PEOPLE": "PEOPLE", + "OKT": "OKT", + "OKB": "OKB", + "OPTIMISM": "OPTIMISM", + "ETH-LINEA": "ETH", + "BASE": "ETH", + "OKB-X1": "OKB", + "OKB-X1-ETH": "ETH", + "OKB-X1-USDT": "USDT", + "OKB-X1-USDC": "USDC", + "POLY-USDC-3359": "USDC", + "USDC-OPTIMISM-FF85": "USDC", + "USDC-ARBITRUM-NATIVE": "USDC", // BETH "BETH": "BETH", @@ -378,6 +393,7 @@ var ( // ED25519 "SOL": "SOL", + "USDC-SPL": "USDC", "APTOS": "APTOS", "TONCOIN-NEW": "TONCOIN-NEW", "DOT": "DOT", @@ -411,48 +427,55 @@ var ( "ZEC": 8, // ETH - "ETH": 18, - "ETH-ARBITRUM": 18, - "ETH-OPTIMISM": 18, - "USDT": 6, - "USDT-ERC20": 6, - "USDT-POLY": 6, - "USDT-AVAXC": 6, - "USDT-ARBITRUM": 6, - "USDT-OPTIMISM": 6, - "USDT-OKC20": 18, - "USDC": 6, - "POLY-USDC": 6, - "USDC-AVAXC": 6, - "USDC-ARBITRUM": 6, - "USDC-OPTIMISM": 6, - "ETC": 18, - "OKB-OKC20": 18, - "LTCK-OKC20": 18, - "FILK-OKC20": 18, - "USDC-OKC20": 18, - "SHIBK-KIP20": 18, - "DOTK-OKC20": 18, - "ETCK-KIP20": 18, - "XRPK-KIP20": 18, - "UNIK-OKC20": 18, - "BCHK-KIP20": 18, - "BABYDOGE-KIP20": 18, - "LINKK-OKC20": 18, - "TRXK-KIP20": 18, - "BABYDOGE-BSC": 18, - "SHIB": 18, - "UNI": 18, - "LINK": 18, - "ETHW": 18, - "BLUR": 18, - "MATIC": 18, - "PEOPLE": 18, - "OKT": 18, - "OKB": 18, - "OPTIMISM": 18, - "ETH-LINEA": 18, - "BASE": 18, + "ETH": 18, + "ETH-ARBITRUM": 18, + "ETH-OPTIMISM": 18, + "USDT": 6, + "USDT-ERC20": 6, + "USDT-POLY": 6, + "USDT-AVAXC": 6, + "USDT-ARBITRUM": 6, + "USDT-OPTIMISM": 6, + "USDT-OKC20": 18, + "USDC": 6, + "POLY-USDC": 6, + "USDC-AVAXC": 6, + "USDC-ARBITRUM": 6, + "USDC-OPTIMISM": 6, + "ETC": 18, + "OKB-OKC20": 18, + "LTCK-OKC20": 18, + "FILK-OKC20": 18, + "USDC-OKC20": 18, + "SHIBK-KIP20": 18, + "DOTK-OKC20": 18, + "ETCK-KIP20": 18, + "XRPK-KIP20": 18, + "UNIK-OKC20": 18, + "BCHK-KIP20": 18, + "BABYDOGE-KIP20": 18, + "LINKK-OKC20": 18, + "TRXK-KIP20": 18, + "BABYDOGE-BSC": 18, + "SHIB": 18, + "UNI": 18, + "LINK": 18, + "ETHW": 18, + "BLUR": 18, + "MATIC": 18, + "PEOPLE": 18, + "OKT": 18, + "OKB": 18, + "OPTIMISM": 18, + "ETH-LINEA": 18, + "BASE": 18, + "OKB-X1": 18, + "OKB-X1-ETH": 18, + "OKB-X1-USDT": 6, + "OKB-X1-USDC": 6, + "POLY-USDC-3359": 6, + "USDC-OPTIMISM-FF85": 6, + "USDC-ARBITRUM-NATIVE": 6, // BETH "BETH": 18, @@ -470,6 +493,7 @@ var ( // ED25519 "SOL": 9, + "USDC-SPL": 6, "APTOS": 8, "TONCOIN-NEW": 9, "DOT": 10, @@ -501,6 +525,7 @@ var ( "LUNC": true, "SOL": true, + "USDC-SPL": true, "APTOS": true, "TONCOIN-NEW": true, "DOT": true, diff --git a/common/verify.go b/common/verify.go index bb1c7ec..b6abaf3 100644 --- a/common/verify.go +++ b/common/verify.go @@ -286,8 +286,13 @@ func VerifyEd25519Coin(coin, addr, msg, sign, pubkey string) error { if ok := ed25519.Verify(pubkeyBytes, hash, res); !ok { return ErrInvalidSign } + + addrType, exist := PorCoinAddressTypeMap[coin] + if !exist { + return fmt.Errorf("invalid coin type %s, addr:%s", coin, addr) + } var recoverAddr string - switch coin { + switch addrType { case "SOL": out := [32]byte{} byteCount := len(pubkeyBytes) @@ -307,7 +312,7 @@ func VerifyEd25519Coin(coin, addr, msg, sign, pubkey string) error { re, _ := regexp.Compile("^0x0*") recoverAddr = re.ReplaceAllString(recoverAddr, "0x") - case "TONCOIN-NEW": + case "TON": a, err := tonWallet.AddressFromPubKey(pubkeyBytes, tonWallet.V3, tonWallet.DefaultSubwallet) if err != nil { return fmt.Errorf("%s, coin: %s, addr: %s, error: %v", ErrInvalidSign, coin, addr, err) diff --git a/common/verify_test.go b/common/verify_test.go index ae49bc4..3f3712e 100644 --- a/common/verify_test.go +++ b/common/verify_test.go @@ -35,7 +35,7 @@ func TestUtxoCoinSigToPubKey(t *testing.T) { want: "02b514e7ccc2845d3f1ca7181dacab0d1ac277616e753547922f82cc0cdfb5c691", }, { - coin: "BCH", + coin: "BCHN", addr: "393maTY7rQScy4SmYE1XSXUSgK73byhgfA", msg: "hello world", sign: "Hz+cZI5GfSzNSvBpna20diV47/rhlQMRQTNGZd9sI4UZQaWH4ZY3KJA4IlcP5bwuicO+myA4vLdiMkj7OU+rDpg=",