Skip to content

Commit 02c7e42

Browse files
fix
1 parent 5be6ea6 commit 02c7e42

File tree

6 files changed

+30
-15
lines changed

6 files changed

+30
-15
lines changed

Sources/web3swift/Convenience/SECP256k1.swift

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -184,6 +184,8 @@ extension SECP256K1 {
184184
v -= 27
185185
} else if v >= 31 && v <= 34 {
186186
v -= 31
187+
} else if v >= 35 && v <= 38 {
188+
v -= 35
187189
}
188190
let result = serializedSignature.withUnsafeBytes { (serRawBufferPtr: UnsafeRawBufferPointer) -> Int32? in
189191
if let serRawPtr = serRawBufferPtr.baseAddress, serRawBufferPtr.count > 0 {
@@ -221,9 +223,9 @@ extension SECP256K1 {
221223
guard let res = result, res != 0 else {
222224
return nil
223225
}
224-
if (v == 0) {
226+
if (v == 0 || v == 27 || v == 31 || v == 35) {
225227
serializedSignature.append(0x1b)
226-
} else if (v == 1) {
228+
} else if (v == 1 || v == 28 || v == 32 || v == 36) {
227229
serializedSignature.append(0x1c)
228230
} else {
229231
return nil

Sources/web3swift/EthereumABI/ABIDecoding.swift

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -67,13 +67,15 @@ extension ABIDecoder {
6767
let dataSlice = elementItself[0 ..< 32]
6868
let v = BigUInt(dataSlice)
6969
// print("Address element is: \n" + String(v))
70-
if v == BigUInt(28) {
70+
if v == BigUInt(36) ||
71+
v == BigUInt(32) ||
72+
v == BigUInt(28) ||
73+
v == BigUInt(1) {
7174
return (true as AnyObject, type.memoryUsage)
72-
} else if (v == BigUInt(27)) {
73-
return (false as AnyObject, type.memoryUsage)
74-
} else if (v == BigUInt(1)) {
75-
return (true as AnyObject, type.memoryUsage)
76-
} else if (v == BigUInt(0)) {
75+
} else if v == BigUInt(35) ||
76+
v == BigUInt(31) ||
77+
v == BigUInt(27) ||
78+
v == BigUInt(0) {
7779
return (false as AnyObject, type.memoryUsage)
7880
}
7981
case .bytes(let length):

Sources/web3swift/HookedFunctions/Web3+BrowserFunctions.swift

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,8 @@ extension web3.BrowserFunctions {
6363
vData -= 27
6464
} else if vData >= 31 && vData <= 34 {
6565
vData -= 31
66+
} else if vData >= 35 && vData <= 38 {
67+
vData -= 35
6668
}
6769
guard let signatureData = SECP256K1.marshalSignature(v: vData, r: rData, s: sData) else {return nil}
6870
guard let hash = Web3.Utils.hashPersonalMessage(personalMessage) else {return nil}

Sources/web3swift/Transaction/EthereumTransaction.swift

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -115,17 +115,19 @@ public struct EthereumTransaction: CustomStringConvertible {
115115
var normalizedV:BigUInt = BigUInt(27)
116116
let inferedChainID = self.inferedChainID
117117
var d = BigUInt(0)
118-
if self.v >= 39 {
118+
if self.v >= 35 && self.v <= 38 {
119119
d = BigUInt(35)
120-
} else if self.v >= 35 && self.v <= 38 {
121-
d = BigUInt(8)
120+
} else if self.v >= 31 && self.v <= 34 {
121+
d = BigUInt(31)
122+
} else if self.v >= 27 && self.v <= 30 {
123+
d = BigUInt(27)
122124
}
123125
if (self.chainID != nil && self.chainID != BigUInt(0)) {
124126
normalizedV = self.v - d - self.chainID! - self.chainID!
125127
} else if (inferedChainID != nil) {
126128
normalizedV = self.v - d - inferedChainID! - inferedChainID!
127129
} else {
128-
normalizedV = self.v
130+
normalizedV = self.v - d
129131
}
130132
guard let vData = normalizedV.serialize().setLengthLeft(1) else {return nil}
131133
guard let rData = r.serialize().setLengthLeft(32) else {return nil}

Sources/web3swift/Transaction/TransactionSigner.swift

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -92,12 +92,15 @@ public struct Web3Signer {
9292
let originalPublicKey = SECP256K1.privateToPublic(privateKey: privateKey)
9393
transaction.chainID = nil
9494
var d = BigUInt(0)
95+
var a = BigUInt(0)
9596
if unmarshalledSignature.v >= 0 && unmarshalledSignature.v <= 3 {
9697
d = BigUInt(27)
97-
} else if unmarshalledSignature.v >= 27 && unmarshalledSignature.v <= 30 {
98-
d = BigUInt(0)
98+
} else if unmarshalledSignature.v >= 31 && unmarshalledSignature.v <= 34 {
99+
a = BigUInt(4)
100+
} else if unmarshalledSignature.v >= 35 && unmarshalledSignature.v <= 38 {
101+
a = BigUInt(8)
99102
}
100-
transaction.v = BigUInt(unmarshalledSignature.v) + d
103+
transaction.v = BigUInt(unmarshalledSignature.v) + d - a
101104
transaction.r = BigUInt(Data(unmarshalledSignature.r))
102105
transaction.s = BigUInt(Data(unmarshalledSignature.s))
103106
let recoveredPublicKey = transaction.recoverPublicKey()

Sources/web3swift/Web3/Web3+Utils.swift

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6118,6 +6118,8 @@ extension Web3.Utils {
61186118
vData -= 27
61196119
} else if vData >= 31 && vData <= 34 {
61206120
vData -= 31
6121+
} else if vData >= 35 && vData <= 38 {
6122+
vData -= 35
61216123
}
61226124

61236125
guard let signatureData = SECP256K1.marshalSignature(v: vData, r: rData, s: sData) else {return nil}
@@ -6140,6 +6142,8 @@ extension Web3.Utils {
61406142
vData -= 27
61416143
} else if vData >= 31 && vData <= 34 {
61426144
vData -= 31
6145+
} else if vData >= 35 && vData <= 38 {
6146+
vData -= 35
61436147
}
61446148
guard let signatureData = SECP256K1.marshalSignature(v: vData, r: rData, s: sData) else {return nil}
61456149
guard let publicKey = SECP256K1.recoverPublicKey(hash: hash, signature: signatureData) else {return nil}

0 commit comments

Comments
 (0)