Skip to content

Commit

Permalink
featEIP712): parser test
Browse files Browse the repository at this point in the history
  • Loading branch information
JeneaVranceanu committed Oct 18, 2023
1 parent 354589f commit 0342f91
Show file tree
Hide file tree
Showing 2 changed files with 124 additions and 70 deletions.
124 changes: 124 additions & 0 deletions Tests/web3swiftTests/localTests/EIP712ParserTests.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,124 @@
//
// EIP712ParserTests.swift
//
//
// Created by JeneaVranceanu on 18.10.2023.
//

import Foundation
import XCTest
import web3swift
import Web3Core

class EIP712ParserTests: 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)

XCTAssertEqual(parsedEip712TypedData.types.count, 3)
let eip712Domain = parsedEip712TypedData.types["EIP712Domain"]
XCTAssertNotNil(eip712Domain)
let person = parsedEip712TypedData.types["Person"]
XCTAssertNotNil(person)
let mail = parsedEip712TypedData.types["Mail"]
XCTAssertNotNil(mail)


XCTAssertNotNil(eip712Domain?.first { $0.name == "name" && $0.type == "string"})
XCTAssertNotNil(eip712Domain?.first { $0.name == "version" && $0.type == "string"})
XCTAssertNotNil(eip712Domain?.first { $0.name == "chainId" && $0.type == "uint256"})
XCTAssertNotNil(eip712Domain?.first { $0.name == "verifyingContract" && $0.type == "address"})


XCTAssertNotNil(person?.first { $0.name == "name" && $0.type == "string"})
XCTAssertNotNil(person?.first { $0.name == "wallet" && $0.type == "address"})

XCTAssertNotNil(mail?.first { $0.name == "from" && $0.type == "Person"})
XCTAssertNotNil(mail?.first { $0.name == "to" && $0.type == "Person"})
XCTAssertNotNil(mail?.first { $0.name == "contents" && $0.type == "string"})

XCTAssertEqual(parsedEip712TypedData.primaryType, "Mail")

XCTAssertEqual(parsedEip712TypedData.domain.count, 4)
XCTAssertEqual(parsedEip712TypedData.domain["name"] as? String, "Ether Mail")
XCTAssertEqual(parsedEip712TypedData.domain["version"] as? String, "1")
XCTAssertEqual(parsedEip712TypedData.domain["chainId"] as? Int, 1)
XCTAssertEqual(parsedEip712TypedData.domain["verifyingContract"] as? String, "0xCcCCccccCCCCcCCCCCCcCcCccCcCCCcCcccccccC")


XCTAssertEqual(parsedEip712TypedData.message.count, 3)
XCTAssertEqual(parsedEip712TypedData.message["from"] as? [String : String],
["name" : "Cow",
"wallet" : "0xCD2a3d9F938E13CD947Ec05AbC7FE734Df8DD826"])
XCTAssertEqual(parsedEip712TypedData.message["to"] as? [String : String],
["name" : "Bob",
"wallet" : "0xbBbBBBBbbBBBbbbBbbBbbbbBBbBbbbbBbBbbBBbB"])
XCTAssertEqual(parsedEip712TypedData.message["contents"] as? String, "Hello, Bob!")
}
}
70 changes: 0 additions & 70 deletions Tests/web3swiftTests/localTests/EIP712Tests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -3,72 +3,6 @@ import Web3Core
@testable import web3swift

class EIP712Tests: 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 testWithoutChainId() throws {
let to = EthereumAddress("0x3F06bAAdA68bB997daB03d91DBD0B73e196c5A4d")!
Expand Down Expand Up @@ -171,8 +105,4 @@ class EIP712Tests: XCTestCase {
chainId: chainId)
XCTAssertEqual(signature.toHexString(), "9ee2aadf14739e1cafc3bc1a0b48457c12419d5b480a8ffa86eb7df538c82d0753ca2a6f8024dea576b383cbcbe5e2b181b087e489298674bf6512756cabc5b01b")
}

func testEIP712Parser() throws {
try NSLog(String.init(describing: EIP712Parser.parse(testTypedDataPayload)))
}
}

0 comments on commit 0342f91

Please sign in to comment.