Skip to content

Commit

Permalink
Merge pull request #47 from EjaraApp/cardano_integration
Browse files Browse the repository at this point in the history
Integrated cardano on android and ios
  • Loading branch information
jayluxferro authored Jun 15, 2023
2 parents d7cd79d + e00a862 commit a8413e0
Show file tree
Hide file tree
Showing 13 changed files with 200 additions and 27 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -5,3 +5,4 @@
build/
.idea/
pubspec.lock
org.eclipse.buildship.core.prefs
13 changes: 0 additions & 13 deletions android/.settings/org.eclipse.buildship.core.prefs

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ import XLM
import BNB
import BSC
import DOGE
import MATIC
import ADA

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

Expand Down
52 changes: 52 additions & 0 deletions android/src/main/kotlin/africa/ejara/trustdart/coins/ADA.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
import africa.ejara.trustdart.Coin
import africa.ejara.trustdart.Numeric
import africa.ejara.trustdart.utils.toLong
import com.google.protobuf.ByteString
import io.flutter.Log
import wallet.core.jni.CoinType
import wallet.core.jni.HDWallet
import wallet.core.jni.proto.Cardano
import wallet.core.java.AnySigner



class ADA : Coin("ADA", CoinType.CARDANO){

override fun signTransaction(
path: String,
txData: Map<String, Any>,
mnemonic: String,
passphrase: String
): String? {
val wallet = HDWallet(mnemonic, passphrase)
val privateKey = wallet.getKey(coinType, path)
val listOfAllUtxos = mutableListOf<Cardano.TxInput>();
val utxos: List<Map<String, Any>> = txData["utxos"] as List<Map<String, Any>>
val message = Cardano.Transfer.newBuilder()
.setToAddress(txData["receiverAddress"] as String)
.setChangeAddress(txData["senderAddress" ] as String)
.setAmount(txData["amount"]!!.toLong())
.build()
val input = Cardano.SigningInput.newBuilder()
.setTransferMessage(message)
.setTtl(53333333)

input.addPrivateKey(ByteString.copyFrom(privateKey.data()))
for (utx in utxos) {
val outpoint1 = Cardano.OutPoint.newBuilder()
.setTxHash(ByteString.copyFrom(Numeric.hexStringToByteArray(utx["txid"] as String)))
.setOutputIndex(utx["index"]!!.toLong())
.build()
val utxo1 = Cardano.TxInput.newBuilder()
.setOutPoint(outpoint1)
.setAddress(utx["senderAddress"] as String)
.setAmount(utx["amount"]!!.toLong())
.build()
listOfAllUtxos.add(utxo1)
}
input.addAllUtxos(listOfAllUtxos)

val output = AnySigner.sign(input.build(), coinType, Cardano.SigningOutput.parser())
return Numeric.toHexString(output.encoded.toByteArray())
}
}
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 wallet = HDWallet(mnemonic, passphrase)
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())
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())
}

}
13 changes: 0 additions & 13 deletions example/android/.settings/org.eclipse.buildship.core.prefs

This file was deleted.

