diff --git a/packages/keyring-api/CHANGELOG.md b/packages/keyring-api/CHANGELOG.md index 202f15635..33478471e 100644 --- a/packages/keyring-api/CHANGELOG.md +++ b/packages/keyring-api/CHANGELOG.md @@ -7,6 +7,10 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 ## [Unreleased] +### Added + +- Add XRPL account types and scopes (`XrpScope`, `XrpAccountType`, `XrpAccount`, etc.) ([#723](https://github.com/MetaMask/accounts/pull/723)) + ## [24.3.0] ### Added diff --git a/packages/keyring-api/src/api/account.ts b/packages/keyring-api/src/api/account.ts index 6c53a4ffa..aad697d5c 100644 --- a/packages/keyring-api/src/api/account.ts +++ b/packages/keyring-api/src/api/account.ts @@ -49,6 +49,19 @@ export enum XlmAccountType { Account = 'stellar:account', } +/** + * Supported XRPL account types. + * + * We currently model only the classic XRPL account (`r…` address). + * This excludes X-addresses (`X…`); use a classic address plus an optional + * destination tag instead. + * + * @see https://xrpl.org/docs/concepts/accounts/addresses + */ +export enum XrpAccountType { + Account = 'xrpl:account', +} + /** * A generic account type. It can be used to represent any account type that is * not covered by the other account types. It only applies to non-EVM chains. @@ -70,6 +83,7 @@ export type KeyringAccountType = | `${SolAccountType.DataAccount}` | `${TrxAccountType.Eoa}` | `${XlmAccountType.Account}` + | `${XrpAccountType.Account}` | `${AnyAccountType.Account}`; /** @@ -85,6 +99,7 @@ export const KeyringAccountTypeStruct = enums([ `${SolAccountType.DataAccount}`, `${TrxAccountType.Eoa}`, `${XlmAccountType.Account}`, + `${XrpAccountType.Account}`, `${AnyAccountType.Account}`, ]); @@ -114,6 +129,7 @@ export const KeyringAccountStruct = object({ `${SolAccountType.DataAccount}`, `${TrxAccountType.Eoa}`, `${XlmAccountType.Account}`, + `${XrpAccountType.Account}`, `${AnyAccountType.Account}`, ]), diff --git a/packages/keyring-api/src/index.ts b/packages/keyring-api/src/index.ts index 3f403407f..c791006cd 100644 --- a/packages/keyring-api/src/index.ts +++ b/packages/keyring-api/src/index.ts @@ -4,6 +4,8 @@ export * from './sol'; export * from './eth'; export * from './trx'; export * from './xlm'; +export * from './xrp'; export type * from './contexts'; + export * from './rpc'; export * from './events'; diff --git a/packages/keyring-api/src/xrp/constants.ts b/packages/keyring-api/src/xrp/constants.ts new file mode 100644 index 000000000..e932b9925 --- /dev/null +++ b/packages/keyring-api/src/xrp/constants.ts @@ -0,0 +1,10 @@ +// istanbul ignore file + +/** + * Scopes for XRPL account type. See {@link KeyringAccount.scopes}. + * + * @see https://namespaces.chainagnostic.org/xrpl/caip2 + */ +export enum XrpScope { + Mainnet = 'xrpl:0', +} diff --git a/packages/keyring-api/src/xrp/index.ts b/packages/keyring-api/src/xrp/index.ts new file mode 100644 index 000000000..4fbce9564 --- /dev/null +++ b/packages/keyring-api/src/xrp/index.ts @@ -0,0 +1,2 @@ +export * from './constants'; +export * from './types'; diff --git a/packages/keyring-api/src/xrp/types.test-d.ts b/packages/keyring-api/src/xrp/types.test-d.ts new file mode 100644 index 000000000..b51529050 --- /dev/null +++ b/packages/keyring-api/src/xrp/types.test-d.ts @@ -0,0 +1,8 @@ +import type { Extends } from '@metamask/keyring-utils'; +import { expectTrue } from '@metamask/keyring-utils'; + +import type { KeyringAccount } from '../api'; +import type { XrpAccount } from './types'; + +// `XrpAccount` extends `KeyringAccount` +expectTrue>(); diff --git a/packages/keyring-api/src/xrp/types.test.ts b/packages/keyring-api/src/xrp/types.test.ts new file mode 100644 index 000000000..5a437512a --- /dev/null +++ b/packages/keyring-api/src/xrp/types.test.ts @@ -0,0 +1,28 @@ +import { XrpAddressStruct } from './types'; + +describe('types', () => { + describe('XrpAddressStruct', () => { + it.each([ + 'rHb9CJAWyB4rj91VRWn96DkukG4bwdtyTh', + 'rPEPPER7kfTD9w2To4CQk6UCfuHM9c6GDY', + ])('is valid address: %s', (address) => { + expect(() => XrpAddressStruct.assert(address)).not.toThrow(); + }); + + it.each([ + // Empty or invalid input + '', + // Eth style address + '0x1234', + 'not-an-address', + // Too short (fewer than 25 chars total) + 'rHb9CJAWyB4rj91VRWn', + // X-address (not supported) + 'XVPcpSm47b1CZkf5AkKM9a84dQHe3m4sBhsrA4XtnBECTAc', + ])('rejects invalid address: %s', (address) => { + expect(() => XrpAddressStruct.assert(address)).toThrow( + `Expected a value of type \`XrpAddress\`, but received: \`"${address}"\``, + ); + }); + }); +}); diff --git a/packages/keyring-api/src/xrp/types.ts b/packages/keyring-api/src/xrp/types.ts new file mode 100644 index 000000000..3841d16c0 --- /dev/null +++ b/packages/keyring-api/src/xrp/types.ts @@ -0,0 +1,54 @@ +import type { Infer } from '@metamask/superstruct'; +import { array, enums, literal, nonempty, object } from '@metamask/superstruct'; +import { definePattern } from '@metamask/utils'; + +import { + CaipChainIdStruct, + KeyringAccountStruct, + XrpAccountType, +} from '../api'; + +/** + * XRPL classic account addresses are Base58Check-encoded strings that start + * with `r` and are typically 25–35 characters. X-addresses (`X…`) are not + * supported; callers should use a classic address plus optional destination + * tag. + * + * TODO: Add checksum validation. + * + * @see https://xrpl.org/docs/concepts/accounts/addresses + */ +export const XrpAddressStruct = definePattern( + 'XrpAddress', + /^r[1-9A-HJ-NP-Za-km-z]{24,34}$/u, +); + +/** + * Supported XRPL methods. + */ +export enum XrpMethod { + SignMessage = 'signMessage', + SignTransaction = 'signTransaction', +} + +export const XrpAccountStruct = object({ + ...KeyringAccountStruct.schema, + /** + * Account address. + */ + address: XrpAddressStruct, + /** + * Account type. + */ + type: literal(`${XrpAccountType.Account}`), + /** + * Account supported scopes (CAIP-2 chain IDs). + */ + scopes: nonempty(array(CaipChainIdStruct)), + /** + * Account supported methods. + */ + methods: array(enums(Object.values(XrpMethod))), +}); + +export type XrpAccount = Infer; diff --git a/packages/keyring-internal-api/CHANGELOG.md b/packages/keyring-internal-api/CHANGELOG.md index ea365babd..0defb976f 100644 --- a/packages/keyring-internal-api/CHANGELOG.md +++ b/packages/keyring-internal-api/CHANGELOG.md @@ -7,6 +7,10 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 ## [Unreleased] +### Added + +- Add internal XRPL account struct (`InternalXrpAccount`) ([#723](https://github.com/MetaMask/accounts/pull/723)) + ## [12.1.2] ### Changed diff --git a/packages/keyring-internal-api/src/types.test.ts b/packages/keyring-internal-api/src/types.test.ts index e37f381e4..ca36be35c 100644 --- a/packages/keyring-internal-api/src/types.test.ts +++ b/packages/keyring-internal-api/src/types.test.ts @@ -16,6 +16,11 @@ describe('InternalAccount', () => { address: 'GAKGOF5HPQSMKOJ6L4I2LNTLY6EERIRC7ZB6F7MOAXDLPZ7D5I4NZGNZ', scopes: ['stellar:pubnet', 'stellar:testnet'], }, + { + type: 'xrpl:account', + address: 'rHb9CJAWyB4rj91VRWn96DkukG4bwdtyTh', + scopes: ['xrpl:0'], + }, ])('should have the correct structure: %s', ({ type, address, scopes }) => { const account = { id: '606a7759-b0fb-48e4-9874-bab62ff8e7eb', diff --git a/packages/keyring-internal-api/src/types.ts b/packages/keyring-internal-api/src/types.ts index 4e8f1704e..2f8fc37e1 100644 --- a/packages/keyring-internal-api/src/types.ts +++ b/packages/keyring-internal-api/src/types.ts @@ -17,6 +17,8 @@ import { TrxEoaAccountStruct, XlmAccountType, XlmAccountStruct, + XrpAccountType, + XrpAccountStruct, } from '@metamask/keyring-api'; import type { Infer, Struct } from '@metamask/superstruct'; import { exactOptional, object, string, number } from '@metamask/superstruct'; @@ -26,7 +28,8 @@ export type InternalAccountType = | BtcAccountType | SolAccountType | TrxAccountType - | XlmAccountType; + | XlmAccountType + | XrpAccountType; export const InternalAccountMetadataStruct = object({ metadata: object({ @@ -90,6 +93,11 @@ export const InternalXlmAccountStruct = object({ ...InternalAccountMetadataStruct.schema, }); +export const InternalXrpAccountStruct = object({ + ...XrpAccountStruct.schema, + ...InternalAccountMetadataStruct.schema, +}); + export type InternalEthEoaAccount = Infer; export type InternalEthErc4337Account = Infer< @@ -114,6 +122,8 @@ export type InternalTrxEoaAccount = Infer; export type InternalXlmAccount = Infer; +export type InternalXrpAccount = Infer; + export const InternalAccountStructs: Record< string, | Struct @@ -125,6 +135,7 @@ export const InternalAccountStructs: Record< | Struct | Struct | Struct + | Struct > = { [`${EthAccountType.Eoa}`]: InternalEthEoaAccountStruct, [`${EthAccountType.Erc4337}`]: InternalEthErc4337AccountStruct, @@ -135,6 +146,7 @@ export const InternalAccountStructs: Record< [`${SolAccountType.DataAccount}`]: InternalSolDataAccountStruct, [`${TrxAccountType.Eoa}`]: InternalTrxEoaAccountStruct, [`${XlmAccountType.Account}`]: InternalXlmAccountStruct, + [`${XrpAccountType.Account}`]: InternalXrpAccountStruct, }; export type InternalAccountTypes = @@ -146,7 +158,8 @@ export type InternalAccountTypes = | InternalBtcP2trAccount | InternalSolDataAccount | InternalTrxEoaAccount - | InternalXlmAccount; + | InternalXlmAccount + | InternalXrpAccount; export const InternalAccountStruct = object({ ...KeyringAccountStruct.schema, diff --git a/packages/keyring-snap-bridge/CHANGELOG.md b/packages/keyring-snap-bridge/CHANGELOG.md index fe2a084ba..543c8fa73 100644 --- a/packages/keyring-snap-bridge/CHANGELOG.md +++ b/packages/keyring-snap-bridge/CHANGELOG.md @@ -7,6 +7,10 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 ## [Unreleased] +### Added + +- Support XRPL (`xrpl:account`) accounts in account assertions and v1 migrations ([#723](https://github.com/MetaMask/accounts/pull/723)) + ### Changed - Bump `@metamask/keyring-sdk` from `^4.2.1` to `^4.3.0` ([#720](https://github.com/MetaMask/accounts/pull/720)) diff --git a/packages/keyring-snap-bridge/src/SnapKeyring.test.ts b/packages/keyring-snap-bridge/src/SnapKeyring.test.ts index 1140cd167..31ffb3c19 100644 --- a/packages/keyring-snap-bridge/src/SnapKeyring.test.ts +++ b/packages/keyring-snap-bridge/src/SnapKeyring.test.ts @@ -34,6 +34,9 @@ import { XlmScope, XlmMethod, XlmAccountType, + XrpScope, + XrpMethod, + XrpAccountType, AccountCreationType, } from '@metamask/keyring-api'; import { SnapManageAccountsMethod } from '@metamask/keyring-snap-sdk'; @@ -233,7 +236,17 @@ describe('SnapKeyring', () => { type: XlmAccountType.Account, }; + const xrpAccount = { + id: '890ee179-5ab5-449d-9c25-34e12c1ada69', + address: 'rHb9CJAWyB4rj91VRWn96DkukG4bwdtyTh', + options: {}, + methods: [...Object.values(XrpMethod)], + scopes: [XrpScope.Mainnet], + type: XrpAccountType.Account, + }; + // This is an example generic chain, its scope and address are bogus. + const anyGenericAccount: KeyringAccount = { id: '6131c205-26b5-40a8-9626-4761423bb1d5', address: 'test-address-1234', @@ -280,6 +293,7 @@ describe('SnapKeyring', () => { solDataAccount, trxEoaAccount, xlmAccount, + xrpAccount, anyGenericAccount, ] as const; @@ -1245,6 +1259,7 @@ describe('SnapKeyring', () => { solDataAccount.address, trxEoaAccount.address, xlmAccount.address, + xrpAccount.address, anyGenericAccount.address, ]); }); @@ -1270,6 +1285,7 @@ describe('SnapKeyring', () => { solDataAccount.address, trxEoaAccount.address, xlmAccount.address, + xrpAccount.address, anyGenericAccount.address, ]); }); @@ -1487,6 +1503,7 @@ describe('SnapKeyring', () => { solDataAccount.address, trxEoaAccount.address, xlmAccount.address, + xrpAccount.address, anyGenericAccount.address, ]); }); @@ -1534,6 +1551,7 @@ describe('SnapKeyring', () => { [solDataAccount.id]: { account: solDataAccount, snapId }, [trxEoaAccount.id]: { account: trxEoaAccount, snapId }, [xlmAccount.id]: { account: xlmAccount, snapId }, + [xrpAccount.id]: { account: xrpAccount, snapId }, [anyGenericAccount.id]: { account: anyGenericAccount, snapId }, }, }; @@ -1614,6 +1632,7 @@ describe('SnapKeyring', () => { solDataAccount, trxEoaAccount, xlmAccount, + xrpAccount, ])('migrates accounts v1: %s', async (expectedAccount: KeyringAccount) => { // A v1 account has no scopes, so remove it. const state = { @@ -1638,6 +1657,7 @@ describe('SnapKeyring', () => { solDataAccount, trxEoaAccount, xlmAccount, + xrpAccount, anyGenericAccount, ])( 'migrates v2 accounts to v1 accounts is noop: %s', @@ -2299,6 +2319,7 @@ describe('SnapKeyring', () => { accounts[9].address, accounts[10].address, accounts[11].address, + accounts[12].address, ]); }); @@ -2369,11 +2390,13 @@ describe('SnapKeyring', () => { accounts[9].address, accounts[10].address, accounts[11].address, + accounts[12].address, ]); expect(console.error).toHaveBeenCalledWith( `Account '${ethEoaAccount1.id}' may not have been removed from snap '${snapId}':`, 'some error', ); + spy.mockRestore(); }); }); diff --git a/packages/keyring-snap-bridge/src/account.ts b/packages/keyring-snap-bridge/src/account.ts index afe9f34eb..38294e060 100644 --- a/packages/keyring-snap-bridge/src/account.ts +++ b/packages/keyring-snap-bridge/src/account.ts @@ -16,6 +16,8 @@ import { TrxEoaAccountStruct, XlmAccountType, XlmAccountStruct, + XrpAccountType, + XrpAccountStruct, isEvmAccountType, } from '@metamask/keyring-api'; import { assert, omit } from '@metamask/superstruct'; @@ -79,6 +81,10 @@ export function assertKeyringAccount< assert(account, XlmAccountStruct); return account; } + case XrpAccountType.Account: { + assert(account, XrpAccountStruct); + return account; + } case AnyAccountType.Account: { assert(account, KeyringAccountStruct); return account; diff --git a/packages/keyring-snap-bridge/src/migrations/v1.ts b/packages/keyring-snap-bridge/src/migrations/v1.ts index d298b3541..796efc486 100644 --- a/packages/keyring-snap-bridge/src/migrations/v1.ts +++ b/packages/keyring-snap-bridge/src/migrations/v1.ts @@ -9,6 +9,8 @@ import { TrxAccountType, XlmScope, XlmAccountType, + XrpScope, + XrpAccountType, } from '@metamask/keyring-api'; import type { CaipChainId, KeyringAccount } from '@metamask/keyring-api'; import { isBtcMainnetAddress } from '@metamask/keyring-utils'; @@ -77,6 +79,10 @@ export function getScopesForAccountV1( // Stellar account supports mainnet and testnet. return [XlmScope.Pubnet, XlmScope.Testnet]; } + case XrpAccountType.Account: { + // XRPL account currently supports mainnet only. + return [XrpScope.Mainnet]; + } default: // We re-use EOA scopes if we don't know what to do for now. return [EthScope.Eoa];