Skip to content

Commit

Permalink
polygon integration
Browse files Browse the repository at this point in the history
  • Loading branch information
Rakeally committed Jun 13, 2023
1 parent d7cd79d commit e75bd33
Show file tree
Hide file tree
Showing 6 changed files with 81 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import XLM
import BNB
import BSC
import DOGE
import MATIC

import africa.ejara.trustdart.utils.WalletError
import africa.ejara.trustdart.utils.WalletValidateResponse
Expand All @@ -29,6 +30,7 @@ class WalletHandler {
"BNB" to BNB(),
"BSC" to BSC(),
"DOGE" to DOGE(),
"MATIC" to MATIC(),
)
}

Expand Down
39 changes: 39 additions & 0 deletions android/src/main/kotlin/africa/ejara/trustdart/coins/MATIC.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
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<String, Any>,
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())
}

}
1 change: 1 addition & 0 deletions example/lib/coins.dart
Original file line number Diff line number Diff line change
Expand Up @@ -18,4 +18,5 @@ List<Coin> coinList = [
Coin(code: 'BNB', path: "m/44'/714'/0'/0/0"),
Coin(code: 'BSC', path: "m/44'/60'/0'/0/0"),
Coin(code: 'DOGE', path: "m/44'/3'/0'/0/0"),
Coin(code: 'MATIC', path: "m/44'/60'/0'/0/0")
];
11 changes: 10 additions & 1 deletion example/lib/operations.dart
Original file line number Diff line number Diff line change
Expand Up @@ -216,6 +216,14 @@ Map<String, dynamic> operations = {
"fees": 5000,
"changeAddress": "D9pvhnWknRza2HTXhY5WT29D4kvYzTZQAF",
},
'MATIC': {
"chainID": "0x01",
"nonce": "0x00",
"gasPrice": "0x07FF684650",
"gasLimit": "0x5208",
"toAddress": "0xC894F1dCE55358ef44D760d8B1fb3397F5b1c24b",
"amount": "0x0DE0B6B3A7640000",
},
};

// ignore: inference_failure_on_function_return_type
Expand All @@ -227,7 +235,8 @@ runOperations() async {
print('Here is our mnemonic: \n$mnemonic');

String dondo =
"imitate embody law mammal exotic transfer roof hope price swift ordinary uncle";
"able student evoke travel find shift gasp beauty venture dove valid lounge";
// "imitate embody law mammal exotic transfer roof hope price swift ordinary uncle";
// dondo = "a d f d s e w q t y u l";
bool wallet = await Trustdart.checkMnemonic(dondo);
print(wallet);
Expand Down
1 change: 1 addition & 0 deletions ios/Classes/WalletHandler.swift
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ class WalletHandler {
"BNB" : BNB(),
"BSC" : BSC(),
"DOGE" : DOGE(),
"MATIC" : MATIC()
]

func getCoin(_ coin: String) -> Coin {
Expand Down
28 changes: 28 additions & 0 deletions ios/Classes/coins/MATIC.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
import WalletCore

class MATIC: Coin {
init() {
super.init(name: "MATIC", coinType: .polygon)
}

override func signTransaction(path: String, txData: [String : Any], mnemonic: String, passphrase: String) -> String? {
let privateKey = HDWallet(mnemonic: mnemonic, passphrase: passphrase)?.getKey(coin: self.coinType, derivationPath: path)

let input = EthereumSigningInput.with {
$0.chainID = Data(hexString: txData["chainID"] as! String)!
$0.nonce = Data(hexString: (txData["nonce"] as! String))!
$0.gasPrice = Data(hexString: (txData["gasPrice"] as! String))!
$0.gasLimit = Data(hexString: (txData["gasLimit"] as! String))!
$0.toAddress = txData["toAddress"] as! String
$0.privateKey = privateKey!.data
$0.transaction = EthereumTransaction.with {
$0.transfer = EthereumTransaction.Transfer.with {
$0.amount = Data(hexString: (txData["amount"] as! String))!
}
}
}

let sign: EthereumSigningOutput = AnySigner.sign(input: input, coin: coinType)
return sign.encoded.hexString
}
}

0 comments on commit e75bd33

Please sign in to comment.