Skip to content
Closed
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
4 changes: 2 additions & 2 deletions Package.swift
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ let package = Package(
platforms: [.iOS(.v13)],
products: [
.library(name: "WalletCore", targets: ["WalletCore"]),
.library(name: "SwiftProtobuf", targets: ["SwiftProtobuf"])
.library(name: "WalletCoreSwiftProtobuf", targets: ["WalletCoreSwiftProtobuf"])
],
dependencies: [],
targets: [
Expand All @@ -16,7 +16,7 @@ let package = Package(
checksum: "651894a9418fdd33ae5374367a6a64a57fa92b6e6ffb2d6723c319da97472cb4"
),
.binaryTarget(
name: "SwiftProtobuf",
name: "WalletCoreSwiftProtobuf",
url: "https://github.com/trustwallet/wallet-core/releases/download/4.2.9/SwiftProtobuf.xcframework.zip",
checksum: "946efd4b0132b92208335902e0b65e0aba2d11b9dd6f6d79cc8318e2530c9ae0"
)
Expand Down
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ Then add libraries to target's `dependencies`:

```swift
.product(name: "WalletCore", package: "WalletCore"),
.product(name: "SwiftProtobuf", package: "WalletCore"),
.product(name: "WalletCoreSwiftProtobuf", package: "WalletCore"),
```

### CocoaPods
Expand Down
5 changes: 2 additions & 3 deletions WalletCore.podspec
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ Pod::Spec.new do |s|
end

s.subspec 'Core' do |ss|
protobuf_source_dir = 'build/local/src/protobuf/protobuf-3.19.2'
protobuf_source_dir = 'build/local/src/protobuf/protobuf-3.20.3'
include_dir = 'build/local/include'
ss.source_files =
'src/**/*.{c,cc,cpp,h}',
Expand All @@ -45,6 +45,7 @@ Pod::Spec.new do |s|
"#{protobuf_source_dir}/src/google/protobuf/api.pb.cc",
"#{protobuf_source_dir}/src/google/protobuf/arena.cc",
"#{protobuf_source_dir}/src/google/protobuf/arenastring.cc",
"#{protobuf_source_dir}/src/google/protobuf/arenaz_sampler.cc",
"#{protobuf_source_dir}/src/google/protobuf/compiler/importer.cc",
"#{protobuf_source_dir}/src/google/protobuf/compiler/parser.cc",
"#{protobuf_source_dir}/src/google/protobuf/descriptor.cc",
Expand All @@ -59,8 +60,6 @@ Pod::Spec.new do |s|
"#{protobuf_source_dir}/src/google/protobuf/generated_enum_util.cc",
"#{protobuf_source_dir}/src/google/protobuf/generated_message_bases.cc",
"#{protobuf_source_dir}/src/google/protobuf/generated_message_reflection.cc",
"#{protobuf_source_dir}/src/google/protobuf/generated_message_table_driven.cc",
"#{protobuf_source_dir}/src/google/protobuf/generated_message_table_driven_lite.cc",
"#{protobuf_source_dir}/src/google/protobuf/generated_message_tctable_full.cc",
"#{protobuf_source_dir}/src/google/protobuf/generated_message_tctable_lite.cc",
"#{protobuf_source_dir}/src/google/protobuf/generated_message_util.cc",
Expand Down
22 changes: 22 additions & 0 deletions WalletCoreSwiftProtobuf.podspec
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
# Original podspec:
# https://github.com/apple/swift-protobuf/blob/main/SwiftProtobuf.podspec
Pod::Spec.new do |s|
s.name = 'WalletCoreSwiftProtobuf'
s.version = '1.29.0'
s.license = { :type => 'Apache 2.0', :file => 'LICENSE.txt' }
s.summary = 'Swift Protobuf Runtime Library'
s.homepage = 'https://github.com/apple/swift-protobuf'
s.author = 'Apple Inc.'
s.source = { :git => 'https://github.com/apple/swift-protobuf.git', :tag => s.version }

s.requires_arc = true
s.ios.deployment_target = '11.0'
s.osx.deployment_target = '10.13'

s.cocoapods_version = '>= 1.13.0'

s.source_files = 'Sources/SwiftProtobuf/**/*.swift'
s.resource_bundle = {'WalletCoreSwiftProtobuf' => ['Sources/SwiftProtobuf/PrivacyInfo.xcprivacy']}

s.swift_versions = ['5.0']
end
Original file line number Diff line number Diff line change
Expand Up @@ -36,20 +36,23 @@ class TestCoinType {
assertEquals(CoinType.TEZOS.value(), 1729)
assertEquals(CoinType.QTUM.value(), 2301)
assertEquals(CoinType.NEBULAS.value(), 2718)
assertEquals(CoinType.PACTUS.value(), 21888)
}

@Test
fun testCoinPurpose() {
assertEquals(Purpose.BIP84, CoinType.BITCOIN.purpose())
assertEquals(Purpose.BIP44, CoinType.PACTUS.purpose())
}

@Test
fun testCoinCurve() {
assertEquals(Curve.SECP256K1, CoinType.BITCOIN.curve())
assertEquals(Curve.ED25519, CoinType.PACTUS.curve())
}

@Test
fun testDerivationPath() {
fun testDerivationPathBitcoin() {
var res = CoinType.createFromValue(CoinType.BITCOIN.value()).derivationPath().toString()
assertEquals(res, "m/84'/0'/0'/0/0")
res = CoinType.createFromValue(CoinType.BITCOIN.value()).derivationPathWithDerivation(Derivation.BITCOINLEGACY).toString()
Expand All @@ -61,10 +64,31 @@ class TestCoinType {
}

@Test
fun testDeriveAddressFromPublicKeyAndDerivation() {
fun testDeriveAddressFromPublicKeyAndDerivationBitcoin() {
val publicKey = PublicKey("0279BE667EF9DCBBAC55A06295CE870B07029BFCDB2DCE28D959F2815B16F81798".toHexByteArray(), PublicKeyType.SECP256K1)

val address = CoinType.BITCOIN.deriveAddressFromPublicKeyAndDerivation(publicKey, Derivation.BITCOINSEGWIT)
assertEquals(address, "bc1qw508d6qejxtdg4y5r3zarvary0c5xw7kv8f3t4")
}

@Test
fun testDerivationPathPactus() {
var res = CoinType.createFromValue(CoinType.PACTUS.value()).derivationPath().toString()
assertEquals(res, "m/44'/21888'/3'/0'")
res = CoinType.createFromValue(CoinType.PACTUS.value()).derivationPathWithDerivation(Derivation.PACTUSMAINNET).toString()
assertEquals(res, "m/44'/21888'/3'/0'")
res = CoinType.createFromValue(CoinType.PACTUS.value()).derivationPathWithDerivation(Derivation.PACTUSTESTNET).toString()
assertEquals(res, "m/44'/21777'/3'/0'")
}

@Test
fun testDeriveAddressFromPublicKeyAndDerivationPactus() {
val publicKey = PublicKey("95794161374b22c696dabb98e93f6ca9300b22f3b904921fbf560bb72145f4fa".toHexByteArray(), PublicKeyType.ED25519)

val mainnet_address = CoinType.PACTUS.deriveAddressFromPublicKeyAndDerivation(publicKey, Derivation.PACTUSMAINNET)
assertEquals(mainnet_address, "pc1rwzvr8rstdqypr80ag3t6hqrtnss9nwymcxy3lr")

val testnet_address = CoinType.PACTUS.deriveAddressFromPublicKeyAndDerivation(publicKey, Derivation.PACTUSTESTNET)
assertEquals(testnet_address, "tpc1rwzvr8rstdqypr80ag3t6hqrtnss9nwymzqkcrg")
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -281,7 +281,7 @@ class TestBarz {
}.build()
}.build()

eip7702Authority = Ethereum.Authority.newBuilder().apply {
eip7702Authorization = Ethereum.Authorization.newBuilder().apply {
address = "0x117BC8454756456A0f83dbd130Bb94D793D3F3F7"
}.build()
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ class TestPactusAddress {
}

@Test
fun testAddress() {
fun testMainnetAddress() {
val key = PrivateKey("4e51f1f3721f644ac7a193be7f5e7b8c2abaa3467871daf4eacb5d3af080e5d6".toHexByteArray())
val pubkey = key.publicKeyEd25519
val address = AnyAddress(pubkey, CoinType.PACTUS)
Expand All @@ -26,4 +26,15 @@ class TestPactusAddress {
assertEquals(pubkey.data().toHex(), "0x95794161374b22c696dabb98e93f6ca9300b22f3b904921fbf560bb72145f4fa")
assertEquals(address.description(), expected.description())
}

@Test
fun testTestnetAddress() {
val key = PrivateKey("4e51f1f3721f644ac7a193be7f5e7b8c2abaa3467871daf4eacb5d3af080e5d6".toHexByteArray())
val pubkey = key.publicKeyEd25519
val address = AnyAddress(pubkey, CoinType.PACTUS, Derivation.PACTUSTESTNET)
val expected = AnyAddress("tpc1rwzvr8rstdqypr80ag3t6hqrtnss9nwymzqkcrg", CoinType.PACTUS)

assertEquals(pubkey.data().toHex(), "0x95794161374b22c696dabb98e93f6ca9300b22f3b904921fbf560bb72145f4fa")
assertEquals(address.description(), expected.description())
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@ class TestHDWallet {
}

@Test
fun testGetKeyForCoin() {
fun testGetKeyForCoinBitcoin() {
val coin = CoinType.BITCOIN
val wallet = HDWallet(words, password)
val key = wallet.getKeyForCoin(coin)
Expand All @@ -109,7 +109,7 @@ class TestHDWallet {
}

@Test
fun testGetKeyDerivation() {
fun testGetKeyDerivationBitcoin() {
val coin = CoinType.BITCOIN
val wallet = HDWallet(words, password)

Expand All @@ -127,7 +127,7 @@ class TestHDWallet {
}

@Test
fun testGetAddressForCoin() {
fun testGetAddressForCoinBitcoin() {
val coin = CoinType.BITCOIN
val wallet = HDWallet(words, password)

Expand All @@ -136,7 +136,7 @@ class TestHDWallet {
}

@Test
fun testGetAddressDerivation() {
fun testGetAddressDerivationBitcoin() {
val coin = CoinType.BITCOIN
val wallet = HDWallet(words, password)

Expand All @@ -153,6 +153,49 @@ class TestHDWallet {
assertEquals(address4, "bc1pgqks0cynn93ymve4x0jq3u7hne77908nlysp289hc44yc4cmy0hslyckrz")
}

@Test
fun testGetKeyForCoinPactus() {
val coin = CoinType.PACTUS
val wallet = HDWallet(words, password)
val key = wallet.getKeyForCoin(coin)

val address = coin.deriveAddress(key)
assertEquals(address, "pc1rjkzc23l7qkkenx6xwy04srwppzfk6m5t7q46ff")
}

@Test
fun testGetKeyDerivationPactus() {
val coin = CoinType.PACTUS
val wallet = HDWallet(words, password)

val key1 = wallet.getKeyDerivation(coin, Derivation.PACTUSMAINNET)
assertEquals(key1.data().toHex(), "0x153fefb8168f246f9f77c60ea10765c1c39828329e87284ddd316770717f3a5e")

val key2 = wallet.getKeyDerivation(coin, Derivation.PACTUSTESTNET)
assertEquals(key2.data().toHex(), "0x54f3c54dd6af5794bea1f86de05b8b9f164215e8deee896f604919046399e54d")
}

@Test
fun testGetAddressForCoinPactus() {
val coin = CoinType.PACTUS
val wallet = HDWallet(words, password)

val address = wallet.getAddressForCoin(coin)
assertEquals(address, "pc1rjkzc23l7qkkenx6xwy04srwppzfk6m5t7q46ff")
}

@Test
fun testGetAddressDerivationPactus() {
val coin = CoinType.PACTUS
val wallet = HDWallet(words, password)

val address1 = wallet.getAddressDerivation(coin, Derivation.PACTUSMAINNET)
assertEquals(address1, "pc1rjkzc23l7qkkenx6xwy04srwppzfk6m5t7q46ff")

val address2 = wallet.getAddressDerivation(coin, Derivation.PACTUSTESTNET)
assertEquals(address2, "tpc1rjtamyqp203j4367q4plkp4qt32d7sv34kfmj5e")
}

@Test
fun testDerive() {
val wallet = HDWallet(words, password)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -91,4 +91,36 @@ class TestKeyStore {
val privateKey = newKeyStore.decryptPrivateKey("".toByteArray())
assertNull(privateKey)
}

@Test
fun testImportKeyEncodedEthereum() {
val privateKeyHex = "9cdb5cab19aec3bd0fcd614c5f185e7a1d97634d4225730eba22497dc89a716c"
val password = "password".toByteArray()
val key = StoredKey.importPrivateKeyEncoded(privateKeyHex, "name", password, CoinType.ETHEREUM)
val json = key.exportJSON()

val keyStore = StoredKey.importJSON(json)
val storedEncoded = keyStore.decryptPrivateKeyEncoded(password)

assertTrue(keyStore.hasPrivateKeyEncoded())
assertNotNull(keyStore)
assertNotNull(storedEncoded)
assertEquals(privateKeyHex, storedEncoded)
}

@Test
fun testImportKeyEncodedSolana() {
val privateKeyBase58 = "A7psj2GW7ZMdY4E5hJq14KMeYg7HFjULSsWSrTXZLvYr"
val password = "password".toByteArray()
val key = StoredKey.importPrivateKeyEncoded(privateKeyBase58, "name", password, CoinType.SOLANA)
val json = key.exportJSON()

val keyStore = StoredKey.importJSON(json)
val storedEncoded = keyStore.decryptPrivateKeyEncoded(password)

assertTrue(keyStore.hasPrivateKeyEncoded())
assertNotNull(keyStore)
assertNotNull(storedEncoded)
assertEquals(privateKeyBase58, storedEncoded)
}
}
12 changes: 5 additions & 7 deletions cmake/Protobuf.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@
#
# Copyright © 2017 Trust Wallet.

set(protobuf_SOURCE_DIR ${CMAKE_CURRENT_LIST_DIR}/../build/local/src/protobuf/protobuf-3.19.2)
set(protobuf_source_dir ${CMAKE_CURRENT_LIST_DIR}/../build/local/src/protobuf/protobuf-3.19.2)
set(protobuf_SOURCE_DIR ${CMAKE_CURRENT_LIST_DIR}/../build/local/src/protobuf/protobuf-3.20.3)
set(protobuf_source_dir ${CMAKE_CURRENT_LIST_DIR}/../build/local/src/protobuf/protobuf-3.20.3)

# sort + uniq -u
# https://github.com/protocolbuffers/protobuf/blob/master/cmake/libprotobuf.cmake
Expand All @@ -16,6 +16,7 @@ set(protobuf_SOURCE_FILES
${protobuf_source_dir}/src/google/protobuf/api.pb.cc
${protobuf_source_dir}/src/google/protobuf/arena.cc
${protobuf_source_dir}/src/google/protobuf/arenastring.cc
${protobuf_source_dir}/src/google/protobuf/arenaz_sampler.cc
${protobuf_source_dir}/src/google/protobuf/compiler/importer.cc
${protobuf_source_dir}/src/google/protobuf/compiler/parser.cc
${protobuf_source_dir}/src/google/protobuf/descriptor.cc
Expand All @@ -30,8 +31,6 @@ set(protobuf_SOURCE_FILES
${protobuf_source_dir}/src/google/protobuf/generated_enum_util.cc
${protobuf_source_dir}/src/google/protobuf/generated_message_bases.cc
${protobuf_source_dir}/src/google/protobuf/generated_message_reflection.cc
${protobuf_source_dir}/src/google/protobuf/generated_message_table_driven.cc
${protobuf_source_dir}/src/google/protobuf/generated_message_table_driven_lite.cc
${protobuf_source_dir}/src/google/protobuf/generated_message_tctable_full.cc
${protobuf_source_dir}/src/google/protobuf/generated_message_tctable_lite.cc
${protobuf_source_dir}/src/google/protobuf/generated_message_util.cc
Expand All @@ -51,6 +50,7 @@ set(protobuf_SOURCE_FILES
${protobuf_source_dir}/src/google/protobuf/message.cc
${protobuf_source_dir}/src/google/protobuf/message_lite.cc
${protobuf_source_dir}/src/google/protobuf/parse_context.cc
${protobuf_source_dir}/src/google/protobuf/reflection_internal.h
${protobuf_source_dir}/src/google/protobuf/reflection_ops.cc
${protobuf_source_dir}/src/google/protobuf/repeated_field.cc
${protobuf_source_dir}/src/google/protobuf/repeated_ptr_field.cc
Expand Down Expand Up @@ -104,6 +104,7 @@ set(protobuf_HEADER_FILES
${protobuf_source_dir}/src/google/protobuf/arena.h
${protobuf_source_dir}/src/google/protobuf/arena_impl.h
${protobuf_source_dir}/src/google/protobuf/arenastring.h
${protobuf_source_dir}/src/google/protobuf/arenaz_sampler.h
${protobuf_source_dir}/src/google/protobuf/compiler/importer.h
${protobuf_source_dir}/src/google/protobuf/compiler/parser.h
${protobuf_source_dir}/src/google/protobuf/descriptor.h
Expand All @@ -121,11 +122,8 @@ set(protobuf_HEADER_FILES
${protobuf_source_dir}/src/google/protobuf/generated_enum_util.h
${protobuf_source_dir}/src/google/protobuf/generated_message_bases.h
${protobuf_source_dir}/src/google/protobuf/generated_message_reflection.h
${protobuf_source_dir}/src/google/protobuf/generated_message_table_driven.h
${protobuf_source_dir}/src/google/protobuf/generated_message_table_driven_lite.h
${protobuf_source_dir}/src/google/protobuf/generated_message_tctable_decl.h
${protobuf_source_dir}/src/google/protobuf/generated_message_tctable_impl.h
${protobuf_source_dir}/src/google/protobuf/generated_message_tctable_impl.inc
${protobuf_source_dir}/src/google/protobuf/generated_message_util.h
${protobuf_source_dir}/src/google/protobuf/has_bits.h
${protobuf_source_dir}/src/google/protobuf/implicit_weak_message.h
Expand Down
8 changes: 8 additions & 0 deletions codegen-v2/manifest/TWDerivation.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -19,3 +19,11 @@ enums:
value: 5
- name: solanaSolana
value: 6
- name: stratisSegwit
value: 7
- name: bitcoinTaproot
value: 8
- name: pactusMainnet
value: 9
- name: pactusTestnet
value: 10
2 changes: 1 addition & 1 deletion codegen-v2/src/codegen/swift/functions.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ pub(super) fn process_methods(

let mut ops = vec![];

// Initalize the 'self' type, which is then passed on to the underlying
// Initialize the 'self' type, which is then passed on to the underlying
// C FFI function, assuming the function is not static.
//
// E.g:
Expand Down
2 changes: 1 addition & 1 deletion codegen-v2/src/codegen/swift/properties.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ pub(super) fn process_properties(

let mut ops = vec![];

// Initalize the 'self' type, which is then passed on to the underlying
// Initialize the 'self' type, which is then passed on to the underlying
// C FFI function.
ops.push(match object {
// E.g. `let obj = self.rawValue`
Expand Down
2 changes: 2 additions & 0 deletions include/TrustWalletCore/TWDerivation.h
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@ enum TWDerivation {
TWDerivationSolanaSolana = 6,
TWDerivationStratisSegwit = 7,
TWDerivationBitcoinTaproot = 8,
TWDerivationPactusMainnet = 9,
TWDerivationPactusTestnet = 10,
// end_of_derivation_enum - USED TO GENERATE CODE
};

Expand Down
Loading