Skip to content
Open
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
4 changes: 4 additions & 0 deletions packages/keyring-api/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
16 changes: 16 additions & 0 deletions packages/keyring-api/src/api/account.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -70,6 +83,7 @@ export type KeyringAccountType =
| `${SolAccountType.DataAccount}`
| `${TrxAccountType.Eoa}`
| `${XlmAccountType.Account}`
| `${XrpAccountType.Account}`
| `${AnyAccountType.Account}`;

/**
Expand All @@ -85,6 +99,7 @@ export const KeyringAccountTypeStruct = enums([
`${SolAccountType.DataAccount}`,
`${TrxAccountType.Eoa}`,
`${XlmAccountType.Account}`,
`${XrpAccountType.Account}`,
`${AnyAccountType.Account}`,
]);

Expand Down Expand Up @@ -114,6 +129,7 @@ export const KeyringAccountStruct = object({
`${SolAccountType.DataAccount}`,
`${TrxAccountType.Eoa}`,
`${XlmAccountType.Account}`,
`${XrpAccountType.Account}`,
`${AnyAccountType.Account}`,
]),

Expand Down
2 changes: 2 additions & 0 deletions packages/keyring-api/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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';
10 changes: 10 additions & 0 deletions packages/keyring-api/src/xrp/constants.ts
Original file line number Diff line number Diff line change
@@ -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',
}
2 changes: 2 additions & 0 deletions packages/keyring-api/src/xrp/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
export * from './constants';
export * from './types';
8 changes: 8 additions & 0 deletions packages/keyring-api/src/xrp/types.test-d.ts
Original file line number Diff line number Diff line change
@@ -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<Extends<XrpAccount, KeyringAccount>>();
28 changes: 28 additions & 0 deletions packages/keyring-api/src/xrp/types.test.ts
Original file line number Diff line number Diff line change
@@ -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}"\``,
);
});
});
});
54 changes: 54 additions & 0 deletions packages/keyring-api/src/xrp/types.ts
Original file line number Diff line number Diff line change
@@ -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<typeof XrpAccountStruct>;
4 changes: 4 additions & 0 deletions packages/keyring-internal-api/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
5 changes: 5 additions & 0 deletions packages/keyring-internal-api/src/types.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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',
Expand Down
17 changes: 15 additions & 2 deletions packages/keyring-internal-api/src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand All @@ -26,7 +28,8 @@ export type InternalAccountType =
| BtcAccountType
| SolAccountType
| TrxAccountType
| XlmAccountType;
| XlmAccountType
| XrpAccountType;

export const InternalAccountMetadataStruct = object({
metadata: object({
Expand Down Expand Up @@ -90,6 +93,11 @@ export const InternalXlmAccountStruct = object({
...InternalAccountMetadataStruct.schema,
});

export const InternalXrpAccountStruct = object({
...XrpAccountStruct.schema,
...InternalAccountMetadataStruct.schema,
});

export type InternalEthEoaAccount = Infer<typeof InternalEthEoaAccountStruct>;

export type InternalEthErc4337Account = Infer<
Expand All @@ -114,6 +122,8 @@ export type InternalTrxEoaAccount = Infer<typeof InternalTrxEoaAccountStruct>;

export type InternalXlmAccount = Infer<typeof InternalXlmAccountStruct>;

export type InternalXrpAccount = Infer<typeof InternalXrpAccountStruct>;

export const InternalAccountStructs: Record<
string,
| Struct<InternalEthEoaAccount>
Expand All @@ -125,6 +135,7 @@ export const InternalAccountStructs: Record<
| Struct<InternalSolDataAccount>
| Struct<InternalTrxEoaAccount>
| Struct<InternalXlmAccount>
| Struct<InternalXrpAccount>
> = {
[`${EthAccountType.Eoa}`]: InternalEthEoaAccountStruct,
[`${EthAccountType.Erc4337}`]: InternalEthErc4337AccountStruct,
Expand All @@ -135,6 +146,7 @@ export const InternalAccountStructs: Record<
[`${SolAccountType.DataAccount}`]: InternalSolDataAccountStruct,
[`${TrxAccountType.Eoa}`]: InternalTrxEoaAccountStruct,
[`${XlmAccountType.Account}`]: InternalXlmAccountStruct,
[`${XrpAccountType.Account}`]: InternalXrpAccountStruct,
};

export type InternalAccountTypes =
Expand All @@ -146,7 +158,8 @@ export type InternalAccountTypes =
| InternalBtcP2trAccount
| InternalSolDataAccount
| InternalTrxEoaAccount
| InternalXlmAccount;
| InternalXlmAccount
| InternalXrpAccount;

export const InternalAccountStruct = object({
...KeyringAccountStruct.schema,
Expand Down
4 changes: 4 additions & 0 deletions packages/keyring-snap-bridge/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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))
Expand Down
23 changes: 23 additions & 0 deletions packages/keyring-snap-bridge/src/SnapKeyring.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,9 @@ import {
XlmScope,
XlmMethod,
XlmAccountType,
XrpScope,
XrpMethod,
XrpAccountType,
AccountCreationType,
} from '@metamask/keyring-api';
import { SnapManageAccountsMethod } from '@metamask/keyring-snap-sdk';
Expand Down Expand Up @@ -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',
Expand Down Expand Up @@ -280,6 +293,7 @@ describe('SnapKeyring', () => {
solDataAccount,
trxEoaAccount,
xlmAccount,
xrpAccount,
anyGenericAccount,
] as const;

Expand Down Expand Up @@ -1245,6 +1259,7 @@ describe('SnapKeyring', () => {
solDataAccount.address,
trxEoaAccount.address,
xlmAccount.address,
xrpAccount.address,
anyGenericAccount.address,
]);
});
Expand All @@ -1270,6 +1285,7 @@ describe('SnapKeyring', () => {
solDataAccount.address,
trxEoaAccount.address,
xlmAccount.address,
xrpAccount.address,
anyGenericAccount.address,
]);
});
Expand Down Expand Up @@ -1487,6 +1503,7 @@ describe('SnapKeyring', () => {
solDataAccount.address,
trxEoaAccount.address,
xlmAccount.address,
xrpAccount.address,
anyGenericAccount.address,
]);
});
Expand Down Expand Up @@ -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 },
},
};
Expand Down Expand Up @@ -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 = {
Expand All @@ -1638,6 +1657,7 @@ describe('SnapKeyring', () => {
solDataAccount,
trxEoaAccount,
xlmAccount,
xrpAccount,
anyGenericAccount,
])(
'migrates v2 accounts to v1 accounts is noop: %s',
Expand Down Expand Up @@ -2299,6 +2319,7 @@ describe('SnapKeyring', () => {
accounts[9].address,
accounts[10].address,
accounts[11].address,
accounts[12].address,
]);
});

Expand Down Expand Up @@ -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();
});
});
Expand Down
6 changes: 6 additions & 0 deletions packages/keyring-snap-bridge/src/account.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@ import {
TrxEoaAccountStruct,
XlmAccountType,
XlmAccountStruct,
XrpAccountType,
XrpAccountStruct,
isEvmAccountType,
} from '@metamask/keyring-api';
import { assert, omit } from '@metamask/superstruct';
Expand Down Expand Up @@ -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;
Expand Down
Loading
Loading