Skip to content

Commit

Permalink
Merge branch 'release/1.4.15'
Browse files Browse the repository at this point in the history
  • Loading branch information
Foboz committed Jan 4, 2022
2 parents 63823d1 + 842efac commit 43d2997
Show file tree
Hide file tree
Showing 4 changed files with 59 additions and 32 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,15 @@ class MEWconnectSignatureTests: QuickSpec {
expect(signedDataLeadingV) == testResultLeadingV
expect(signedDataTrailingV) == testResultTrailingV
}
it("Should have correct length") {
let testKey = Data(hex: "064701b9218c1a1893d8ef7e33f45afa11d4bf986fa7f815e8b23a2dc8b4d89b")
let data = "476020729".hashPersonalMessage()
let signature = data?.sign(key: testKey, leadingV: false)

let correctSignature = Data(hex: "cb739fd04d52879de00721dcdf62ba32a79c8d8bf39c8802fb9d7154b41b2d15003ac8f68b4b1e5394831dc9ced7350bde2776024888558e1d6fe07d4f7d12f71c")
expect(signature) == correctSignature
expect(signature?.count) == 65
}

it("Should multiply EC correctly") {
let testPrivateKey = Data(hex: "db391b235fc493cbcab9f5d2ed9582036606b946b8685995db4a17df2b87e2a2")
Expand Down
56 changes: 28 additions & 28 deletions Sources/Classes/BigInt/RLPBigInt.swift
Original file line number Diff line number Diff line change
Expand Up @@ -10,38 +10,38 @@ import Foundation
import BigInt

public struct RLPBigInt {
public let value: BigInt

public init(value: BigInt) {
self.value = value
public let value: BigInt

public init(value: BigInt) {
self.value = value
}

private var _dataCount: Int?

var dataLength: Int {
get {
return self._dataCount ?? _data.count
}

private var _dataCount: Int?

var dataLength: Int {
get {
return self._dataCount ?? _data.count
}
set {
_dataCount = newValue
}
set {
_dataCount = newValue
}

internal var data: Data {
var data = Data(_data)
if let count = self._dataCount {
data.setLength(count, appendFromLeft: true)
}
return data
}

private var _data: [UInt8] {
return value.toTwosComplement().bytes
}

internal var data: Data {
var data = Data(_data)
if let count = self._dataCount {
data.setLength(count, appendFromLeft: true)
}
return data
}

private var _data: [UInt8] {
return value.toTwosComplement().bytes
}
}

extension BigInt {
func toRLP() -> RLPBigInt {
return RLPBigInt(value: self)
}
func toRLP() -> RLPBigInt {
return RLPBigInt(value: self)
}
}
24 changes: 21 additions & 3 deletions Sources/eips/eip155/TransactionSignature.swift
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ internal struct TransactionSignature: CustomDebugStringConvertible {
}
}

init(signature: Data, chainID: BigInt? = nil) throws {
init(signature: Data, chainID: BigInt? = nil, normalized: Bool = false) throws {
guard signature.count == 65 else {
throw TransactionSignatureError.invalidSignature
}
Expand All @@ -48,24 +48,33 @@ internal struct TransactionSignature: CustomDebugStringConvertible {
}

self.chainID = chainID ?? BigInt()
if normalized {
self._normalize()
}
}

// swiftlint:disable identifier_name
init(r: BigInt, s: BigInt, v: BigInt, chainID: BigInt? = nil) {
init(r: BigInt, s: BigInt, v: BigInt, chainID: BigInt? = nil, normalized: Bool = false) {
self.r = r.toRLP()
self.s = s.toRLP()
self.v = v.toRLP()
self.signatureYParity = v.toRLP()
self.chainID = chainID ?? BigInt()
if normalized {
self._normalize()
}
}

init(r: String, s: String, v: String, chainID: BigInt? = nil) throws {
init(r: String, s: String, v: String, chainID: BigInt? = nil, normalized: Bool = false) throws {
self.r = BigInt(Data(hex: r).bytes).toRLP()
self.s = BigInt(Data(hex: s).bytes).toRLP()
self.v = BigInt(Data(hex: v).bytes).toRLP()
self.signatureYParity = self.v

self.chainID = chainID ?? BigInt()
if normalized {
self._normalize()
}
}
// swiftlint:enable identifier_name

Expand Down Expand Up @@ -94,6 +103,15 @@ internal struct TransactionSignature: CustomDebugStringConvertible {
guard let publicKey = signature.secp256k1RecoverPublicKey(hash: hash, context: context) else { return nil }
return publicKey
}

// MARK: - Normalization

private mutating func _normalize() {
self.r.dataLength = 32
self.s.dataLength = 32
self.signatureYParity.dataLength = 1
self.v.dataLength = 1
}

// MARK: - CustomDebugStringConvertible

Expand Down
2 changes: 1 addition & 1 deletion Sources/jsonrpc/Extensions/Data+EthSign.swift
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ internal extension Data {
return nil
}
do {
let signature = try TransactionSignature(signature: serializedRecoverableSignature)
let signature = try TransactionSignature(signature: serializedRecoverableSignature, normalized: true)
var signed = Data()
if leadingV {
signed.append(signature.v.data)
Expand Down

0 comments on commit 43d2997

Please sign in to comment.