Skip to content

Commit

Permalink
chore(EIP712): new test + moved EIP712 reused data into one file;
Browse files Browse the repository at this point in the history
  • Loading branch information
JeneaVranceanu committed Oct 18, 2023
1 parent 07965a1 commit 5493dd0
Show file tree
Hide file tree
Showing 3 changed files with 95 additions and 74 deletions.
76 changes: 76 additions & 0 deletions Tests/web3swiftTests/localTests/EIP712TestData.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
//
// EIP712TestData.swift
//
// Created by JeneaVranceanu on 19.10.2023.
//

import Foundation

class EIP712TestData {
static let testTypedDataPayload = """
{
"types":{
"EIP712Domain":[
{
"name":"name",
"type":"string"
},
{
"name":"version",
"type":"string"
},
{
"name":"chainId",
"type":"uint256"
},
{
"name":"verifyingContract",
"type":"address"
}
],
"Person":[
{
"name":"name",
"type":"string"
},
{
"name":"wallet",
"type":"address"
}
],
"Mail":[
{
"name":"from",
"type":"Person"
},
{
"name":"to",
"type":"Person"
},
{
"name":"contents",
"type":"string"
}
]
},
"primaryType":"Mail",
"domain":{
"name":"Ether Mail",
"version":"1",
"chainId":1,
"verifyingContract":"0xCcCCccccCCCCcCCCCCCcCcCccCcCCCcCcccccccC"
},
"message":{
"from":{
"name":"Cow",
"wallet":"0xCD2a3d9F938E13CD947Ec05AbC7FE734Df8DD826"
},
"to":{
"name":"Bob",
"wallet":"0xbBbBBBBbbBBBbbbBbbBbbbbBBbBbbbbBbBbbBBbB"
},
"contents":"Hello, Bob!"
}
}
"""
}
12 changes: 12 additions & 0 deletions Tests/web3swiftTests/localTests/EIP712Tests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -105,4 +105,16 @@ class EIP712Tests: XCTestCase {
chainId: chainId)
XCTAssertEqual(signature.toHexString(), "9ee2aadf14739e1cafc3bc1a0b48457c12419d5b480a8ffa86eb7df538c82d0753ca2a6f8024dea576b383cbcbe5e2b181b087e489298674bf6512756cabc5b01b")
}

func testEIP712TypedDataSigning() throws {
let mnemonic = "normal dune pole key case cradle unfold require tornado mercy hospital buyer"
let keystore = try! BIP32Keystore(mnemonics: mnemonic, password: "", mnemonicsPassword: "")!
let account = keystore.addresses?[0]
let eip712TypedData = try EIP712Parser.parse(EIP712TestData.testTypedDataPayload)
let signature = try Web3Signer.signEIP712(
eip712TypedData,
keystore: keystore,
account: account!)
XCTAssertEqual(signature.toHexString(), "70d1f5d9eac7b6303683d0792ea8dc93369e3b79888c4e0b86121bec19f479ba4067cf7ac3f8208cbc60a706c4793c2c17e19637298bb31642e531619272b26e1b")
}
}
81 changes: 7 additions & 74 deletions Tests/web3swiftTests/localTests/EIP712TypedDataPayloadTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -13,75 +13,8 @@ import web3swift

