Repository Public
1// =============================================================================2// SP BOUCHER | SOFTWARE & RESEARCH3// ARTIFICIAL INTELLIGENCE • FINANCE • ENGINEERING4// =============================================================================5//6// Project : QYVORA7// File : Tests/QyvoraEthereumTests/EthereumTests.swift8// Module : Tests9// Repository : https://git.spboucher.ai/qyvora10// Project Website : Not specified11// Created : 2026-12-0412// Last Modified : 2026-12-0413//14// Author : Simon-Pierre Boucher15// Position : Professor of Finance16// Institution : Université du Québec en Outaouais (UQO)17// Organization : SP Boucher18// Website : https://spboucher.ai19// Git : https://git.spboucher.ai20//21// Preferred Contact: contact@spboucher.ai22// Development : dev@spboucher.ai23// Support : support@spboucher.ai24// Security/Admin : admin@spboucher.ai25//26// Copyright (c) 202627// Simon-Pierre Boucher28// All rights reserved where applicable. This file is governed by the29// applicable project license, copyright notices and relevant law.30// Existing third-party rights and notices remain unaffected.31//32// License : None specified (no license file in this repository)33//34// Do not embed credentials, private keys, access tokens,35// certificates or confidential personal information.36//37// =============================================================================38// END OF SP BOUCHER SOURCE HEADER39// =============================================================================4041import Testing42import QyvoraMath43import QyvoraKeccak44import QyvoraSecp256k145import QyvoraCore46@testable import QyvoraEthereum4748@Suite struct EthereumTests {49 @Test func rlpVectors() throws {50 #expect(RLP.bytes(Bytes("dog".utf8)).encoded.hex == "83646f67")51 #expect(RLP.list([.bytes(Bytes("cat".utf8)), .bytes(Bytes("dog".utf8))]).encoded.hex == "c88363617483646f67")52 #expect(RLP.bytes([]).encoded.hex == "80")53 #expect(RLP.list([]).encoded.hex == "c0")54 #expect(RLP.uint(UInt64(0)).encoded.hex == "80")55 #expect(RLP.uint(UInt64(15)).encoded.hex == "0f")56 #expect(RLP.uint(UInt64(1024)).encoded.hex == "820400")57 // [ [], [[]], [ [], [[]] ] ]58 #expect(RLP.list([.list([]), .list([.list([])]), .list([.list([]), .list([.list([])])])]).encoded.hex == "c7c0c1c0c3c0c1c0")59 let long = Bytes("Lorem ipsum dolor sit amet, consectetur adipisicing elit".utf8)60 #expect(RLP.bytes(long).encoded.hex.hasPrefix("b838"))61 for item in [RLP.bytes(long), .list([.bytes(long), .uint(UInt64(7)), .list([])]), .bytes(Bytes(repeating: 1, count: 300))] {62 #expect(try RLP.decode(item.encoded) == item)63 }64 #expect(throws: RLP.Error.nonCanonical) { try RLP.decode([0x81, 0x05]) }65 #expect(throws: RLP.Error.nonCanonical) { try RLP.decode([0xb8, 0x05, 1, 2, 3, 4, 5]) }66 #expect(throws: RLP.Error.trailingBytes) { try RLP.decode([0x80, 0x80]) }67 #expect(throws: RLP.Error.truncated) { try RLP.decode([0x83, 0x64]) }68 #expect(throws: RLP.Error.truncated) { try RLP.decode([0xc2, 0x83]) }69 }7071 @Test func eip55() throws {72 for a in ["0x5aAeb6053F3E94C9b9A09f33669435E7Ef1BeAed", "0xfB6916095ca1df60bB79Ce92cE3Ea74c37c5d359",73 "0xdbF03B407c01E7cD3CBea99509d93f8DDDC8C6FB", "0xD1220A0cf47c7B9Be7A2E6BA89F429762e7b9aDb"] {74 #expect(EthereumAddress(a)?.checksummed == a)75 #expect(EthereumAddress(a.lowercased())?.checksummed == a)76 }77 #expect(EthereumAddress("0x5aAeb6053F3E94C9b9A09f33669435E7Ef1BeAeD") == nil)78 #expect(EthereumAddress("5aAeb6053F3E94C9b9A09f33669435E7Ef1BeAed") == nil)79 #expect(EthereumAddress("0x5aAeb6053F3E94C9b9A09f33669435E7Ef1BeAe") == nil)80 // private key 181 let pk = try Secp256k1.publicKey(privateKey: Bytes(repeating: 0, count: 31) + [1])82 #expect(try EthereumAddress(publicKey: pk).checksummed == "0x7E5F4552091A69125d5DfCb7b8C2659029395Bdf")83 }8485 @Test func hdAccountMatchesKnownWallets() throws {86 // "test test … junk" (the Hardhat / Anvil default sentence), first account87 let words = Array(repeating: "test", count: 11) + ["junk"]88 let key = try ExtendedKey.master(seed: try BIP39.seed(mnemonic: words)).derive(StandardPaths.ethereum())89 #expect(key.privateKey?.hex == "ac0974bec39a17e36ba4a6b4d238ff944bacb478cbed5efcae784d7bf4f2ff80")90 #expect(try EthereumAddress(publicKey: key.publicKey).checksummed == "0xf39Fd6e51aad88F6F4ce6aB8827279cffFb92266")91 }9293 @Test func eip1559SignAndDecode() throws {94 let key = Bytes(hex: "ac0974bec39a17e36ba4a6b4d238ff944bacb478cbed5efcae784d7bf4f2ff80")!95 let to = EthereumAddress("0x70997970C51812dc3A010C7d01b50e0d17dc79C8")!96 let tx = EIP1559Transaction(chainID: 11_155_111, nonce: 3, maxPriorityFeePerGas: UInt256(1_500_000_000),97 maxFeePerGas: UInt256(30_000_000_000), gasLimit: 21_000, to: to,98 value: UInt256(decimal: "1000000000000000000")!)99 let signed = try tx.sign(privateKey: key)100 #expect(signed.raw.first == 0x02)101 #expect(try signed.sender().checksummed == "0xf39Fd6e51aad88F6F4ce6aB8827279cffFb92266")102 let decoded = try SignedEIP1559Transaction.decode(signed.raw)103 #expect(decoded == signed && decoded.transaction == tx)104 #expect(try EIP1559Transaction.decodeUnsigned(tx.unsignedPayload) == tx)105 #expect(tx.maximumCost?.description == "1000630000000000000")106 var tampered = signed.raw107 tampered[tampered.count - 40] ^= 1108 let other = try? SignedEIP1559Transaction.decode(tampered)109 #expect(other == nil || (try? other!.sender()) != (try? signed.sender()))110 var bad = tx111 bad.maxPriorityFeePerGas = UInt256(40_000_000_000)112 #expect(throws: EIP1559Transaction.Error.feeOrder) { try bad.sign(privateKey: key) }113 }114115 @Test func erc20Encoding() {116 #expect(ERC20.transferSelector.hex == "a9059cbb")117 #expect(ERC20.approveSelector.hex == "095ea7b3")118 #expect(ERC20.balanceOfSelector.hex == "70a08231")119 #expect(ERC20.allowanceSelector.hex == "dd62ed3e")120 #expect(ERC20.transferTopic.hex == "ddf252ad1be2c89b69c2b068fc378daa952ba7f163c4a11628f55a4df523b3ef")121 let to = EthereumAddress("0x70997970C51812dc3A010C7d01b50e0d17dc79C8")!122 let data = ERC20.transfer(to: to, amount: UInt256(1_000_000))123 #expect(data.hex == "a9059cbb00000000000000000000000070997970c51812dc3a010c7d01b50e0d17dc79c800000000000000000000000000000000000000000000000000000000000f4240")124 #expect(ERC20.decodeCall(data) == .transfer(to: to, amount: UInt256(1_000_000)))125 #expect(ERC20.decodeCall(ERC20.approve(spender: to, amount: .max)) == .approve(spender: to, amount: .max))126 var dirty = data127 dirty[5] = 1128 #expect(ERC20.decodeCall(dirty) == nil)129 #expect(ERC20.decodeCall(Bytes(data.dropLast())) == nil)130 #expect(ERC20.isUnlimited(.max) && !ERC20.isUnlimited(UInt256(1)))131 let abiString = Bytes(hex: "0000000000000000000000000000000000000000000000000000000000000020" + "0000000000000000000000000000000000000000000000000000000000000004" + "5553444300000000000000000000000000000000000000000000000000000000")!132 #expect(ERC20.decodeString(abiString) == "USDC")133 #expect(ERC20.decodeString(Bytes(hex: "4d4b520000000000000000000000000000000000000000000000000000000000")!) == "MKR")134 #expect(BuiltInTokens.mainnet.count == 24)135 }136137 @Test func personalSign() throws {138 let key = Bytes(hex: "ac0974bec39a17e36ba4a6b4d238ff944bacb478cbed5efcae784d7bf4f2ff80")!139 let sig = try PersonalMessage.sign(Bytes("hello".utf8), privateKey: key)140 #expect(try PersonalMessage.recoverSigner(Bytes("hello".utf8), signature: sig).checksummed == "0xf39Fd6e51aad88F6F4ce6aB8827279cffFb92266")141 }142}143