Skip to content

Commit

Permalink
verify address, derive address modified to return different types of …
Browse files Browse the repository at this point in the history
…addresses
  • Loading branch information
Baah Kusi committed Jun 23, 2021
1 parent 49f3d85 commit d33a87b
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 18 deletions.
18 changes: 9 additions & 9 deletions .idea/workspace.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

39 changes: 31 additions & 8 deletions android/src/main/kotlin/africa/ejara/trustdart/TrustdartPlugin.kt
Original file line number Diff line number Diff line change
Expand Up @@ -45,22 +45,30 @@ class TrustdartPlugin: FlutterPlugin, MethodCallHandler {
}
"importWalletFromMnemonic" -> {
val mnmemonic: String = call.arguments()
println(mnmemonic)
wallet = HDWallet(mnmemonic, "")
result.success(true)
}
"generateAddressForCoin" -> {
if (!::wallet.isInitialized) return result.error("empty_wallet", "wallet not initialized", null)
val path: String? = call.argument("path")
val coin: String? = call.argument("coin")
print("$coin $path")
if (path != null && coin != null) {
val address: String? = generateAddressForCoin(path, coin)
val address: Map<String, String?>? = generateAddressForCoin(path, coin)
if (address == null) result.error("address_null", "failed to generate address", null) else result.success(address)
} else {
result.error("arguments_null", "$path and $coin cannot be null", null)
}
}
"validateAddressForCoin" -> {
val address: String? = call.argument("address")
val coin: String? = call.argument("coin")
if (path != null && coin != null) {
val isValid: bool = validateAddressForCoin(coin, address)
result.success(bool)
} else {
result.error("arguments_null", "$path and $coin cannot be null", null)
}
}
else -> result.notImplemented()
}
}
Expand All @@ -69,20 +77,35 @@ class TrustdartPlugin: FlutterPlugin, MethodCallHandler {
channel.setMethodCallHandler(null)
}

private fun generateAddressForCoin(path: String, coin: String): String? {
private fun generateAddressForCoin(path: String, coin: String): Map<String, String?>? {
val privateKey = wallet.getKey(path)
return when(coin) {
"BTC" -> {
val publicKey = privateKey.getPublicKeySecp256k1(true)
val address = BitcoinAddress(publicKey, CoinType.BITCOIN.p2pkhPrefix())
// CoinType.BITCOIN.deriveAddress(key)
address.description()
mapOf("legacy" to CoinType.BITCOIN.deriveAddress(privateKey), "segwit" to address.description())
}
"ETH" -> {
mapOf("legacy" to CoinType.ETHEREUM.deriveAddress(privateKey))
}
"XTZ" -> {
CoinType.TEZOS.
mapOf("legacy" to CoinType.TEZOS.deriveAddress(privateKey))
}
else -> null
}
}

private fun validateAddressForCoin(coin: String, address: String): bool {
return when(coin) {
"BTC" -> {
CoinType.BITCOIN.validateAddress(address)
}
"ETH" -> {
CoinType.ETHEREUM.deriveAddress(privateKey)
CoinType.ETHEREUM.validateAddress(address)
}
"XTZ" -> {
CoinType.TEZOS.deriveAddress(privateKey)
CoinType.TEZOS.validateAddress(address)
}
else -> null
}
Expand Down
2 changes: 1 addition & 1 deletion lib/trustdart.dart
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ class Trustdart {
return importStatus;
}

static Future<String> generateAddressForCoin(String path, String coin) async {
static Future<Map> generateAddressForCoin(String path, String coin) async {
final address = await _channel.invokeMethod('generateAddressForCoin', <String, String> {
'coin': coin,
'path': path,
Expand Down

0 comments on commit d33a87b

Please sign in to comment.