@@ -69,7 +69,7 @@ public class HDNode {
69
69
}
70
70
71
71
public init ? ( _ data: Data ) {
72
- guard data. count == 82 else { return nil }
72
+ guard data. count == 82 else { return nil }
73
73
let header = data [ 0 ..< 4 ]
74
74
var serializePrivate = false
75
75
if header == HDNode . HDversion ( ) . privatePrefix {
@@ -81,30 +81,30 @@ public class HDNode {
81
81
chaincode = data [ 13 ..< 45 ]
82
82
if serializePrivate {
83
83
privateKey = data [ 46 ..< 78 ]
84
- guard let pubKey = Utilities . privateToPublic ( privateKey!, compressed: true ) else { return nil }
85
- if pubKey [ 0 ] != 0x02 && pubKey [ 0 ] != 0x03 { return nil }
84
+ guard let pubKey = Utilities . privateToPublic ( privateKey!, compressed: true ) else { return nil }
85
+ if pubKey [ 0 ] != 0x02 && pubKey [ 0 ] != 0x03 { return nil }
86
86
publicKey = pubKey
87
87
} else {
88
88
publicKey = data [ 45 ..< 78 ]
89
89
}
90
90
let hashedData = data [ 0 ..< 78 ] . sha256 ( ) . sha256 ( )
91
91
let checksum = hashedData [ 0 ..< 4 ]
92
- if checksum != data [ 78 ..< 82 ] { return nil }
92
+ if checksum != data [ 78 ..< 82 ] { return nil }
93
93
}
94
94
95
95
public init ? ( seed: Data ) {
96
- guard seed. count >= 16 else { return nil }
96
+ guard seed. count >= 16 else { return nil }
97
97
let hmacKey = " Bitcoin seed " . data ( using: . ascii) !
98
98
let hmac : Authenticator = HMAC ( key: hmacKey. bytes, variant: HMAC . Variant. sha2 ( . sha512) )
99
- guard let entropy = try ? hmac. authenticate ( seed. bytes) else { return nil }
99
+ guard let entropy = try ? hmac. authenticate ( seed. bytes) else { return nil }
100
100
guard entropy. count == 64 else { return nil }
101
101
let I_L = entropy [ 0 ..< 32 ]
102
102
let I_R = entropy [ 32 ..< 64 ]
103
103
chaincode = Data ( I_R)
104
104
let privKeyCandidate = Data ( I_L)
105
- guard SECP256K1 . verifyPrivateKey ( privateKey: privKeyCandidate) else { return nil }
106
- guard let pubKeyCandidate = SECP256K1 . privateToPublic ( privateKey: privKeyCandidate, compressed: true ) else { return nil }
107
- guard pubKeyCandidate. bytes [ 0 ] == 0x02 || pubKeyCandidate. bytes [ 0 ] == 0x03 else { return nil }
105
+ guard SECP256K1 . verifyPrivateKey ( privateKey: privKeyCandidate) else { return nil }
106
+ guard let pubKeyCandidate = SECP256K1 . privateToPublic ( privateKey: privKeyCandidate, compressed: true ) else { return nil }
107
+ guard pubKeyCandidate. bytes [ 0 ] == 0x02 || pubKeyCandidate. bytes [ 0 ] == 0x03 else { return nil }
108
108
publicKey = pubKeyCandidate
109
109
privateKey = privKeyCandidate
110
110
depth = 0x00
@@ -135,7 +135,7 @@ extension HDNode {
135
135
inputForHMAC. append ( Data ( [ UInt8 ( 0x00 ) ] ) )
136
136
inputForHMAC. append ( self . privateKey!)
137
137
inputForHMAC. append ( trueIndex. serialize32 ( ) )
138
- guard let ent = try ? hmac. authenticate ( inputForHMAC. bytes) else { return nil }
138
+ guard let ent = try ? hmac. authenticate ( inputForHMAC. bytes) else { return nil }
139
139
guard ent. count == 64 else { return nil }
140
140
entropy = ent
141
141
} else {
@@ -144,7 +144,7 @@ extension HDNode {
144
144
var inputForHMAC = Data ( )
145
145
inputForHMAC. append ( self . publicKey)
146
146
inputForHMAC. append ( trueIndex. serialize32 ( ) )
147
- guard let ent = try ? hmac. authenticate ( inputForHMAC. bytes) else { return nil }
147
+ guard let ent = try ? hmac. authenticate ( inputForHMAC. bytes) else { return nil }
148
148
guard ent. count == 64 else { return nil }
149
149
entropy = ent
150
150
}
@@ -165,11 +165,11 @@ extension HDNode {
165
165
}
166
166
return nil
167
167
}
168
- guard let privKeyCandidate = newPK. serialize ( ) . setLengthLeft ( 32 ) else { return nil }
169
- guard SECP256K1 . verifyPrivateKey ( privateKey: privKeyCandidate) else { return nil }
170
- guard let pubKeyCandidate = SECP256K1 . privateToPublic ( privateKey: privKeyCandidate, compressed: true ) else { return nil }
171
- guard pubKeyCandidate. bytes [ 0 ] == 0x02 || pubKeyCandidate. bytes [ 0 ] == 0x03 else { return nil }
172
- guard self . depth < UInt8 . max else { return nil }
168
+ guard let privKeyCandidate = newPK. serialize ( ) . setLengthLeft ( 32 ) else { return nil }
169
+ guard SECP256K1 . verifyPrivateKey ( privateKey: privKeyCandidate) else { return nil }
170
+ guard let pubKeyCandidate = SECP256K1 . privateToPublic ( privateKey: privKeyCandidate, compressed: true ) else { return nil }
171
+ guard pubKeyCandidate. bytes [ 0 ] == 0x02 || pubKeyCandidate. bytes [ 0 ] == 0x03 else { return nil }
172
+ guard self . depth < UInt8 . max else { return nil }
173
173
let newNode = HDNode ( )
174
174
newNode. chaincode = cc
175
175
newNode. depth = self . depth + 1
@@ -201,7 +201,7 @@ extension HDNode {
201
201
var inputForHMAC = Data ( )
202
202
inputForHMAC. append ( self . publicKey)
203
203
inputForHMAC. append ( index. serialize32 ( ) )
204
- guard let ent = try ? hmac. authenticate ( inputForHMAC. bytes) else { return nil }
204
+ guard let ent = try ? hmac. authenticate ( inputForHMAC. bytes) else { return nil }
205
205
guard ent. count == 64 else { return nil }
206
206
entropy = ent
207
207
}
@@ -215,13 +215,13 @@ extension HDNode {
215
215
}
216
216
return nil
217
217
}
218
- guard let tempKey = bn. serialize ( ) . setLengthLeft ( 32 ) else { return nil }
219
- guard SECP256K1 . verifyPrivateKey ( privateKey: tempKey) else { return nil }
220
- guard let pubKeyCandidate = SECP256K1 . privateToPublic ( privateKey: tempKey, compressed: true ) else { return nil }
221
- guard pubKeyCandidate. bytes [ 0 ] == 0x02 || pubKeyCandidate. bytes [ 0 ] == 0x03 else { return nil }
222
- guard let newPublicKey = SECP256K1 . combineSerializedPublicKeys ( keys: [ self . publicKey, pubKeyCandidate] , outputCompressed: true ) else { return nil }
223
- guard newPublicKey. bytes [ 0 ] == 0x02 || newPublicKey. bytes [ 0 ] == 0x03 else { return nil }
224
- guard self . depth < UInt8 . max else { return nil }
218
+ guard let tempKey = bn. serialize ( ) . setLengthLeft ( 32 ) else { return nil }
219
+ guard SECP256K1 . verifyPrivateKey ( privateKey: tempKey) else { return nil }
220
+ guard let pubKeyCandidate = SECP256K1 . privateToPublic ( privateKey: tempKey, compressed: true ) else { return nil }
221
+ guard pubKeyCandidate. bytes [ 0 ] == 0x02 || pubKeyCandidate. bytes [ 0 ] == 0x03 else { return nil }
222
+ guard let newPublicKey = SECP256K1 . combineSerializedPublicKeys ( keys: [ self . publicKey, pubKeyCandidate] , outputCompressed: true ) else { return nil }
223
+ guard newPublicKey. bytes [ 0 ] == 0x02 || newPublicKey. bytes [ 0 ] == 0x03 else { return nil }
224
+ guard self . depth < UInt8 . max else { return nil }
225
225
let newNode = HDNode ( )
226
226
newNode. chaincode = cc
227
227
newNode. depth = self . depth + 1
@@ -255,22 +255,22 @@ extension HDNode {
255
255
if component. hasSuffix ( " ' " ) {
256
256
hardened = true
257
257
}
258
- guard let index = UInt32 ( component. trimmingCharacters ( in: CharacterSet ( charactersIn: " ' " ) ) ) else { return nil }
259
- guard let newNode = currentNode. derive ( index: index, derivePrivateKey: derivePrivateKey, hardened: hardened) else { return nil }
258
+ guard let index = UInt32 ( component. trimmingCharacters ( in: CharacterSet ( charactersIn: " ' " ) ) ) else { return nil }
259
+ guard let newNode = currentNode. derive ( index: index, derivePrivateKey: derivePrivateKey, hardened: hardened) else { return nil }
260
260
currentNode = newNode
261
261
}
262
262
return currentNode
263
263
}
264
264
265
265
public func serializeToString( serializePublic: Bool = true , version: HDversion = HDversion ( ) ) -> String ? {
266
- guard let data = self . serialize ( serializePublic: serializePublic, version: version) else { return nil }
266
+ guard let data = self . serialize ( serializePublic: serializePublic, version: version) else { return nil }
267
267
let encoded = Base58 . base58FromBytes ( data. bytes)
268
268
return encoded
269
269
}
270
270
271
271
public func serialize( serializePublic: Bool = true , version: HDversion = HDversion ( ) ) -> Data ? {
272
272
var data = Data ( )
273
- if !serializePublic && !self . hasPrivate { return nil }
273
+ if !serializePublic && !self . hasPrivate { return nil }
274
274
if serializePublic {
275
275
data. append ( version. publicPrefix)
276
276
} else {
0 commit comments