Skip to content
This repository has been archived by the owner on Sep 13, 2024. It is now read-only.

Commit

Permalink
Fixed invalid signature from web3.js
Browse files Browse the repository at this point in the history
  • Loading branch information
v57 committed Nov 29, 2018
1 parent 9da555c commit 664aac2
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 1 deletion.
10 changes: 9 additions & 1 deletion Sources/Convenience/LibSecp256k1Extension.swift
Original file line number Diff line number Diff line change
Expand Up @@ -258,7 +258,15 @@ struct SECP256K1 {
try signature.checkSignatureSize()
var recoverableSignature = secp256k1_ecdsa_recoverable_signature()
let serializedSignature = Data(signature[0 ..< 64])
let v = Int32(signature[64])
var v = Int32(signature[64])

/*
fix for web3.js signs
eth-lib code: vrs.v < 2 ? vrs.v : 1 - (vrs.v % 2)
https://github.com/MaiaVictor/eth-lib/blob/d959c54faa1e1ac8d474028ed1568c5dce27cc7a/src/account.js#L60
*/
v = v < 2 ? v : 1 - (v % 2)

let result = serializedSignature.withUnsafeBytes { (serPtr: UnsafePointer<UInt8>) -> Int32 in
withUnsafeMutablePointer(to: &recoverableSignature, { (signaturePointer: UnsafeMutablePointer<secp256k1_ecdsa_recoverable_signature>) in
secp256k1_ecdsa_recoverable_signature_parse_compact(context!, signaturePointer, serPtr, v)
Expand Down
14 changes: 14 additions & 0 deletions Tests/SECP256K1Tests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -98,4 +98,18 @@ class SECP256K1Tests: XCTestCase {
}
}
}

func testSomeSignatures() throws {

let password = "Your password"
let keystore = try! BIP32Keystore(mnemonics: Mnemonics(), password: password)

for i in 1...3 {
print("Signing \(i)/10")
let message = "\(i) Hello World \(i)".data
let signature = try! Web3Signer.signPersonalMessage(message, keystore: keystore, account: keystore.addresses[0], password: password)
let address = try! Web3.default.personal.ecrecover(personalMessage: message, signature: signature)
XCTAssertEqual(address,keystore.addresses[0])
}
}
}

0 comments on commit 664aac2

Please sign in to comment.