Skip to content

Commit d31fb83

Browse files
committed
added derive without warns, added skip discovery if account is zero
1 parent 43f339d commit d31fb83

File tree

2 files changed

+24
-11
lines changed

2 files changed

+24
-11
lines changed

Sources/Web3Core/KeystoreManager/BIP44.swift

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,11 +25,13 @@ public enum BIP44Error: Equatable {
2525
extension HDNode: BIP44 {
2626
public func derive(path: String, warns: Bool = true) async throws -> HDNode? {
2727
if warns {
28-
var accountIndex = 0
2928
guard let account = path.accountFromPath else {
3029
return nil
3130
}
32-
31+
if account == 0 {
32+
return derive(path: path, derivePrivateKey: true)
33+
}
34+
var accountIndex = 0
3335
return nil
3436
} else {
3537
return derive(path: path, derivePrivateKey: true)

Tests/web3swiftTests/localTests/BIP44Tests.swift

Lines changed: 20 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -8,17 +8,28 @@ import Web3Core
88
@testable import web3swift
99

1010
final class BIP44Tests: LocalTestCase {
11-
12-
private let mnemonic = "fruit wave dwarf banana earth journey tattoo true farm silk olive fence"
1311

14-
func testBIP44DeriveWithoutWarningIfChildNodeHasPreviousChildsWithTransactions() async throws {
15-
let seed = try XCTUnwrap(BIP39.seedFromMmemonics(mnemonic, password: ""))
16-
let rootNode = try XCTUnwrap(HDNode(seed: seed))
12+
func testDeriveNoWarn() async throws {
13+
let rootNode = try rootNode()
1714

18-
let optChildNode = try await rootNode.derive(path: "m/44'/60'/1'/0/1", warns: false)
19-
let childNode = try XCTUnwrap(optChildNode)
15+
let childNode = try await rootNode.derive(path: "m/44'/60'/8096'/0/1", warns: false)
2016

21-
XCTAssertEqual(childNode.publicKey.toHexString(), "03fe5fad1740a0c749c3c976c9b37f337204bf3df1d73d78da282dd365a7d47ad9")
22-
XCTAssertEqual(Utilities.publicToAddress(childNode.publicKey)?.address, "0x73C13e421eF367c4F55BBC02a8e2a2b12e82f717")
17+
XCTAssertEqual(try XCTUnwrap(childNode).publicKey.toHexString(), "035785d4918449c87892371c0f9ccf6e4eda40a7fb0f773f1254c064d3bba64026")
18+
}
19+
20+
func testAccountZeroCanBeDerived() async throws {
21+
let rootNode = try rootNode()
22+
23+
let childNode = try await rootNode.derive(path: "m/44'/60'/0'/0/255", warns: true)
24+
25+
XCTAssertEqual(try XCTUnwrap(childNode).publicKey.toHexString(), "0262fba1af8f149258123265318114066decf50d16c1222a9d657b7de2296c2734")
26+
}
27+
28+
// MARK: - private
29+
30+
private func rootNode() throws -> HDNode {
31+
let mnemonic = "fruit wave dwarf banana earth journey tattoo true farm silk olive fence"
32+
let seed = try XCTUnwrap(BIP39.seedFromMmemonics(mnemonic, password: ""))
33+
return try XCTUnwrap(HDNode(seed: seed))
2334
}
2435
}

0 commit comments

Comments
 (0)