-
Notifications
You must be signed in to change notification settings - Fork 8
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #47 from EjaraApp/cardano_integration
Integrated cardano on android and ios
- Loading branch information
Showing
13 changed files
with
200 additions
and
27 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -5,3 +5,4 @@ | |
build/ | ||
.idea/ | ||
pubspec.lock | ||
org.eclipse.buildship.core.prefs |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
52 changes: 52 additions & 0 deletions
52
android/src/main/kotlin/africa/ejara/trustdart/coins/ADA.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
39
android/src/main/kotlin/africa/ejara/trustdart/coins/MATIC.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
13
example/android/.settings/org.eclipse.buildship.core.prefs
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} | ||
} |