Skip to content

Commit 5c2b7dd

Browse files
committed
keep reference to Ethereum address in kotlin class
1 parent a32f2ff commit 5c2b7dd

File tree

5 files changed

+28
-9
lines changed

5 files changed

+28
-9
lines changed

gradle.properties

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@ kotlin.code.style=official
22
#kotlin.mpp.enableGranularSourceSetsMetadata=true
33
kotlin.native.enableDependencyPropagation=false
44
kotlin.mpp.enableCInteropCommonization=true
5+
kotlin.native.binary.memoryModel=experimental
6+
57

68
kotlin.native.cacheKind=none
79

src/commonMain/kotlin/Web3.kt

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package io.eqoty.web3
22

33
expect object Web3 {
44

5-
fun getAddress(privKey: String): String
5+
fun importAccount(privKey: String)
6+
fun getAddress(): String?
67

78
}

src/commonTest/kotlin/Web3Tests.kt

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,22 +4,27 @@ import kotlin.test.Test
44
import kotlin.test.assertEquals
55

66
class Web3Tests {
7+
8+
79
@Test fun `hex prefixed private key to address`() {
810

11+
912
val privKey = "0xeff8ddb99a4fa276e8e70f4c375d99964511b2ac1506ee7e48dcff43d6bc04b9"
13+
Web3.importAccount(privKey)
1014
val expectedAddress = "0x2AD093baD5b62F51A2145B87bB0F2295b1f9d4Fd".lowercase()
1115

1216

13-
assertEquals(expectedAddress, Web3.getAddress(privKey) )
17+
assertEquals(expectedAddress, Web3.getAddress())
1418
}
1519

1620
@Test fun `private key to address`() {
1721

1822
val privKey = "eff8ddb99a4fa276e8e70f4c375d99964511b2ac1506ee7e48dcff43d6bc04b9"
23+
Web3.importAccount(privKey)
1924
val expectedAddress = "0x2AD093baD5b62F51A2145B87bB0F2295b1f9d4Fd".lowercase()
2025

2126

22-
assertEquals(expectedAddress, Web3.getAddress(privKey) )
27+
assertEquals(expectedAddress, Web3.getAddress())
2328
}
2429

2530
}
Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,18 @@
1+
@file:Suppress("VARIABLE_IN_SINGLETON_WITHOUT_THREAD_LOCAL")
2+
13
package io.eqoty.web3
24

35
import swift.web3swift.Web3Swift
46

57
actual object Web3 {
6-
actual fun getAddress(privKey: String): String {
7-
return Web3Swift.getAddressWithPrivKey(privKey)
8+
var ethereumAccount: Any? = null
9+
10+
actual fun importAccount(privKey: String) {
11+
ethereumAccount = Web3Swift.importAccountWithPrivKey(privKey)
812
}
913

14+
actual fun getAddress(): String? {
15+
return Web3Swift.getAddressWithEthereumAccount(ethereumAccount!!)
16+
}
1017

1118
}

web3swift/web3swift/Web3Swift.swift

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,18 @@
11
import Foundation
2-
import CryptoKit
32
import web3
43

54

65
@objc public class Web3Swift : NSObject {
76

8-
@objc public class func getAddress(privKey: String) -> String {
7+
8+
@objc public class func importAccount(privKey: String) -> Any {
99
let keyStorage = EthereumKeyLocalStorage()
10-
let account = try! EthereumAccount.importAccount(keyStorage: keyStorage, privateKey: privKey, keystorePassword: "password")
11-
return account.address.value
10+
return try! EthereumAccount.importAccount(keyStorage: keyStorage, privateKey: privKey, keystorePassword: "password")
11+
}
12+
13+
14+
@objc public class func getAddress(ethereumAccount : Any) -> String? {
15+
return (ethereumAccount as! EthereumAccount).address.value
1216
}
1317

1418
}

0 commit comments

Comments
 (0)