Skip to content

Sign-In with Ethereum + ERC1271 #246

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 8 commits into from
Jul 18, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
114 changes: 114 additions & 0 deletions web3sTests/ERC1271/ERC1271Tests.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,114 @@
//
// ERC1271Tests.swift
//
//
// Created by Rodrigo Kreutz on 15/06/22.
//

import XCTest
@testable import web3

class ERC1271Tests: XCTestCase {
var client: EthereumClientProtocol!
var erc1271: ERC1271!

override func setUp() {
super.setUp()
if self.client == nil {
self.client = EthereumHttpClient(url: URL(string: TestConfig.clientUrl)!)
}
self.erc1271 = ERC1271(client: self.client)
}

override func tearDown() {
super.tearDown()
}

func testSuccesfulVerificationWithMagicNumberContract() async {
do {
let isValid = try await erc1271.isValidSignature(
contract: EthereumAddress("0x2bD85c85666a29bD453918B20b9E5ef7603d9007"),
messageHash: "0xb7755e72da7aca68df7d5ed5a832d027b624d56dab707d2b5257bbfc1bc5d4fd".web3.hexData!,
signature: "0x468732fa8210c6f8481a288a668bd6f40745e67c9640f82f7415b44e7ba280e13b6fce01acaaa4ab2fe8620a179ca99960620a014fdf74d9cf828912811c1b821b".web3.hexData!
)
XCTAssertTrue(isValid)
} catch {
XCTFail("Failed with: \(error)")
}
}

func testSuccessfulVerificationWithBooleanContract() async {
do {
let isValid = try await erc1271.isValidSignature(
contract: EthereumAddress("0x2505E4d4A76EC941591828311159552A832681D5"),
messageHash: "0xb7755e72da7aca68df7d5ed5a832d027b624d56dab707d2b5257bbfc1bc5d4fd".web3.hexData!,
signature: "0x468732fa8210c6f8481a288a668bd6f40745e67c9640f82f7415b44e7ba280e13b6fce01acaaa4ab2fe8620a179ca99960620a014fdf74d9cf828912811c1b821b".web3.hexData!
)
XCTAssertTrue(isValid)
} catch {
XCTFail("Failed with: \(error)")
}
}

func testFailedVerification() async {
do {
// Here the signature and the hash matches, but the contract will say is invalid cause the signer is not the owner of the contract
let isValid = try await erc1271.isValidSignature(
contract: EthereumAddress("0x2bD85c85666a29bD453918B20b9E5ef7603d9007"),
messageHash: "0x09bf2f6417d2bc487040194b78cbdd6b04f72ea12cf0014f83f4f228bed95ee4".web3.hexData!,
signature: "0xf85b9506180b11dc472278ff1e5fbb1e4b50baa3cadaec26b4b8179a55623f652a794c09b49227231f1144a62221453c854f09a986c1de1e19cdfff451e751b21c".web3.hexData!
)
XCTAssertFalse(isValid)
} catch {
XCTFail("Failed with: \(error)")
}

do {
// Here the signature and the hash don't match
let isValid = try await erc1271.isValidSignature(
contract: EthereumAddress("0x2bD85c85666a29bD453918B20b9E5ef7603d9007"),
messageHash: "0xb7755e72da7aca68df7d5ed5a832d027b624d56dab707d2b5257bbfc1bc5d4fd".web3.hexData!,
signature: "0xf85b9506180b11dc472278ff1e5fbb1e4b50baa3cadaec26b4b8179a55623f652a794c09b49227231f1144a62221453c854f09a986c1de1e19cdfff451e751b21c".web3.hexData!
)
XCTAssertFalse(isValid)
} catch {
XCTFail("Failed with: \(error)")
}
}

func testFailedVerificationWithBooleanContract() async {
do {
// Here the signature and the hash matches, but the contract will say is invalid cause the signer is not the owner of the contract
let isValid = try await erc1271.isValidSignature(
contract: EthereumAddress("0x2505E4d4A76EC941591828311159552A832681D5"),
messageHash: "0x09bf2f6417d2bc487040194b78cbdd6b04f72ea12cf0014f83f4f228bed95ee4".web3.hexData!,
signature: "0xf85b9506180b11dc472278ff1e5fbb1e4b50baa3cadaec26b4b8179a55623f652a794c09b49227231f1144a62221453c854f09a986c1de1e19cdfff451e751b21c".web3.hexData!
)
XCTAssertFalse(isValid)
} catch {
XCTFail("Failed with: \(error)")
}

do {
// Here the signature and the hash don't match
let isValid = try await erc1271.isValidSignature(
contract: EthereumAddress("0x2505E4d4A76EC941591828311159552A832681D5"),
messageHash: "0xb7755e72da7aca68df7d5ed5a832d027b624d56dab707d2b5257bbfc1bc5d4fd".web3.hexData!,
signature: "0xf85b9506180b11dc472278ff1e5fbb1e4b50baa3cadaec26b4b8179a55623f652a794c09b49227231f1144a62221453c854f09a986c1de1e19cdfff451e751b21c".web3.hexData!
)
XCTAssertFalse(isValid)
} catch {
XCTFail("Failed with: \(error)")
}
}
}

final class ERC1271WebSocketTests: ERC1271Tests {

override func setUp() {
if self.client == nil {
self.client = EthereumWebSocketClient(url: URL(string: TestConfig.wssUrl)!)
}
super.setUp()
}
}
67 changes: 67 additions & 0 deletions web3sTests/SIWE/SIWETests.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
//
// SIWETests.swift
//
//
// Created by Rodrigo Kreutz on 16/06/22.
//

import XCTest
@testable import web3

class SIWETests: XCTestCase {

var client: EthereumClientProtocol!
var verifier: SiweVerifier!

override func setUp() {
super.setUp()
if self.client == nil {
self.client = EthereumHttpClient(url: URL(string: TestConfig.clientUrl)!)
}
self.verifier = SiweVerifier(client: self.client)
}

func testEndToEnd() async {
let account = try! EthereumAccount.init(keyStorage: TestEthereumKeyStorage(privateKey: "0x4646464646464646464646464646464646464646464646464646464646464646"))
let message = try! SiweMessage(
"""
login.xyz wants you to sign in with your Ethereum account:
\(account.address.toChecksumAddress())

Please sign this 🙏

URI: https://login.xyz/demo#login
Version: 1
Chain ID: 3
Nonce: qwerty123456
Issued At: \(SiweMessage.dateFormatter.string(from: Date()))
Expiration Time: \(SiweMessage.dateFormatter.string(from: Date(timeInterval: 60, since: Date())))
Not Before: \(SiweMessage.dateFormatter.string(from: Date(timeInterval: -60, since: Date())))
Request ID: some-request-id
Resources:
- https://docs.login.xyz
- https://login.xyz
"""
)

var signature: String = ""
XCTAssertNoThrow(signature = try account.signSIWERequest(message))
var isValid = false
do {
isValid = try await verifier.verify(message: message, against: signature)
XCTAssertTrue(isValid)
} catch {
XCTFail("Error thrown while verifying signature: \(error)")
}
}
}

final class SIWEWebSocketTests: SIWETests {

override func setUp() {
if self.client == nil {
self.client = EthereumWebSocketClient(url: URL(string: TestConfig.wssUrl)!)
}
super.setUp()
}
}
Loading