Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Remove ethers #1616

Merged
merged 2 commits into from
Dec 30, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 10 additions & 8 deletions packages/aws-kms-adapter/src/KMSVeChainSigner.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -388,20 +389,21 @@ class KMSVeChainSigner extends VeChainAbstractSigner {
*/
public async signTypedData(
domain: TypedDataDomain,
types: Record<string, TypedDataField[]>,
value: Record<string, unknown>
types: Record<string, TypedDataParameter[]>,
primaryType: string,
message: Record<string, unknown>
): Promise<string> {
try {
const payload = Hex.of(
TypedDataEncoder.hash(domain, types, value)
hashTypedData({ domain, types, primaryType, message })
).bytes;

return await this.signPayload(payload);
} catch (error) {
throw new SignerMethodError(
'KMSVeChainSigner.signTypedData',
'The typed data could not be signed.',
{ domain, types, value },
{ domain, types, primaryType, message },
error
);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -299,6 +299,7 @@ describe('KMSVeChainSigner - Thor Solo', () => {
const signature = await signer.signTypedData(
typedData.domain,
typedData.types,
typedData.primaryType,
typedData.data
);
expect(signature).toBeDefined();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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', () => ({
Expand Down Expand Up @@ -136,6 +136,7 @@ describe('KMSVeChainSigner', () => {
}
]
},
'Mail',
{
from: {
name: 'Cow',
Expand All @@ -159,7 +160,8 @@ describe('KMSVeChainSigner', () => {
await expect(
signer.signTypedData(
{} as unknown as TypedDataDomain,
{} as unknown as Record<string, TypedDataField[]>,
{} as unknown as Record<string, TypedDataParameter[]>,
'primaryType',
{} as unknown as Record<string, unknown>
)
).rejects.toThrow(SignerMethodError);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand Down Expand Up @@ -61,7 +61,7 @@ const ethSignTypedDataV4 = async (
{
primaryType: string;
domain: TypedDataDomain;
types: Record<string, TypedDataField[]>;
types: Record<string, TypedDataParameter[]>;
message: Record<string, unknown>;
}
];
Expand All @@ -74,6 +74,7 @@ const ethSignTypedDataV4 = async (
return await signer.signTypedData(
typedData.domain,
typedData.types,
typedData.primaryType,
typedData.message
);
} catch (error) {
Expand Down
10 changes: 6 additions & 4 deletions packages/network/src/signer/signers/types.d.ts
Original file line number Diff line number Diff line change
@@ -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
Expand Down Expand Up @@ -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;

/**
Expand Down Expand Up @@ -368,9 +369,10 @@ interface VeChainSigner {
* Signs the [[link-eip-712]] typed data.
*/
signTypedData: (
domain: vechain_sdk_core_ethers.TypedDataDomain,
types: Record<string, vechain_sdk_core_ethers.TypedDataField[]>,
value: Record<string, unknown>,
domain: TypedDataDomain,
types: Record<string, TypedDataParameter[]>,
primaryType: string,
message: Record<string, unknown>,
options?: SignTypedDataOptions
) => Promise<string>;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand Down Expand Up @@ -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<string, TypedDataField[]>} types - The types used for signing.
* @param {Record<string, unknown>} value - The message data to be signed.
* @param {Record<string, TypedDataParameter[]>} types - The types used for signing.
* @param {string} primaryType - The primary type used for signing.
* @param {Record<string, unknown>} message - The message data to be signed.
*
* @return {Promise<string>} - A promise that resolves with the signature string.
*/
abstract signTypedData(
domain: TypedDataDomain,
types: Record<string, TypedDataField[]>,
value: Record<string, unknown>,
types: Record<string, TypedDataParameter[]>,
primaryType: string,
message: Record<string, unknown>,
options?: SignTypedDataOptions
): Promise<string>;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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<string, TypedDataField[]>} types - The types used for signing.
* @param {Record<string, unknown>} value - The value data to be signed.
* @param {Record<string, TypedDataParameter[]>} types - The types used for signing.
* @param {string} primaryType - The primary type used for signing.
* @param {Record<string, unknown>} message - The value data to be signed.
*
* @return {Promise<string>} - A promise that resolves with the signature string.
*/
async signTypedData(
domain: TypedDataDomain,
types: Record<string, TypedDataField[]>,
value: Record<string, unknown>
types: Record<string, TypedDataParameter[]>,
primaryType: string,
message: Record<string, unknown>
): Promise<string> {
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,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ import {
populateCallTestCases,
populateCallTestCasesAccount
} from './fixture';
import { InvalidAbiEncodingTypeError } from 'viem';

/**
* VeChain base signer tests
Expand Down Expand Up @@ -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 () => {
Expand All @@ -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);
Expand All @@ -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');
Expand All @@ -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);
Expand Down
Loading