import { assert, expect } from 'chai'; import { TypedDataField, Wallet } from 'ethers'; import { signAuthenticationTypedData, authenticationReqTypes, getAuthenticationReqValue, ecdsaSignatureToTsPrivKey, getTypedDataDomain, getTsRollupSignerFromWallet, } from '../src'; import { signTypedDataAccountTestCaseData } from './data/signTypedData-account.testcase'; describe(`signTypedData test`, function () { const { chainId, verifyingContract } = signTypedDataAccountTestCaseData['expectedDomain']; const accounts = signTypedDataAccountTestCaseData['accounts']; it('getTypedDataDomain', function () { const domain = getTypedDataDomain(chainId, verifyingContract); expect(domain).deep.equal(signTypedDataAccountTestCaseData['expectedDomain']); }); it('getAuthenticationReqValue', function () { const { expectTypeDataValue } = signTypedDataAccountTestCaseData['getAuthenticationReqValue']; const inputTypeDataValue = getAuthenticationReqValue(); expectEIP712TypeDataMatchField(authenticationReqTypes, inputTypeDataValue, expectTypeDataValue); }); accounts.forEach(({ ethereumPrivKey, expectedSignature, expectedEddsaPrivKey, expectedTsPubKey }, index) => { const ethereumSigner = new Wallet(ethereumPrivKey); it(`signAuthenticationTypedData, index=${index}`, async function () { const signature = await signAuthenticationTypedData(chainId, verifyingContract, ethereumSigner); expectHexStringAreSame(signature, expectedSignature); }); it('ecdsaSignatureToTsPrivKey', async function () { const eddsaPrivKey = ecdsaSignatureToTsPrivKey(expectedSignature); expectHexStringAreSame(eddsaPrivKey, expectedEddsaPrivKey); }); it('getTsRollupSignerFromWallet', async function () { const tsSigner = await getTsRollupSignerFromWallet(chainId, verifyingContract, ethereumSigner); expect(tsSigner.tsPubKey.map((v) => '0x' + v.toString(16))).deep.equal(expectedTsPubKey); }); }); }); function expectHexStringAreSame(inputTypeDataValue: string, expectedSignature: string) { assert(inputTypeDataValue.startsWith('0x'), 'inputTypeDataValue should start with 0x'); assert(expectedSignature.startsWith('0x'), 'expectedSignature should start with 0x'); expect(inputTypeDataValue.toLowerCase()).equal(expectedSignature.toLowerCase()); } function expectEIP712TypeDataMatchField(typedDataField: Record, inputTypeDataValue: any, expectTypeDataValue: any) { const mainField = typedDataField.Main; Object.values(mainField).forEach(({ name, type }) => { expect(typeof inputTypeDataValue[name]).equal(type); expect(inputTypeDataValue[name]).equal(expectTypeDataValue[name]); }); }