4 changes: 3 additions & 1 deletion example/ios/Runner.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
archiveVersion = 1;
classes = {
};
objectVersion = 51;
objectVersion = 54;
objects = {

/* Begin PBXBuildFile section */
Expand Down Expand Up @@ -238,6 +238,7 @@
};
3B06AD1E1E4923F5004D2608 /* Thin Binary */ = {
isa = PBXShellScriptBuildPhase;
alwaysOutOfDate = 1;
buildActionMask = 2147483647;
files = (
);
Expand All @@ -252,6 +253,7 @@
};
9740EEB61CF901F6004384FC /* Run Script */ = {
isa = PBXShellScriptBuildPhase;
alwaysOutOfDate = 1;
buildActionMask = 2147483647;
files = (
);
Expand Down
2 changes: 2 additions & 0 deletions example/ios/Runner/Info.plist
Original file line number Diff line number Diff line change
Expand Up @@ -43,5 +43,7 @@
<false/>
<key>CADisableMinimumFrameDurationOnPhone</key>
<true/>
<key>UIApplicationSupportsIndirectInputEvents</key>
<true/>
</dict>
</plist>
2 changes: 2 additions & 0 deletions example/lib/coins.dart
Original file line number Diff line number Diff line change
Expand Up @@ -18,4 +18,6 @@ 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"),
Coin(code: 'ADA', path: "m/1852'/1815'/0'/0/0"),
];
25 changes: 25 additions & 0 deletions example/lib/operations.dart
Original file line number Diff line number Diff line change
Expand Up @@ -216,6 +216,31 @@ Map<String, dynamic> operations = {
"fees": 5000,
"changeAddress": "D9pvhnWknRza2HTXhY5WT29D4kvYzTZQAF",
},
'MATIC': {
"chainId": "0x89",
"nonce": "0x01",
"gasPrice": "0x07FF684650",
"gasLimit": "0x5208",
"toAddress": "0xC894F1dCE55358ef44D760d8B1fb3397F5b1c24b",
"amount": "0x3B9ACA00",
},
'ADA': {
"senderAddress":
"addr1q9evp7aqelh4epkacgyeqweqgkvqsl8gdp54mxew5kdvuyhqhuqa6ngy0jrdcnknurcvjgtv4jd84pd7xllgmdz0wtrqgfz5l4",
"receiverAddress":
"addr1qyk022rpw85g7c0f0wuq6zpkakgjwsftmpd99wqjj4xcsjc74pfgs7t76yuehca7hn4pcl37lsl06ccey0epe5sp4lwslxsyrw",
"amount": 40000,
"utxos": [
{
"senderAddress":
"addr1q9evp7aqelh4epkacgyeqweqgkvqsl8gdp54mxew5kdvuyhqhuqa6ngy0jrdcnknurcvjgtv4jd84pd7xllgmdz0wtrqgfz5l4",
"txid":
"76608917328b3768b3985d057e613c7e8f14cb1f27b132a750a363ee64363a57",
"index": 0,
"amount": 16900000,
},
],
}
};

// ignore: inference_failure_on_function_return_type
Expand Down
6 changes: 6 additions & 0 deletions ios/Classes/WalletHandler.swift
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@ class WalletHandler {
"BNB" : BNB(),
"BSC" : BSC(),
"DOGE" : DOGE(),
"MATIC" : MATIC(),
"ADA" : ADA()
]

func getCoin(_ coin: String) -> Coin {
Expand All @@ -46,3 +48,7 @@ class WalletHandler {
}






38 changes: 38 additions & 0 deletions ios/Classes/coins/ADA.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
/*
ADA
*/

import WalletCore

class ADA: Coin {
init() {
super.init(name: "ADA", coinType: .cardano)
}

override func signTransaction(path: String, txData: [String : Any], mnemonic: String, passphrase: String) -> String? {
let privateKey = HDWallet(mnemonic: mnemonic, passphrase: passphrase)?.getKey(coin: coinType, derivationPath: path)
let utxos: [[String: Any]] = txData["utxos"] as! [[String: Any]]
var listOfUtxos: [CardanoTxInput] = []

for utx in utxos {
listOfUtxos.append(CardanoTxInput.with {
$0.outPoint.txHash = Data(hexString: (utx["txid"] as! String))!
$0.outPoint.outputIndex = utx["index"] as! UInt64
$0.address = utx["senderAddress"] as! String
$0.amount = utx["amount"] as! UInt64
})
}

var input = CardanoSigningInput.with {
$0.transferMessage.toAddress = txData["receiverAddress"] as! String
$0.transferMessage.changeAddress = txData["senderAddress"] as! String
$0.transferMessage.amount = txData["amount"] as! UInt64
$0.ttl = 53333333
$0.privateKey = [privateKey!.data]
$0.utxos = listOfUtxos
}

let output: CardanoSigningOutput = AnySigner.sign(input: input, coin: .cardano)
return output.encoded.hexString
}
}
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 a8413e0

Please sign in to comment.