@@ -19,6 +19,7 @@ extension UInt32 {
19
19
let byteArray = Array ( bytePtr)
20
20
return Data ( byteArray)
21
21
}
22
+ static var maxIterationIndex = UInt32 ( 1 ) << 31
22
23
}
23
24
24
25
public class HDNode {
@@ -36,11 +37,11 @@ public class HDNode {
36
37
public var parentFingerprint : Data = Data ( repeating: 0 , count: 4 )
37
38
public var childNumber : UInt32 = UInt32 ( 0 )
38
39
public var isHardened : Bool {
39
- childNumber >= ( UInt32 ( 1 ) << 31 )
40
+ childNumber >= UInt32 . maxIterationIndex
40
41
}
41
42
public var index : UInt32 {
42
43
if self . isHardened {
43
- return childNumber - ( UInt32 ( 1 ) << 31 )
44
+ return childNumber - UInt32. maxIterationIndex
44
45
} else {
45
46
return childNumber
46
47
}
@@ -91,7 +92,7 @@ public class HDNode {
91
92
guard seed. count >= 16 else { return nil }
92
93
93
94
guard let hmacKey = " Bitcoin seed " . data ( using: . ascii) else { return nil }
94
- let hmac : Authenticator = HMAC ( key: hmacKey. bytes, variant: HMAC . Variant. sha2 ( . sha512) )
95
+ let hmac : Authenticator = HMAC ( key: hmacKey. bytes, variant: HMAC . Variant. sha2 ( . sha512) )
95
96
96
97
guard let entropy = try ? hmac. authenticate ( seed. bytes) , entropy. count == 64 else { return nil }
97
98
let I_L = entropy [ 0 ..< 32 ]
@@ -112,7 +113,7 @@ public class HDNode {
112
113
public static var defaultPathPrefix : String = " m/44'/60'/0' "
113
114
public static var defaultPathMetamask : String = " m/44'/60'/0'/0/0 "
114
115
public static var defaultPathMetamaskPrefix : String = " m/44'/60'/0'/0 "
115
- public static var hardenedIndexPrefix : UInt32 = ( UInt32 ( 1 ) << 31 )
116
+ public static var hardenedIndexPrefix : UInt32 = UInt32 . maxIterationIndex
116
117
}
117
118
118
119
extension HDNode {
@@ -126,14 +127,14 @@ extension HDNode {
126
127
127
128
public func deriveWithoutPrivateKey( index: UInt32 , hardened: Bool = false ) -> HDNode ? {
128
129
var entropy : [ UInt8 ] // derive public key when is itself public key
129
- if index >= ( UInt32 ( 1 ) << 31 ) || hardened {
130
+ if index >= UInt32 . maxIterationIndex || hardened {
130
131
return nil // no derivation of hardened public key from extended public key
131
132
} else {
132
133
let hmac : Authenticator = HMAC ( key: self . chaincode. bytes, variant: . sha2( . sha512) )
133
134
var inputForHMAC = Data ( )
134
135
inputForHMAC. append ( self . publicKey)
135
136
inputForHMAC. append ( index. serialize32 ( ) )
136
- guard let ent = try ? hmac. authenticate ( inputForHMAC. bytes) else { return nil }
137
+ guard let ent = try ? hmac. authenticate ( inputForHMAC. bytes) else { return nil }
137
138
guard ent. count == 64 else { return nil }
138
139
entropy = ent
139
140
}
@@ -148,7 +149,7 @@ extension HDNode {
148
149
return nil
149
150
}
150
151
guard let tempKey = bn. serialize ( ) . setLengthLeft ( 32 ) else { return nil }
151
- guard SECP256K1 . verifyPrivateKey ( privateKey: tempKey) else { return nil }
152
+ guard SECP256K1 . verifyPrivateKey ( privateKey: tempKey) else { return nil }
152
153
guard let pubKeyCandidate = SECP256K1 . privateToPublic ( privateKey: tempKey, compressed: true ) else { return nil }
153
154
guard pubKeyCandidate. bytes. first == 0x02 || pubKeyCandidate. bytes. first == 0x03 else { return nil }
154
155
guard let newPublicKey = SECP256K1 . combineSerializedPublicKeys ( keys: [ self . publicKey, pubKeyCandidate] , outputCompressed: true ) else { return nil }
@@ -180,17 +181,17 @@ extension HDNode {
180
181
}
181
182
var entropy : [ UInt8 ]
182
183
var trueIndex : UInt32
183
- if index >= ( UInt32 ( 1 ) << 31 ) || hardened {
184
+ if index >= UInt32 . maxIterationIndex || hardened {
184
185
trueIndex = index
185
- if trueIndex < ( UInt32 ( 1 ) << 31 ) {
186
- trueIndex = trueIndex + ( UInt32 ( 1 ) << 31 )
186
+ if trueIndex < UInt32 . maxIterationIndex {
187
+ trueIndex = trueIndex + UInt32. maxIterationIndex
187
188
}
188
189
let hmac : Authenticator = HMAC ( key: self . chaincode. bytes, variant: . sha2( . sha512) )
189
190
var inputForHMAC = Data ( )
190
191
inputForHMAC. append ( Data ( [ UInt8 ( 0x00 ) ] ) )
191
192
inputForHMAC. append ( privateKey)
192
193
inputForHMAC. append ( trueIndex. serialize32 ( ) )
193
- guard let ent = try ? hmac. authenticate ( inputForHMAC. bytes) else { return nil }
194
+ guard let ent = try ? hmac. authenticate ( inputForHMAC. bytes) else { return nil }
194
195
guard ent. count == 64 else { return nil }
195
196
entropy = ent
196
197
} else {
@@ -199,7 +200,7 @@ extension HDNode {
199
200
var inputForHMAC = Data ( )
200
201
inputForHMAC. append ( self . publicKey)
201
202
inputForHMAC. append ( trueIndex. serialize32 ( ) )
202
- guard let ent = try ? hmac. authenticate ( inputForHMAC. bytes) else { return nil }
203
+ guard let ent = try ? hmac. authenticate ( inputForHMAC. bytes) else { return nil }
203
204
guard ent. count == 64 else { return nil }
204
205
entropy = ent
205
206
}
@@ -221,7 +222,7 @@ extension HDNode {
221
222
return nil
222
223
}
223
224
guard let privKeyCandidate = newPK. serialize ( ) . setLengthLeft ( 32 ) else { return nil }
224
- guard SECP256K1 . verifyPrivateKey ( privateKey: privKeyCandidate) else { return nil }
225
+ guard SECP256K1 . verifyPrivateKey ( privateKey: privKeyCandidate) else { return nil }
225
226
guard let pubKeyCandidate = SECP256K1 . privateToPublic ( privateKey: privKeyCandidate, compressed: true ) else { return nil }
226
227
guard pubKeyCandidate. bytes [ 0 ] == 0x02 || pubKeyCandidate. bytes [ 0 ] == 0x03 else { return nil }
227
228
guard self . depth < UInt8 . max else { return nil }
0 commit comments