From 89236a51f0ed55d5c1470f0de3ee425b7d395a46 Mon Sep 17 00:00:00 2001 From: Rakeally <40327144+Rakeally@users.noreply.github.com> Date: Fri, 16 Jun 2023 20:31:37 +0100 Subject: [PATCH] polygon integration --- .../africa/ejara/trustdart/WalletHandler.kt | 22 +++++-- .../africa/ejara/trustdart/coins/BSC.kt | 41 ------------- .../africa/ejara/trustdart/coins/ETH.kt | 59 ++++++++++++++++++- .../africa/ejara/trustdart/coins/MATIC.kt | 39 ------------ example/lib/operations.dart | 39 +++++++++--- 5 files changed, 107 insertions(+), 93 deletions(-) delete mode 100644 android/src/main/kotlin/africa/ejara/trustdart/coins/BSC.kt delete mode 100644 android/src/main/kotlin/africa/ejara/trustdart/coins/MATIC.kt diff --git a/android/src/main/kotlin/africa/ejara/trustdart/WalletHandler.kt b/android/src/main/kotlin/africa/ejara/trustdart/WalletHandler.kt index 519dc9e..8922120 100644 --- a/android/src/main/kotlin/africa/ejara/trustdart/WalletHandler.kt +++ b/android/src/main/kotlin/africa/ejara/trustdart/WalletHandler.kt @@ -8,12 +8,13 @@ import TRX import XTZ import XLM import BNB -import BSC +//import BSC import DOGE -import MATIC +//import MATIC import africa.ejara.trustdart.utils.WalletError import africa.ejara.trustdart.utils.WalletValidateResponse +import wallet.core.jni.CoinType import wallet.core.jni.HDWallet class WalletHandler { @@ -28,11 +29,24 @@ class WalletHandler { "NEAR" to NEAR(), "XLM" to XLM(), "BNB" to BNB(), - "BSC" to BSC(), + "BSC" to ETH(), "DOGE" to DOGE(), - "MATIC" to MATIC(), + "MATIC" to ETH(), ) } +// val coins = mapOf( +// "BTC" to BTC(), +// "ETH" to ETH("ETH", CoinType.ETHEREUM), +// "XTZ" to XTZ(), +// "TRX" to TRX(), +// "SOL" to SOL(), +// "NEAR" to NEAR(), +// "XLM" to XLM(), +// "BNB" to BNB(), +// "BSC" to ETH("BSC", CoinType.SMARTCHAIN), +// "DOGE" to DOGE(), +// "MATIC" to ETH("MATIC", CoinType.POLYGON), +// ) fun getCoin(coin: String?): Coin { return coins[coin]!! diff --git a/android/src/main/kotlin/africa/ejara/trustdart/coins/BSC.kt b/android/src/main/kotlin/africa/ejara/trustdart/coins/BSC.kt deleted file mode 100644 index 43a978e..0000000 --- a/android/src/main/kotlin/africa/ejara/trustdart/coins/BSC.kt +++ /dev/null @@ -1,41 +0,0 @@ -import africa.ejara.trustdart.Coin -import com.google.protobuf.ByteString -import wallet.core.java.AnySigner -import africa.ejara.trustdart.Numeric -import africa.ejara.trustdart.utils.toHexByteArray -import wallet.core.jni.CoinType -import wallet.core.jni.HDWallet -import wallet.core.jni.proto.Ethereum.SigningOutput -import wallet.core.jni.proto.Ethereum - - -class BSC : Coin("BSC", CoinType.SMARTCHAIN) { - - override fun signTransaction( - path: String, - txData: Map, - mnemonic: String, - passphrase: String - ): String? { - val signingInput = Ethereum.SigningInput.newBuilder() - val wallet = HDWallet(mnemonic, passphrase) - signingInput.apply { - privateKey = ByteString.copyFrom(wallet.getKey(coinType, path).data()) - toAddress = txData["toAddress"] as String - chainId = ByteString.copyFrom((txData["chainID"] as String).toHexByteArray()) - nonce = ByteString.copyFrom((txData["nonce"] as String).toHexByteArray()) - gasPrice = ByteString.copyFrom((txData["gasPrice"] as String).toHexByteArray()) - gasLimit = ByteString.copyFrom((txData["gasLimit"] as String).toHexByteArray()) - transaction = Ethereum.Transaction.newBuilder().apply { - transfer = Ethereum.Transaction.Transfer.newBuilder().apply { - amount = ByteString.copyFrom((txData["amount"] as String).toHexByteArray()) - }.build() - }.build() - } - - val sign = AnySigner.sign(signingInput.build(), coinType, SigningOutput.parser()) - return Numeric.toHexString(sign.encoded.toByteArray()) - } - -} - diff --git a/android/src/main/kotlin/africa/ejara/trustdart/coins/ETH.kt b/android/src/main/kotlin/africa/ejara/trustdart/coins/ETH.kt index dbb92b8..a091bd2 100644 --- a/android/src/main/kotlin/africa/ejara/trustdart/coins/ETH.kt +++ b/android/src/main/kotlin/africa/ejara/trustdart/coins/ETH.kt @@ -1,4 +1,61 @@ import africa.ejara.trustdart.Coin +import com.google.protobuf.ByteString +import wallet.core.java.AnySigner +import africa.ejara.trustdart.Numeric +import africa.ejara.trustdart.utils.toHexByteArray import wallet.core.jni.CoinType +import wallet.core.jni.HDWallet +import wallet.core.jni.proto.Ethereum.SigningOutput +import wallet.core.jni.proto.Ethereum -class ETH : Coin("ETH", CoinType.ETHEREUM) \ No newline at end of file +open class ETH: Coin("ETH", CoinType.ETHEREUM) { + + override fun signTransaction( + path: String, + txData: Map, + mnemonic: String, + passphrase: String + ): String? { + val wallet = HDWallet(mnemonic, passphrase) + val cmd: String? = txData["cmd"] as String? + val signingInput = Ethereum.SigningInput.newBuilder() + + signingInput.apply { + privateKey = ByteString.copyFrom(wallet.getKey(coinType, path).data()) + toAddress = txData["toAddress"] as String + chainId = ByteString.copyFrom((txData["chainId"] as String).toHexByteArray()) + nonce = ByteString.copyFrom((txData["nonce"] as String).toHexByteArray()) + gasPrice = ByteString.copyFrom((txData["gasPrice"] as String).toHexByteArray()) + gasLimit = ByteString.copyFrom((txData["gasLimit"] as String).toHexByteArray()) + } + when (cmd) { + "ERC20" -> { + + var transaction = Ethereum.Transaction.newBuilder().apply { + erc20Transfer = Ethereum.Transaction.ERC20Transfer.newBuilder().apply { + to = txData["contractAddress"] as String + amount = ByteString.copyFrom((txData["amount"] as String).toHexByteArray()) + }.build() + }.build() + + signingInput.transaction = transaction + } + else -> { + var transaction = Ethereum.Transaction.newBuilder().apply { + transfer = Ethereum.Transaction.Transfer.newBuilder().apply { + amount = ByteString.copyFrom((txData["amount"] as String).toHexByteArray()) + }.build() + }.build() + + + signingInput.setTransaction(transaction) + } + + } + val sign = AnySigner.sign(signingInput.build(), coinType, SigningOutput.parser()) + + return Numeric.toHexString(sign.encoded.toByteArray()) + + } + +} \ No newline at end of file diff --git a/android/src/main/kotlin/africa/ejara/trustdart/coins/MATIC.kt b/android/src/main/kotlin/africa/ejara/trustdart/coins/MATIC.kt deleted file mode 100644 index de57004..0000000 --- a/android/src/main/kotlin/africa/ejara/trustdart/coins/MATIC.kt +++ /dev/null @@ -1,39 +0,0 @@ -import africa.ejara.trustdart.Coin -import com.google.protobuf.ByteString -import wallet.core.java.AnySigner -import africa.ejara.trustdart.Numeric -import africa.ejara.trustdart.utils.toHexByteArray -import wallet.core.jni.CoinType -import wallet.core.jni.HDWallet -import wallet.core.jni.proto.Ethereum.SigningOutput -import wallet.core.jni.proto.Ethereum - -class MATIC : Coin("MATIC", CoinType.POLYGON) { - - override fun signTransaction( - path: String, - txData: Map, - mnemonic: String, - passphrase: String - ): String? { - val signingInput = Ethereum.SigningInput.newBuilder() - val wallet = HDWallet(mnemonic, passphrase) - signingInput.apply { - privateKey = ByteString.copyFrom(wallet.getKey(coinType, path).data()) - toAddress = txData["toAddress"] as String - chainId = ByteString.copyFrom((txData["chainId"] as String).toHexByteArray()) - nonce = ByteString.copyFrom((txData["nonce"] as String).toHexByteArray()) - gasPrice = ByteString.copyFrom((txData["gasPrice"] as String).toHexByteArray()) - gasLimit = ByteString.copyFrom((txData["gasLimit"] as String).toHexByteArray()) - transaction = Ethereum.Transaction.newBuilder().apply { - transfer = Ethereum.Transaction.Transfer.newBuilder().apply { - amount = ByteString.copyFrom((txData["amount"] as String).toHexByteArray()) - }.build() - }.build() - } - - val sign = AnySigner.sign(signingInput.build(), coinType, SigningOutput.parser()) - return Numeric.toHexString(sign.encoded.toByteArray()) - } - -} \ No newline at end of file diff --git a/example/lib/operations.dart b/example/lib/operations.dart index fbefda2..3c3ae73 100644 --- a/example/lib/operations.dart +++ b/example/lib/operations.dart @@ -216,14 +216,37 @@ Map operations = { "fees": 5000, "changeAddress": "D9pvhnWknRza2HTXhY5WT29D4kvYzTZQAF", }, - 'MATIC': { - "chainID": "0x01", - "nonce": "0x00", - "gasPrice": "0x07FF684650", - "gasLimit": "0x5208", - "toAddress": "0xC894F1dCE55358ef44D760d8B1fb3397F5b1c24b", - "amount": "0x0DE0B6B3A7640000", - }, + // 'MATIC': { + // "chainID": "0x89", + // "nonce": "0x00", + // "gasPrice": "0x07FF684650", + // "gasLimit": "0x5208", + // "toAddress": "0xC894F1dCE55358ef44D760d8B1fb3397F5b1c24b", + // "amount": "0x0DE0B6B3A7640000", + // }, + //............Polygon USDC test........ + // 'MATIC': { + // "cmd": "ERC20", + // "chainID": "0x89", + // "nonce": "0x00", + // "gasPrice": "0x07FF684650", + // "gasLimit": "0x5208", + // "toAddress": "0xC894F1dCE55358ef44D760d8B1fb3397F5b1c24b", + // "amount": "0x0DE0B6B3A7640000", + // "contractAddress": "0x2791Bca1f2de4661ED88A30C99A7a9449Aa84174" + // }, + + //............Polygon USDT test........ + // 'MATIC': { + // "cmd": "ERC20", + // "chainID": "0x89", + // "nonce": "0x00", + // "gasPrice": "0x07FF684650", + // "gasLimit": "0x5208", + // "toAddress": "0xC894F1dCE55358ef44D760d8B1fb3397F5b1c24b", + // "amount": "0x0DE0B6B3A7640000", + // "contractAddress": "0xc2132D05D31c914a87C6611C10748AEb04B58e8F" + // }, }; // ignore: inference_failure_on_function_return_type