From 860182526de544a1ab50ac7e141921c08ee7699a Mon Sep 17 00:00:00 2001 From: Fabio Rigamonti <73019897+fabiorigam@users.noreply.github.com> Date: Tue, 24 Dec 2024 12:54:32 +0100 Subject: [PATCH] fix: remove ethers --- .../aws-kms-adapter/src/KMSVeChainSigner.ts | 18 ++++++++++-------- .../tests/KMSVeChainSigner.solo.test.ts | 1 + .../tests/KMSVeChainSigner.unit.test.ts | 6 ++++-- .../eth_signTypedData_v4.ts | 5 +++-- packages/network/src/signer/signers/types.d.ts | 10 ++++++---- .../vechain-abstract-signer.ts | 12 +++++++----- .../vechain-private-key-signer.ts | 18 ++++++++++-------- .../vechain-private-key-signer.unit.test.ts | 7 ++++++- 8 files changed, 47 insertions(+), 30 deletions(-) diff --git a/packages/aws-kms-adapter/src/KMSVeChainSigner.ts b/packages/aws-kms-adapter/src/KMSVeChainSigner.ts index 1550009a0..84904c663 100644 --- a/packages/aws-kms-adapter/src/KMSVeChainSigner.ts +++ b/packages/aws-kms-adapter/src/KMSVeChainSigner.ts @@ -12,11 +12,12 @@ import { } from '@vechain/sdk-network'; import { BitString, ObjectIdentifier, Sequence, verifySchema } from 'asn1js'; import { + hashTypedData, + recoverPublicKey, + toHex, type TypedDataDomain, - TypedDataEncoder, - type TypedDataField -} from 'ethers'; -import { recoverPublicKey, toHex } from 'viem'; + type TypedDataParameter +} from 'viem'; import { KMSVeChainProvider } from './KMSVeChainProvider'; class KMSVeChainSigner extends VeChainAbstractSigner { @@ -388,12 +389,13 @@ class KMSVeChainSigner extends VeChainAbstractSigner { */ public async signTypedData( domain: TypedDataDomain, - types: Record, - value: Record + types: Record, + primaryType: string, + message: Record ): Promise { try { const payload = Hex.of( - TypedDataEncoder.hash(domain, types, value) + hashTypedData({ domain, types, primaryType, message }) ).bytes; return await this.signPayload(payload); @@ -401,7 +403,7 @@ class KMSVeChainSigner extends VeChainAbstractSigner { throw new SignerMethodError( 'KMSVeChainSigner.signTypedData', 'The typed data could not be signed.', - { domain, types, value }, + { domain, types, primaryType, message }, error ); } diff --git a/packages/aws-kms-adapter/tests/KMSVeChainSigner.solo.test.ts b/packages/aws-kms-adapter/tests/KMSVeChainSigner.solo.test.ts index 6833f896c..6ad998f5b 100644 --- a/packages/aws-kms-adapter/tests/KMSVeChainSigner.solo.test.ts +++ b/packages/aws-kms-adapter/tests/KMSVeChainSigner.solo.test.ts @@ -299,6 +299,7 @@ describe('KMSVeChainSigner - Thor Solo', () => { const signature = await signer.signTypedData( typedData.domain, typedData.types, + typedData.primaryType, typedData.data ); expect(signature).toBeDefined(); diff --git a/packages/aws-kms-adapter/tests/KMSVeChainSigner.unit.test.ts b/packages/aws-kms-adapter/tests/KMSVeChainSigner.unit.test.ts index d4f8ce2da..90b76fa91 100644 --- a/packages/aws-kms-adapter/tests/KMSVeChainSigner.unit.test.ts +++ b/packages/aws-kms-adapter/tests/KMSVeChainSigner.unit.test.ts @@ -5,7 +5,7 @@ import { type ThorClient, type TransactionRequestInput } from '@vechain/sdk-network'; -import { type TypedDataDomain, type TypedDataField } from 'ethers'; +import { type TypedDataDomain, type TypedDataParameter } from 'viem'; import { KMSVeChainProvider, KMSVeChainSigner } from '../src'; import { EIP712_CONTRACT, EIP712_FROM, EIP712_TO } from './fixture'; jest.mock('asn1js', () => ({ @@ -136,6 +136,7 @@ describe('KMSVeChainSigner', () => { } ] }, + 'Mail', { from: { name: 'Cow', @@ -159,7 +160,8 @@ describe('KMSVeChainSigner', () => { await expect( signer.signTypedData( {} as unknown as TypedDataDomain, - {} as unknown as Record, + {} as unknown as Record, + 'primaryType', {} as unknown as Record ) ).rejects.toThrow(SignerMethodError); diff --git a/packages/network/src/provider/utils/rpc-mapper/methods/eth_signTypedData_v4/eth_signTypedData_v4.ts b/packages/network/src/provider/utils/rpc-mapper/methods/eth_signTypedData_v4/eth_signTypedData_v4.ts index 2b0803d27..b8ff51365 100644 --- a/packages/network/src/provider/utils/rpc-mapper/methods/eth_signTypedData_v4/eth_signTypedData_v4.ts +++ b/packages/network/src/provider/utils/rpc-mapper/methods/eth_signTypedData_v4/eth_signTypedData_v4.ts @@ -4,7 +4,7 @@ import { JSONRPCInvalidParams, stringifyData } from '@vechain/sdk-errors'; -import type { TypedDataDomain, TypedDataField } from 'ethers'; +import type { TypedDataDomain, TypedDataParameter } from 'viem'; import type { VeChainSigner } from '../../../../../signer/signers'; import type { ThorClient } from '../../../../../thor-client'; import type { VeChainProvider } from '../../../../providers/vechain-provider'; @@ -61,7 +61,7 @@ const ethSignTypedDataV4 = async ( { primaryType: string; domain: TypedDataDomain; - types: Record; + types: Record; message: Record; } ]; @@ -74,6 +74,7 @@ const ethSignTypedDataV4 = async ( return await signer.signTypedData( typedData.domain, typedData.types, + typedData.primaryType, typedData.message ); } catch (error) { diff --git a/packages/network/src/signer/signers/types.d.ts b/packages/network/src/signer/signers/types.d.ts index 75ada8550..eac69d4a3 100644 --- a/packages/network/src/signer/signers/types.d.ts +++ b/packages/network/src/signer/signers/types.d.ts @@ -1,5 +1,5 @@ import { type TransactionClause } from '@vechain/sdk-core'; -import { type AccessListish } from 'ethers'; +import { type TypedDataDomain, type TypedDataParameter } from 'viem'; import { type HardhatVeChainProvider, type VeChainProvider @@ -181,6 +181,7 @@ interface TransactionRequestInput { * list are //warmed// by preloading them, so their initial cost to * fetch is guaranteed, but then each additional access is cheaper. */ + // eslint-disable-next-line @typescript-eslint/no-redundant-type-constituents accessList?: null | AccessListish; /** @@ -368,9 +369,10 @@ interface VeChainSigner { * Signs the [[link-eip-712]] typed data. */ signTypedData: ( - domain: vechain_sdk_core_ethers.TypedDataDomain, - types: Record, - value: Record, + domain: TypedDataDomain, + types: Record, + primaryType: string, + message: Record, options?: SignTypedDataOptions ) => Promise; diff --git a/packages/network/src/signer/signers/vechain-abstract-signer/vechain-abstract-signer.ts b/packages/network/src/signer/signers/vechain-abstract-signer/vechain-abstract-signer.ts index 1ed0afe76..60a869861 100644 --- a/packages/network/src/signer/signers/vechain-abstract-signer/vechain-abstract-signer.ts +++ b/packages/network/src/signer/signers/vechain-abstract-signer/vechain-abstract-signer.ts @@ -8,7 +8,7 @@ import { type TransactionClause } from '@vechain/sdk-core'; import { InvalidDataType, JSONRPCInvalidParams } from '@vechain/sdk-errors'; -import { type TypedDataDomain, type TypedDataField } from 'ethers'; +import { type TypedDataDomain, type TypedDataParameter } from 'viem'; import { RPC_METHODS } from '../../../provider/utils/const/rpc-mapper/rpc-methods'; import { type TransactionSimulationResult } from '../../../thor-client'; import { vnsUtils } from '../../../utils'; @@ -336,15 +336,17 @@ abstract class VeChainAbstractSigner implements VeChainSigner { * Signs the [[link-eip-712]] typed data. * * @param {TypedDataDomain} domain - The domain parameters used for signing. - * @param {Record} types - The types used for signing. - * @param {Record} value - The message data to be signed. + * @param {Record} types - The types used for signing. + * @param {string} primaryType - The primary type used for signing. + * @param {Record} message - The message data to be signed. * * @return {Promise} - A promise that resolves with the signature string. */ abstract signTypedData( domain: TypedDataDomain, - types: Record, - value: Record, + types: Record, + primaryType: string, + message: Record, options?: SignTypedDataOptions ): Promise; diff --git a/packages/network/src/signer/signers/vechain-private-key-signer/vechain-private-key-signer.ts b/packages/network/src/signer/signers/vechain-private-key-signer/vechain-private-key-signer.ts index efd4bbba7..069f53cee 100644 --- a/packages/network/src/signer/signers/vechain-private-key-signer/vechain-private-key-signer.ts +++ b/packages/network/src/signer/signers/vechain-private-key-signer/vechain-private-key-signer.ts @@ -15,10 +15,10 @@ import { stringifyData } from '@vechain/sdk-errors'; import { + hashTypedData, type TypedDataDomain, - TypedDataEncoder, - type TypedDataField -} from 'ethers'; + type TypedDataParameter +} from 'viem'; import { RPC_METHODS } from '../../../provider/utils/const/rpc-mapper/rpc-methods'; import { DelegationHandler, @@ -207,20 +207,22 @@ class VeChainPrivateKeySigner extends VeChainAbstractSigner { * albeit Ethereum Name Services are not resolved because he resolution depends on **ethers** provider implementation. * * @param {TypedDataDomain} domain - The domain parameters used for signing. - * @param {Record} types - The types used for signing. - * @param {Record} value - The value data to be signed. + * @param {Record} types - The types used for signing. + * @param {string} primaryType - The primary type used for signing. + * @param {Record} message - The value data to be signed. * * @return {Promise} - A promise that resolves with the signature string. */ async signTypedData( domain: TypedDataDomain, - types: Record, - value: Record + types: Record, + primaryType: string, + message: Record ): Promise { return await new Promise((resolve, reject) => { try { const hash = Hex.of( - TypedDataEncoder.hash(domain, types, value) + hashTypedData({ domain, types, primaryType, message }) ).bytes; const sign = Secp256k1.sign( hash, diff --git a/packages/network/tests/signer/signers/vechain-private-key-signer/vechain-private-key-signer.unit.test.ts b/packages/network/tests/signer/signers/vechain-private-key-signer/vechain-private-key-signer.unit.test.ts index 65f8a65a2..922ab973e 100644 --- a/packages/network/tests/signer/signers/vechain-private-key-signer/vechain-private-key-signer.unit.test.ts +++ b/packages/network/tests/signer/signers/vechain-private-key-signer/vechain-private-key-signer.unit.test.ts @@ -22,6 +22,7 @@ import { populateCallTestCases, populateCallTestCasesAccount } from './fixture'; +import { InvalidAbiEncodingTypeError } from 'viem'; /** * VeChain base signer tests @@ -290,9 +291,10 @@ describe('VeChain base signer tests', () => { signer.signTypedData( eip712TestCases.invalid.domain, eip712TestCases.invalid.types, + eip712TestCases.invalid.primaryType, eip712TestCases.invalid.data ) - ).rejects.toThrowError(TypeError); + ).rejects.toThrowError(InvalidAbiEncodingTypeError); }); test('signTypedData - exception when parsing to hex', async () => { @@ -313,6 +315,7 @@ describe('VeChain base signer tests', () => { signer.signTypedData( eip712TestCases.valid.domain, eip712TestCases.valid.types, + eip712TestCases.valid.primaryType, eip712TestCases.valid.data ) ).rejects.toThrowError(expectedErrorString); @@ -321,6 +324,7 @@ describe('VeChain base signer tests', () => { signer.signTypedData( eip712TestCases.valid.domain, eip712TestCases.valid.types, + eip712TestCases.valid.primaryType, eip712TestCases.valid.data ) ).rejects.toThrowError('Error while signing typed data'); @@ -341,6 +345,7 @@ describe('VeChain base signer tests', () => { ).signTypedData( eip712TestCases.valid.domain, eip712TestCases.valid.types, + eip712TestCases.valid.primaryType, eip712TestCases.valid.data ); expect(actual).toBe(expected);