Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

updated stellar payment #55

Merged
merged 3 commits into from
Jun 14, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -6,3 +6,8 @@ build/
.idea/
pubspec.lock
org.eclipse.buildship.core.prefs
android/.classpath
android/.project
example/android/.project
example/android/.classpath

6 changes: 0 additions & 6 deletions android/.classpath

This file was deleted.

34 changes: 0 additions & 34 deletions android/.project

This file was deleted.

27 changes: 22 additions & 5 deletions android/src/main/kotlin/africa/ejara/trustdart/coins/XLM.kt
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,19 @@ import wallet.core.jni.StellarPassphrase

class XLM : Coin("XLM", CoinType.STELLAR) {

enum class NetworkType(val passphrase: String) {
MAINNET("Public Global Stellar Network ; September 2015"),
TESTNET("Test SDF Network ; September 2015");
companion object {
fun fromString(network: String?): NetworkType {
return when (network?.lowercase()) {
"testnet" -> TESTNET
else -> MAINNET // Default to mainnet
}
}
}
}

override fun getPublicKey(path: String, mnemonic: String, passphrase: String): String? {
val wallet = HDWallet(mnemonic, passphrase)
return wallet.getKey(coinType, path).publicKeyEd25519.data().base64String()
Expand All @@ -29,6 +42,11 @@ class XLM : Coin("XLM", CoinType.STELLAR) {
pass_phrase: String
): String? {
val cmd = txData["cmd"] as String
val networkType: NetworkType = {
val network = txData["network"] as? String
NetworkType.fromString(network)
}()

val secretKey = HDWallet(mnemonic, pass_phrase).getKey(coinType, path)
val txHash: String?
when (cmd) {
Expand All @@ -49,7 +67,7 @@ class XLM : Coin("XLM", CoinType.STELLAR) {
account = txData["ownerAddress"] as String
fee = txData["fee"] as Int
sequence = txData["sequence"]!!.toLong()
passphrase = StellarPassphrase.STELLAR.toString()
passphrase = networkType.passphrase
opChangeTrust = operation.build()
privateKey = ByteString.copyFrom(secretKey.data())
}
Expand All @@ -58,10 +76,9 @@ class XLM : Coin("XLM", CoinType.STELLAR) {
}
"Payment" -> {
val stellarAsset = Stellar.Asset.newBuilder()
if (txData["asset"] != null) {

if (txData["asset"] != null && txData["issuer"] != null) {
stellarAsset.apply {
issuer = txData["ownerAddress"] as String
issuer = txData["issuer"] as String
alphanum4 = txData["asset"] as String
}
}
Expand All @@ -78,7 +95,7 @@ class XLM : Coin("XLM", CoinType.STELLAR) {
account = txData["ownerAddress"] as String
fee = txData["fee"] as Int
sequence = txData["sequence"]!!.toLong()
passphrase = StellarPassphrase.STELLAR.toString()
passphrase = networkType.passphrase
opPayment = operation.build()
privateKey = ByteString.copyFrom(secretKey.data())
if (txData["memo"] != null) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,9 +29,10 @@ fun ByteArray.base64String(): String {
}

fun Any.toLong(): Long {
return if(this is Int){
this.toLong()
}else{
this as Long
return when (this) {
is Int -> this.toLong()
is Long -> this
is String -> this.toLongOrNull() ?: throw NumberFormatException("Cannot convert $this to Long")
else -> throw IllegalArgumentException("Unsupported type")
}
}
28 changes: 0 additions & 28 deletions example/android/.project

This file was deleted.

5 changes: 5 additions & 0 deletions example/lib/operations.dart
Original file line number Diff line number Diff line change
Expand Up @@ -172,9 +172,14 @@ Map<String, dynamic> operations = {
"cmd": 'Payment',
"ownerAddress": "GCPP3J7CE23VF3EONOIDXDL6QODYTI3YWJ7PNMHTO77WSEXGK2TT4QPV",
"toAddress": "GBPT3GVKY727GYXTO6QAEVET3AW3EUVZZCZOCCO5B5PJXRVS3S4GD2AY",
"issuer": "GBBD47IF6LWK7P7MDEVSCWR7DPUWV3NY3DTQEVFL4NAT4AQH3ZLLFLA5",
"network": "testnet",
"validBefore": 1717806538278,
"amount": 2000000,
"fee": 10000,
"sequence": 183629192141733925,
"asset": "USDC",
"memo": "3476840067250060816"
},
// 'XLM': {
// "cmd": "ChangeTrust",
Expand Down
41 changes: 31 additions & 10 deletions ios/Classes/coins/XLM.swift
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,20 @@
import WalletCore

class XLM: Coin {
enum NetworkType: String {
case mainnet
case testnet

var passphrase: String {
switch self {
case .mainnet:
return "Public Global Stellar Network ; September 2015"
case .testnet:
return "Test SDF Network ; September 2015"
}
}
}

init() {
super.init(name: "XLM", coinType: .stellar)
}
Expand All @@ -23,6 +37,14 @@ class XLM: Coin {
let cmd = txData["cmd"] as! String
var txHash: String?

let networkType: NetworkType = {
if let network = txData["network"], let type = NetworkType(rawValue: network as! String) {
return type
} else {
return .mainnet // Default to mainnet if network is not provided or invalid
}
}()

switch(cmd){
case "ChangeTrust":
let asset = StellarAsset.with {
Expand All @@ -39,26 +61,25 @@ class XLM: Coin {
$0.account = txData["ownerAddress"] as! String
$0.fee = txData["fee"] as! Int32
$0.sequence = txData["sequence"] as! Int64
$0.passphrase = StellarPassphrase.stellar.description
$0.passphrase = networkType.passphrase
$0.opChangeTrust = operation
$0.privateKey = privateKey!.data
if (txData["memo"] != nil) {
$0.memoID = StellarMemoId.with {
$0.id = txData["memo"] as! Int64
$0.id = Int64(txData["memo"] as! String)!
}
}
}

let output: StellarSigningOutput = AnySigner.sign(input: signingInput, coin: self.coinType)
txHash = output.signature
case "Payment":

let asset = StellarAsset.with {
$0.issuer = txData["ownerAddress"] as! String
if (txData["asset"] != nil) {
$0.alphanum4 = txData["asset"] as! String
}
var asset = StellarAsset()
if let assetString = txData["asset"] as? String, let issuer = txData["issuer"] as? String {
asset.alphanum4 = assetString
asset.issuer = issuer
}

let operation = StellarOperationPayment.with {
$0.destination = txData["toAddress"] as! String
$0.amount = txData["amount"] as! Int64
Expand All @@ -71,12 +92,12 @@ class XLM: Coin {
$0.account = txData["ownerAddress"] as! String
$0.fee = txData["fee"] as! Int32
$0.sequence = txData["sequence"] as! Int64
$0.passphrase = StellarPassphrase.stellar.description
$0.passphrase = networkType.passphrase
$0.opPayment = operation
$0.privateKey = privateKey!.data
if (txData["memo"] != nil) {
$0.memoID = StellarMemoId.with {
$0.id = txData["memo"] as! Int64
$0.id = Int64(txData["memo"] as! String)!
}
}
}
Expand Down