Skip to content

Commit 7cf34ec

Browse files
authored
Merge pull request #74 from BANKEX/develop
Allow BIP32 keystore init from seed directly Add convenience BIP32 keystore and KeystoreV3 serialization methods Test custom path derivation after saving Add new BIP39 languages
2 parents 72b40c0 + 8e522af commit 7cf34ec

File tree

11 files changed

+117
-23
lines changed

11 files changed

+117
-23
lines changed

Example/web3swiftExample/Podfile.lock

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ EXTERNAL SOURCES:
3838

3939
CHECKOUT OPTIONS:
4040
web3swift:
41-
:commit: 49c569f76f135efa31f545a63a8599e5c9f6ec13
41+
:commit: 72b40c0679a38b3b186669e22af5e1d79b398ac3
4242
:git: https://github.com/bankex/web3swift.git
4343

4444
SPEC CHECKSUMS:

Example/web3swiftExample/Pods/Manifest.lock

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Example/web3swiftExample/Pods/web3swift/web3swift/KeystoreManager/Classes/BIP32Keystore.swift

Lines changed: 0 additions & 8 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Example/web3swiftExample/Pods/web3swift/web3swift/KeystoreManager/Classes/KeystoreManager.swift

Lines changed: 1 addition & 5 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Example/web3swiftExample/Pods/web3swift/web3swift/Web3/Classes/Web3+Eth.swift

Lines changed: 17 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

web3swift.podspec

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
Pod::Spec.new do |s|
22
s.name = "web3swift"
3-
s.version = "0.5.5"
3+
s.version = "0.5.6"
44
s.summary = "Web3 implementation in vanilla Swift for iOS ans macOS"
55

66
s.description = <<-DESC

web3swift/KeystoreManager/Classes/BIP32Keystore.swift

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -69,11 +69,14 @@ public class BIP32Keystore: AbstractKeystore {
6969
rootPrefix = keystoreParams!.rootPath!
7070
}
7171

72-
public init? (mnemonics: String, password: String = "BANKEXFOUNDATION", mnemonicsPassword: String = "", language: BIP39Language = BIP39Language.english, prefixPath: String = HDNode.defaultPathPrefix) throws {
72+
public convenience init? (mnemonics: String, password: String = "BANKEXFOUNDATION", mnemonicsPassword: String = "", language: BIP39Language = BIP39Language.english, prefixPath: String = HDNode.defaultPathPrefix) throws {
7373
guard var seed = BIP39.seedFromMmemonics(mnemonics, password: mnemonicsPassword, language: language) else {throw AbstractKeystoreError.noEntropyError}
74-
guard let prefixNode = HDNode(seed: seed)?.derive(path: prefixPath, derivePrivateKey: true) else {return nil}
7574
defer{ Data.zero(&seed) }
76-
self.mnemonics = mnemonics
75+
try self.init(seed: seed, password: password, prefixPath: prefixPath)
76+
}
77+
78+
public init? (seed: Data, password: String = "BANKEXFOUNDATION", prefixPath: String = HDNode.defaultPathPrefix) throws {
79+
guard let prefixNode = HDNode(seed: seed)?.derive(path: prefixPath, derivePrivateKey: true) else {return nil}
7780
self.rootPrefix = prefixPath
7881
try createNewAccount(parentNode: prefixNode, password: password)
7982
}
@@ -243,4 +246,10 @@ public class BIP32Keystore: AbstractKeystore {
243246
guard decryptedPK?.count == 82 else {return nil}
244247
return Data(bytes:decryptedPK!)
245248
}
249+
250+
public func serialize() throws -> Data? {
251+
guard let params = self.keystoreParams else {return nil}
252+
let data = try JSONEncoder().encode(params)
253+
return data
254+
}
246255
}

web3swift/KeystoreManager/Classes/BIP39+WordLists.swift

Lines changed: 21 additions & 0 deletions
Large diffs are not rendered by default.

web3swift/KeystoreManager/Classes/BIP39.swift

Lines changed: 31 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,10 +11,39 @@ import CryptoSwift
1111

1212
public enum BIP39Language {
1313
case english
14+
case chinese_simplified
15+
case chinese_traditional
16+
case japanese
17+
case korean
18+
case french
19+
case italian
20+
case spanish
1421
var words: [String] {
1522
switch self {
1623
case .english:
1724
return englishWords
25+
case .chinese_simplified:
26+
return simplifiedchineseWords
27+
case .chinese_traditional:
28+
return traditionalchineseWords
29+
case .japanese:
30+
return japaneseWords
31+
case .korean:
32+
return koreanWords
33+
case.french:
34+
return frenchWords
35+
case .italian:
36+
return italianWords
37+
case .spanish:
38+
return spanishWords
39+
}
40+
}
41+
var separator: String {
42+
switch self {
43+
case .japanese:
44+
return "\u{3000}"
45+
default:
46+
return " "
1847
}
1948
}
2049
}
@@ -36,7 +65,8 @@ public class BIP39 {
3665
let word = language.words[index]
3766
wordList.append(word)
3867
}
39-
return wordList.joined(separator: " ")
68+
let separator = language.separator
69+
return wordList.joined(separator: separator)
4070
}
4171

4272
static public func generateMnemonics(bitsOfEntropy: Int, language: BIP39Language = BIP39Language.english) throws -> String? {

web3swift/KeystoreManager/Classes/EthereumKeystoreV3.swift

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -168,4 +168,10 @@ public class EthereumKeystoreV3: AbstractKeystore {
168168
guard decryptedPK != nil else {return nil}
169169
return Data(bytes:decryptedPK!)
170170
}
171+
172+
public func serialize() throws -> Data? {
173+
guard let params = self.keystoreParams else {return nil}
174+
let data = try JSONEncoder().encode(params)
175+
return data
176+
}
171177
}

0 commit comments

Comments
 (0)