Skip to content
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
2 changes: 1 addition & 1 deletion packages/snap/snap.manifest.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
"url": "https://github.com/MetaMask/snap-solana-wallet.git"
},
"source": {
"shasum": "NfjYYpYE9M/qdyVE1zrmo40xozAuPrqS2ayUgh3eS1M=",
"shasum": "ssZ6XwPzUpBCXstPx6ko5dEVFVlcF6aqcaO9LhE4gj8=",
"location": {
"npm": {
"filePath": "dist/bundle.js",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import { SolMethod } from '@metamask/keyring-api';
import type { CaipAssetType, JsonRpcRequest } from '@metamask/snaps-sdk';
import { signature } from '@solana/kit';

import { asStrictKeyringAccount } from '../../../entities';
import { KnownCaip19Id, Network } from '../../constants/solana';
import type { AssetsService } from '../../services/assets/AssetsService';
import type { ConfirmationHandler } from '../../services/confirmation/ConfirmationHandler';
Expand Down Expand Up @@ -34,7 +35,7 @@ import {
import { getBip32EntropyMock } from '../../test/mocks/utils/getBip32Entropy';
import { getBip32Entropy } from '../../utils/getBip32Entropy';
import logger from '../../utils/logger';
import { asStrictKeyringAccount, SolanaKeyring } from './Keyring';
import { SolanaKeyring } from './Keyring';

jest.mock('@metamask/keyring-snap-sdk', () => ({
...jest.requireActual('@metamask/keyring-snap-sdk'),
Expand Down
35 changes: 5 additions & 30 deletions packages/snap/src/core/handlers/onKeyringRequest/Keyring.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
/* eslint-disable prettier/prettier */
/* eslint-disable @typescript-eslint/restrict-plus-operands */
/* eslint-disable @typescript-eslint/prefer-reduce-type-parameter */
import type {
Expand Down Expand Up @@ -33,6 +34,10 @@ import type { Signature } from '@solana/kit';
import { address as asAddress, getAddressDecoder } from '@solana/kit';
import { sortBy } from 'lodash';

import {
asStrictKeyringAccount,
type SolanaKeyringAccount,
} from '../../../entities';
import { type Network } from '../../constants/solana';
import type { AssetsService } from '../../services/assets/AssetsService';
import type { ConfirmationHandler } from '../../services/confirmation/ConfirmationHandler';
Expand Down Expand Up @@ -60,36 +65,6 @@ import {
SolanaKeyringRequestStruct,
} from './structs';

/**
* We need to store the index of the KeyringAccount in the state because
* we want to be able to restore any account with a previously used index.
*/
export type SolanaKeyringAccount = {
entropySource: EntropySourceId;
derivationPath: `m/${string}`;
index: number;
} & KeyringAccount;

/**
* Converts a Solana keyring account to its stricter form (required by the Keyring API).
*
* @param account - A Solana keyring account.
* @returns A strict keyring account (with no additional fields).
*/
export function asStrictKeyringAccount(
account: SolanaKeyringAccount,
): KeyringAccount {
const { type, id, address, options, methods, scopes } = account;
return {
type,
id,
address,
options,
methods,
scopes,
};
}

export class SolanaKeyring implements Keyring {
readonly #state: IStateManager<UnencryptedStateValue>;

Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import type { Transaction } from '@metamask/keyring-api';
import { assert } from '@metamask/superstruct';

import type { SolanaKeyringAccount } from '../../../entities';
import type { Network } from '../../constants/solana';
import type { SolanaKeyringAccount } from '../../handlers/onKeyringRequest/Keyring';
import logger from '../../utils/logger';
import { Base64Struct } from '../../validation/structs';

Expand Down
2 changes: 1 addition & 1 deletion packages/snap/src/core/services/assets/AssetsService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,14 @@ import type { Address } from '@solana/kit';
import { address as asAddress } from '@solana/kit';
import { map, uniq } from 'lodash';

import type { SolanaKeyringAccount } from '../../../entities';
import type { ICache } from '../../caching/ICache';
import { useCache } from '../../caching/useCache';
import {
Network,
SolanaCaip19Tokens,
TOKEN_2022_PROGRAM_ADDRESS,
} from '../../constants/solana';
import type { SolanaKeyringAccount } from '../../handlers/onKeyringRequest/Keyring';
import type {
GetTokenAccountsByOwnerResponse,
TokenAccountInfoWithJsonData,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
import { SolMethod } from '@metamask/keyring-api';
import { assert, union } from '@metamask/superstruct';

import type { SolanaKeyringAccount } from '../../../entities';
import { render as renderConfirmSignIn } from '../../../features/confirmation/views/ConfirmSignIn/render';
import { render as renderConfirmSignMessage } from '../../../features/confirmation/views/ConfirmSignMessage/render';
import {
DEFAULT_CONFIRMATION_CONTEXT,
render as renderConfirmTransactionRequest,
} from '../../../features/confirmation/views/ConfirmTransactionRequest/render';
import { ScheduleBackgroundEventMethod } from '../../handlers/onCronjob/backgroundEvents/ScheduleBackgroundEventMethod';
import type { SolanaKeyringAccount } from '../../handlers/onKeyringRequest/Keyring';
import type { SolanaKeyringRequest } from '../../handlers/onKeyringRequest/structs';
import {
SolanaSignAndSendTransactionRequestStruct,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,8 @@ import {
type Blockhash,
} from '@solana/kit';

import type { SolanaKeyringAccount } from '../../../entities';
import type { Network } from '../../constants/solana';
import type { SolanaKeyringAccount } from '../../handlers/onKeyringRequest/Keyring';
import type { DecompileTransactionMessageFetchingLookupTablesConfig } from '../../sdk-extensions/codecs';
import {
fromBytesToCompilableTransactionMessage,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import type { CompilableTransactionMessage } from '@solana/kit';

import type { SolanaKeyringAccount } from '../../../../../entities';
import type { Network } from '../../../../constants/solana';
import type { SolanaKeyringAccount } from '../../../../handlers/onKeyringRequest/Keyring';

export type MockExecutionScenario = {
name: string;
Expand Down
2 changes: 1 addition & 1 deletion packages/snap/src/core/services/state/State.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@ import type { CaipAssetType } from '@metamask/utils';
import type { Address, Signature } from '@solana/kit';
import { unset } from 'lodash';

import type { SolanaKeyringAccount } from '../../../entities';
import type { SpotPrices } from '../../clients/price-api/types';
import type { SolanaKeyringAccount } from '../../handlers/onKeyringRequest/Keyring';
import { deserialize } from '../../serialization/deserialize';
import { serialize } from '../../serialization/serialize';
import type { Serializable } from '../../serialization/types';
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,8 @@ import {
type Signature,
} from '@solana/kit';

import type { SolanaKeyringAccount } from '../../../entities';
import { Network } from '../../constants/solana';
import type { SolanaKeyringAccount } from '../../handlers/onKeyringRequest/Keyring';
import type { ILogger } from '../../utils/logger';
import type { ConfigProvider } from '../config';
import type { SolanaConnection } from '../connection';
Expand Down Expand Up @@ -144,7 +144,7 @@ export class TransactionsService {

const next =
signatures.length === pagination.limit
? (signatures[signatures.length - 1] ?? null)
? (signatures[signatures.length - 1] ?? null) // eslint-disable-line prettier/prettier
: null;

return {
Expand Down
2 changes: 1 addition & 1 deletion packages/snap/src/core/services/wallet/WalletService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,9 @@ import {
verifySignature,
} from '@solana/kit';

import type { SolanaKeyringAccount } from '../../../entities';
import type { Caip10Address, Network } from '../../constants/solana';
import { ScheduleBackgroundEventMethod } from '../../handlers/onCronjob/backgroundEvents/ScheduleBackgroundEventMethod';
import type { SolanaKeyringAccount } from '../../handlers/onKeyringRequest/Keyring';
import type { DecompileTransactionMessageFetchingLookupTablesConfig } from '../../sdk-extensions/codecs';
import { fromTransactionToBase64String } from '../../sdk-extensions/codecs';
import { addressToCaip10 } from '../../utils/addressToCaip10';
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { mnemonicPhraseToBytes } from '@metamask/key-tree';
import { SolMethod, SolScope } from '@metamask/keyring-api';

import type { SolanaKeyringAccount } from '../../handlers/onKeyringRequest/Keyring';
import type { SolanaKeyringAccount } from '../../../entities';

export const MOCK_SEED_PHRASE =
'sugar interest animal afford dog imitate relief lizard width strategy embark midnight';
Expand Down
1 change: 1 addition & 0 deletions packages/snap/src/entities/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export * from './keyring-account';
31 changes: 31 additions & 0 deletions packages/snap/src/entities/keyring-account.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
import type { EntropySourceId, KeyringAccount } from '@metamask/keyring-api';

/**
* We need to store the index of the KeyringAccount in the state because
* we want to be able to restore any account with a previously used index.
*/
export type SolanaKeyringAccount = {
entropySource: EntropySourceId;
derivationPath: `m/${string}`;
index: number;
} & KeyringAccount;

/**
* Converts a Solana keyring account to its stricter form (required by the Keyring API).
*
* @param account - A Solana keyring account.
* @returns A strict keyring account (with no additional fields).
*/
export function asStrictKeyringAccount(
account: SolanaKeyringAccount,
): KeyringAccount {
const { type, id, address, options, methods, scopes } = account;
return {
type,
id,
address,
options,
methods,
scopes,
};
}
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,11 @@ import {
} from '@metamask/snaps-sdk/jsx';

import { Networks, type Network } from '../../../../core/constants/solana';
import type { SolanaKeyringAccount } from '../../../../core/handlers/onKeyringRequest/Keyring';
import { SOL_IMAGE_SVG } from '../../../../core/test/mocks/solana-image-svg';
import type { Preferences } from '../../../../core/types/snap';
import { addressToCaip10 } from '../../../../core/utils/addressToCaip10';
import { i18n } from '../../../../core/utils/i18n';
import type { SolanaKeyringAccount } from '../../../../entities';
import { BasicNullableField } from '../../components/BasicNullableField/BasicNullableField';
import { EstimatedChanges } from '../../components/EstimatedChanges/EstimatedChanges';
import { ConfirmSignInFormNames } from './events';
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import { assert } from '@metamask/superstruct';

import type { SolanaKeyringAccount } from '../../../../core/handlers/onKeyringRequest/Keyring';
import type { SolanaKeyringRequest } from '../../../../core/handlers/onKeyringRequest/structs';
import { SolanaSignInRequestStruct } from '../../../../core/services/wallet/structs';
import { SOL_IMAGE_SVG } from '../../../../core/test/mocks/solana-image-svg';
Expand All @@ -9,6 +8,7 @@ import {
getPreferences,
showDialog,
} from '../../../../core/utils/interface';
import type { SolanaKeyringAccount } from '../../../../entities';
import type { ConfirmSignInProps } from './ConfirmSignIn';
import { ConfirmSignIn } from './ConfirmSignIn';

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,11 @@ import {
} from '@metamask/snaps-sdk/jsx';

import { Networks, type Network } from '../../../../core/constants/solana';
import type { SolanaKeyringAccount } from '../../../../core/handlers/onKeyringRequest/Keyring';
import { SOL_IMAGE_SVG } from '../../../../core/test/mocks/solana-image-svg';
import { addressToCaip10 } from '../../../../core/utils/addressToCaip10';
import type { Locale } from '../../../../core/utils/i18n';
import { i18n } from '../../../../core/utils/i18n';
import type { SolanaKeyringAccount } from '../../../../entities';
import { ConfirmSignMessageFormNames } from './events';

export type ConfirmSignMessageProps = {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import { assert } from '@metamask/superstruct';
import { getBase64Codec, getUtf8Codec } from '@solana/kit';

import type { SolanaKeyringAccount } from '../../../../core/handlers/onKeyringRequest/Keyring';
import type { SolanaKeyringRequest } from '../../../../core/handlers/onKeyringRequest/structs';
import { SolanaSignMessageRequestStruct } from '../../../../core/services/wallet/structs';
import { SOL_IMAGE_SVG } from '../../../../core/test/mocks/solana-image-svg';
Expand All @@ -11,6 +10,7 @@ import {
getPreferences,
showDialog,
} from '../../../../core/utils/interface';
import type { SolanaKeyringAccount } from '../../../../entities';
import { ConfirmSignMessage } from './ConfirmSignMessage';

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@ import type { SolMethod } from '@metamask/keyring-api';

import type { SpotPrices } from '../../../../core/clients/price-api/types';
import type { Network } from '../../../../core/constants/solana';
import type { SolanaKeyringAccount } from '../../../../core/handlers/onKeyringRequest/Keyring';
import type { TransactionScanResult } from '../../../../core/services/transaction-scan/types';
import type { FetchStatus, Preferences } from '../../../../core/types/snap';
import type { SolanaKeyringAccount } from '../../../../entities';

export type SolanaInstruction = {
programId: string;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,14 +12,14 @@ import {
SolanaCaip19Tokens,
type Network,
} from '../../../../core/constants/solana';
import type { SolanaKeyringAccount } from '../../../../core/handlers/onKeyringRequest/Keyring';
import { addressToCaip10 } from '../../../../core/utils/addressToCaip10';
import { formatCrypto } from '../../../../core/utils/formatCrypto';
import { formatFiat } from '../../../../core/utils/formatFiat';
import type { Locale } from '../../../../core/utils/i18n';
import { i18n } from '../../../../core/utils/i18n';
import { tokenToFiat } from '../../../../core/utils/tokenToFiat';
import { truncateAddress } from '../../../../core/utils/truncateAddress';
import type { SolanaKeyringAccount } from '../../../../entities';

type AccountSelectorProps = {
name: string;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import type { Address, CompilableTransactionMessage } from '@solana/kit';
import type BigNumber from 'bignumber.js';

import type { Network } from '../../../core/constants/solana';
import type { SolanaKeyringAccount } from '../../../core/handlers/onKeyringRequest/Keyring';
import type { SolanaKeyringAccount } from '../../../entities';

export type BuildSendTransactionParams = {
from: SolanaKeyringAccount;
Expand Down
2 changes: 1 addition & 1 deletion packages/snap/src/features/send/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,10 @@ import type { Balance, CaipAssetType } from '@metamask/keyring-api';

import type { SpotPrices } from '../../core/clients/price-api/types';
import type { Network } from '../../core/constants/solana';
import type { SolanaKeyringAccount } from '../../core/handlers/onKeyringRequest/Keyring';
import type { FormFieldError } from '../../core/types/error';
import type { FetchStatus, Preferences } from '../../core/types/snap';
import type { LocalizedMessage } from '../../core/utils/i18n';
import type { SolanaKeyringAccount } from '../../entities';

export type SendFlowStage =
| 'send-form'
Expand Down
Loading