|
| 1 | +import * as assert from "assert"; |
| 2 | +import { ethers } from "ethers"; |
| 3 | +import { generateRandomEthersWallet } from "../src/utils"; |
| 4 | +import { IADDRESS_MAPPING_ADDRESS, IAddressMappingABI } from "../src/contracts/addressMapping"; |
| 5 | +import { convertH160ToPublicKey } from "../src/address-utils"; |
| 6 | +import { u8aToHex } from "@polkadot/util"; |
| 7 | + |
| 8 | +describe("Test address mapping precompile", () => { |
| 9 | + const wallet1 = generateRandomEthersWallet(); |
| 10 | + const wallet2 = generateRandomEthersWallet(); |
| 11 | + |
| 12 | + it("Address mapping converts H160 to AccountId32 correctly", async () => { |
| 13 | + const contract = new ethers.Contract( |
| 14 | + IADDRESS_MAPPING_ADDRESS, |
| 15 | + IAddressMappingABI, |
| 16 | + wallet1 |
| 17 | + ); |
| 18 | + |
| 19 | + // Test with wallet1's address |
| 20 | + const evmAddress = wallet1.address; |
| 21 | + const accountId32 = await contract.addressMapping(evmAddress); |
| 22 | + const expectedAcccountId32 = convertH160ToPublicKey(evmAddress); |
| 23 | + |
| 24 | + // Verify the result is a valid bytes32 (32 bytes) |
| 25 | + assert.ok(accountId32.length === 66, "AccountId32 should be 32 bytes (66 hex chars with 0x)"); |
| 26 | + assert.ok(accountId32.startsWith("0x"), "AccountId32 should start with 0x"); |
| 27 | + |
| 28 | + // Verify it's not all zeros |
| 29 | + assert.notEqual( |
| 30 | + accountId32, |
| 31 | + "0x0000000000000000000000000000000000000000000000000000000000000000", |
| 32 | + "AccountId32 should not be all zeros" |
| 33 | + ); |
| 34 | + |
| 35 | + console.log("accountId32: {}", accountId32); |
| 36 | + console.log("expectedAcccountId32: {}", expectedAcccountId32); |
| 37 | + |
| 38 | + assert.equal(accountId32, u8aToHex(expectedAcccountId32), "AccountId32 should be the same as the expected AccountId32"); |
| 39 | + }); |
| 40 | + |
| 41 | + it("Address mapping works with different addresses", async () => { |
| 42 | + const contract = new ethers.Contract( |
| 43 | + IADDRESS_MAPPING_ADDRESS, |
| 44 | + IAddressMappingABI, |
| 45 | + wallet1 |
| 46 | + ); |
| 47 | + |
| 48 | + // Test with wallet2's address |
| 49 | + const evmAddress1 = wallet1.address; |
| 50 | + const evmAddress2 = wallet2.address; |
| 51 | + |
| 52 | + const accountId1 = await contract.addressMapping(evmAddress1); |
| 53 | + const accountId2 = await contract.addressMapping(evmAddress2); |
| 54 | + |
| 55 | + // Different addresses should map to different AccountIds |
| 56 | + assert.notEqual( |
| 57 | + accountId1, |
| 58 | + accountId2, |
| 59 | + "Different EVM addresses should map to different AccountIds" |
| 60 | + ); |
| 61 | + |
| 62 | + // Both should be valid bytes32 |
| 63 | + assert.ok(accountId1.length === 66, "AccountId1 should be 32 bytes"); |
| 64 | + assert.ok(accountId2.length === 66, "AccountId2 should be 32 bytes"); |
| 65 | + }); |
| 66 | + |
| 67 | + it("Address mapping is deterministic", async () => { |
| 68 | + const contract = new ethers.Contract( |
| 69 | + IADDRESS_MAPPING_ADDRESS, |
| 70 | + IAddressMappingABI, |
| 71 | + wallet1 |
| 72 | + ); |
| 73 | + |
| 74 | + const evmAddress = wallet1.address; |
| 75 | + |
| 76 | + // Call multiple times with the same address |
| 77 | + const accountId1 = await contract.addressMapping(evmAddress); |
| 78 | + const accountId2 = await contract.addressMapping(evmAddress); |
| 79 | + |
| 80 | + // All calls should return the same result |
| 81 | + assert.equal( |
| 82 | + accountId1, |
| 83 | + accountId2, |
| 84 | + "First and second calls should return the same AccountId" |
| 85 | + ); |
| 86 | + }); |
| 87 | +}); |
0 commit comments