/// Tests based primarily on the following example https://eips.ethereum.org/assets/eip-712/Example.js
class EIP712TypedDataPayloadTests: XCTestCase {
let testTypedDataPayload = """
{
"types":{
"EIP712Domain":[
{
"name":"name",
"type":"string"
},
{
"name":"version",
"type":"string"
},
{
"name":"chainId",
"type":"uint256"
},
{
"name":"verifyingContract",
"type":"address"
}
],
"Person":[
{
"name":"name",
"type":"string"
},
{
"name":"wallet",
"type":"address"
}
],
"Mail":[
{
"name":"from",
"type":"Person"
},
{
"name":"to",
"type":"Person"
},
{
"name":"contents",
"type":"string"
}
]
},
"primaryType":"Mail",
"domain":{
"name":"Ether Mail",
"version":"1",
"chainId":1,
"verifyingContract":"0xCcCCccccCCCCcCCCCCCcCcCccCcCCCcCcccccccC"
},
"message":{
"from":{
"name":"Cow",
"wallet":"0xCD2a3d9F938E13CD947Ec05AbC7FE734Df8DD826"
},
"to":{
"name":"Bob",
"wallet":"0xbBbBBBBbbBBBbbbBbbBbbbbBBbBbbbbBbBbbBBbB"
},
"contents":"Hello, Bob!"
}
}
"""

func testEIP712Parser() throws {
let parsedEip712TypedData = try EIP712Parser.parse(testTypedDataPayload)
let parsedEip712TypedData = try EIP712Parser.parse(EIP712TestData.testTypedDataPayload)

XCTAssertEqual(parsedEip712TypedData.types.count, 3)
let eip712Domain = parsedEip712TypedData.types["EIP712Domain"]
Expand Down Expand Up @@ -210,21 +143,21 @@ class EIP712TypedDataPayloadTests: XCTestCase {
}

func testEIP712EncodeType() throws {
let parsedEip712TypedData = try EIP712Parser.parse(testTypedDataPayload)
let parsedEip712TypedData = try EIP712Parser.parse(EIP712TestData.testTypedDataPayload)
try XCTAssertEqual(parsedEip712TypedData.encodeType("EIP712Domain"), "EIP712Domain(string name,string version,uint256 chainId,address verifyingContract)")
try XCTAssertEqual(parsedEip712TypedData.encodeType("Person"), "Person(string name,address wallet)")
try XCTAssertEqual(parsedEip712TypedData.encodeType("Mail"), "Mail(Person from,Person to,string contents)Person(string name,address wallet)")
}

func testEIP712TypeHash() throws {
let parsedEip712TypedData = try EIP712Parser.parse(testTypedDataPayload)
let parsedEip712TypedData = try EIP712Parser.parse(EIP712TestData.testTypedDataPayload)
try XCTAssertEqual(parsedEip712TypedData.typeHash("EIP712Domain"), "0x8b73c3c69bb8fe3d512ecc4cf759cc79239f7b179b0ffacaa9a75d522b39400f")
try XCTAssertEqual(parsedEip712TypedData.typeHash("Person"), "0xb9d8c78acf9b987311de6c7b45bb6a9c8e1bf361fa7fd3467a2163f994c79500")
try XCTAssertEqual(parsedEip712TypedData.typeHash("Mail"), "0xa0cedeb2dc280ba39b857546d74f5549c3a1d7bdc2dd96bf881f76108e23dac2")
}

func testEIP712EncodeData() throws {
let parsedEip712TypedData = try EIP712Parser.parse(testTypedDataPayload)
let parsedEip712TypedData = try EIP712Parser.parse(EIP712TestData.testTypedDataPayload)
let encodedMessage = "a0cedeb2dc280ba39b857546d74f5549c3a1d7bdc2dd96bf881f76108e23dac2fc71e5fa27ff56c350aa531bc129ebdf613b772b6604664f5d8dbe21b85eb0c8cd54f074a4af31b4411ff6a60c9719dbd559c221c8ac3492d9d872b041d703d1b5aadf3154a261abdd9086fc627b61efca26ae5702701d05cd2305f7c52a2fc8"
XCTAssertEqual(try parsedEip712TypedData.encodeData().toHexString(), encodedMessage)
XCTAssertEqual(try parsedEip712TypedData.encodeData(parsedEip712TypedData.primaryType, data: parsedEip712TypedData.message).toHexString(), encodedMessage)
Expand All @@ -242,19 +175,19 @@ class EIP712TypedDataPayloadTests: XCTestCase {
}

func testEIP712StructHash() throws {
let parsedEip712TypedData = try EIP712Parser.parse(testTypedDataPayload)
let parsedEip712TypedData = try EIP712Parser.parse(EIP712TestData.testTypedDataPayload)
XCTAssertEqual(try parsedEip712TypedData.structHash().toHexString(), "c52c0ee5d84264471806290a3f2c4cecfc5490626bf912d01f240d7a274b371e")
XCTAssertEqual(try parsedEip712TypedData.structHash("EIP712Domain", data: parsedEip712TypedData.domain).toHexString(),
"f2cee375fa42b42143804025fc449deafd50cc031ca257e0b194a650a912090f")
}

func testEIP712SignHash() throws {
let parsedEip712TypedData = try EIP712Parser.parse(testTypedDataPayload)
let parsedEip712TypedData = try EIP712Parser.parse(EIP712TestData.testTypedDataPayload)
XCTAssertEqual(try parsedEip712TypedData.signHash().toHexString(), "be609aee343fb3c4b28e1df9e632fca64fcfaede20f02e86244efddf30957bd2")
}

func testEIP712Signing() throws {
let parsedEip712TypedData = try EIP712Parser.parse(testTypedDataPayload)
let parsedEip712TypedData = try EIP712Parser.parse(EIP712TestData.testTypedDataPayload)
let privateKey = Data.fromHex("cow".sha3(.keccak256).addHexPrefix())!
let publicKey = Utilities.privateToPublic(privateKey)!
let address = Utilities.publicToAddress(publicKey)!
Expand Down

0 comments on commit 5493dd0

Please sign in to comment.