Skip to content

Commit

Permalink
Merge branch 'main' into mikesposito/hdkeyring-typescript
Browse files Browse the repository at this point in the history
  • Loading branch information
mikesposito committed Jan 28, 2025
2 parents 19bfdd4 + 063dfa4 commit db56cb5
Show file tree
Hide file tree
Showing 42 changed files with 1,173 additions and 730 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@metamask/accounts-monorepo",
"version": "20.0.0",
"version": "23.0.0",
"private": true,
"description": "Monorepo for MetaMask accounts related packages",
"repository": {
Expand Down
25 changes: 24 additions & 1 deletion packages/keyring-api/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,27 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

## [Unreleased]

## [15.0.0]

### Added

- Add `account{AssetList,Balances,Transactions}UpdatedEventStruct` keyring events ([#154](https://github.com/MetaMask/accounts/pull/154))

### Changed

- **BREAKING:** Make specific `*AccountStruct.scopes` more strict ([#159](https://github.com/MetaMask/accounts/pull/159))

## [14.0.0]

### Added

- Add `listAccountAssets` keyring method ([#148](https://github.com/MetaMask/accounts/pull/148))

### Changed

- **BREAKING:** Make `CaipAssetType` type more restritive ([#150](https://github.com/MetaMask/accounts/pull/150))
- It used to be a `string` but it has been restricted with a template literal type that matches CAIP-19 asset type.

## [13.0.0]

### Added
Expand Down Expand Up @@ -466,7 +487,9 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
- SnapController keyring client. It is intended to be used by MetaMask to talk to the snap.
- Helper functions to create keyring handler in the snap.

[Unreleased]: https://github.com/MetaMask/accounts/compare/@metamask/keyring-api@13.0.0...HEAD
[Unreleased]: https://github.com/MetaMask/accounts/compare/@metamask/keyring-api@15.0.0...HEAD
[15.0.0]: https://github.com/MetaMask/accounts/compare/@metamask/keyring-api@14.0.0...@metamask/keyring-api@15.0.0
[14.0.0]: https://github.com/MetaMask/accounts/compare/@metamask/keyring-api@13.0.0...@metamask/keyring-api@14.0.0
[13.0.0]: https://github.com/MetaMask/accounts/compare/@metamask/keyring-api@12.0.0...@metamask/keyring-api@13.0.0
[12.0.0]: https://github.com/MetaMask/accounts/compare/@metamask/keyring-api@11.1.0...@metamask/keyring-api@12.0.0
[11.1.0]: https://github.com/MetaMask/accounts/compare/@metamask/keyring-api@11.0.0...@metamask/keyring-api@11.1.0
Expand Down
2 changes: 1 addition & 1 deletion packages/keyring-api/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@metamask/keyring-api",
"version": "13.0.0",
"version": "15.0.0",
"description": "MetaMask Keyring API",
"keywords": [
"metamask",
Expand Down
6 changes: 3 additions & 3 deletions packages/keyring-api/src/api/account.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { object, UuidStruct } from '@metamask/keyring-utils';
import { AccountIdStruct, object } from '@metamask/keyring-utils';
import type { Infer } from '@metamask/superstruct';
import {
nonempty,
Expand Down Expand Up @@ -56,7 +56,7 @@ export const KeyringAccountStruct = object({
/**
* Account ID (UUIDv4).
*/
id: UuidStruct,
id: AccountIdStruct,

/**
* Account type.
Expand All @@ -74,7 +74,7 @@ export const KeyringAccountStruct = object({
address: string(),

/**
* Account supported scopes (CAIP-2 chain IDs).
* Account supported scopes (CAIP-2 chain IDs or CAIP-2 namespaces).
*/
scopes: nonempty(array(union([CaipNamespaceStruct, CaipChainIdStruct]))),

Expand Down
25 changes: 16 additions & 9 deletions packages/keyring-api/src/api/asset.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,21 @@ import {
isPlainObject,
} from '@metamask/utils';

/**
* Fungible asset amount struct.
*/
export const FungibleAssetAmountStruct = object({
/**
* Asset unit.
*/
unit: string(),

/**
* Asset amount.
*/
amount: StringNumberStruct,
});

/**
* Fungible asset struct.
*/
Expand All @@ -25,15 +40,7 @@ export const FungibleAssetStruct = object({
*/
type: CaipAssetTypeStruct,

/**
* Asset unit.
*/
unit: string(),

/**
* Asset amount.
*/
amount: StringNumberStruct,
...FungibleAssetAmountStruct.schema,
});

/**
Expand Down
2 changes: 1 addition & 1 deletion packages/keyring-api/src/btc/constants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
/**
* Scopes for Bitcoin account type. See {@link KeyringAccount.scopes}.
*/
export enum BtcScopes {
export enum BtcScope {
Namespace = 'bip122',
Mainnet = 'bip122:000000000019d6689c085ae165831e93',
Testnet = 'bip122:000000000933ea01ad0ee984209779ba',
Expand Down
38 changes: 37 additions & 1 deletion packages/keyring-api/src/btc/types.test.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,20 @@
import { BtcP2wpkhAddressStruct } from './types';
import { BtcScope } from './constants';
import type { BtcP2wpkhAccount } from './types';
import {
BtcMethod,
BtcP2wpkhAccountStruct,
BtcP2wpkhAddressStruct,
} from './types';
import { BtcAccountType } from '../api';

const MOCK_ACCOUNT = {
id: '55583f38-d81b-48f8-8494-fc543c2b5c95',
type: BtcAccountType.P2wpkh,
address: 'bc1qw508d6qejxtdg4y5r3zarvary0c5xw7kv8f3t4',
methods: [BtcMethod.SendBitcoin],
options: {},
scopes: [BtcScope.Mainnet],
};

describe('types', () => {
describe('BtcP2wpkhAddressStruct', () => {
Expand Down Expand Up @@ -37,5 +53,25 @@ describe('types', () => {
`${errorPrefix}: No separator character for ${address}`,
);
});

it('throws an error if there are multiple scopes', () => {
const account: BtcP2wpkhAccount = {
...MOCK_ACCOUNT,
scopes: [BtcScope.Mainnet, BtcScope.Testnet],
};
expect(() => BtcP2wpkhAccountStruct.assert(account)).toThrow(
'At path: scopes -- Expected a array with a length of `1` but received one with a length of `2`',
);
});

it('throws an error if there is no scope', () => {
const account: BtcP2wpkhAccount = {
...MOCK_ACCOUNT,
scopes: [],
};
expect(() => BtcP2wpkhAccountStruct.assert(account)).toThrow(
'At path: scopes -- Expected a array with a length of `1` but received one with a length of `0`',
);
});
});
});
17 changes: 16 additions & 1 deletion packages/keyring-api/src/btc/types.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,14 @@
import { object } from '@metamask/keyring-utils';
import type { Infer } from '@metamask/superstruct';
import { string, array, enums, refine, literal } from '@metamask/superstruct';
import {
string,
array,
enums,
refine,
literal,
size,
} from '@metamask/superstruct';
import { CaipChainIdStruct } from '@metamask/utils';
import { bech32 } from 'bech32';

import { BtcAccountType, KeyringAccountStruct } from '../api';
Expand Down Expand Up @@ -41,6 +49,13 @@ export const BtcP2wpkhAccountStruct = object({
*/
type: literal(`${BtcAccountType.P2wpkh}`),

/**
* Account supported scope (CAIP-2 chain ID).
*
* NOTE: We consider a Bitcoin address to be valid on only 1 network at time.
*/
scopes: size(array(CaipChainIdStruct), 1),

/**
* Account supported methods.
*/
Expand Down
3 changes: 2 additions & 1 deletion packages/keyring-api/src/eth/constants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,8 @@
/**
* Scopes for EVM account type. See {@link KeyringAccount.scopes}.
*/
export enum EthScopes {
export enum EthScope {
Namespace = 'eip155',
Mainnet = 'eip155:1',
Testnet = 'eip155:11155111',
}
10 changes: 8 additions & 2 deletions packages/keyring-api/src/eth/types.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
import { object, definePattern } from '@metamask/keyring-utils';
import type { Infer } from '@metamask/superstruct';
import { nonempty, array, enums, literal } from '@metamask/superstruct';
import { CaipChainIdStruct } from '@metamask/utils';

import { EthScopes } from '.';
import { EthScope } from '.';
import { EthAccountType, KeyringAccountStruct } from '../api';

export const EthBytesStruct = definePattern('EthBytes', /^0x[0-9a-f]*$/iu);
Expand Down Expand Up @@ -50,7 +51,7 @@ export const EthEoaAccountStruct = object({
/**
* Account scopes (must be ['eip155']).
*/
scopes: nonempty(array(literal(EthScopes.Namespace))),
scopes: nonempty(array(literal(EthScope.Namespace))),

/**
* Account supported methods.
Expand Down Expand Up @@ -82,6 +83,11 @@ export const EthErc4337AccountStruct = object({
*/
type: literal(`${EthAccountType.Erc4337}`),

/**
* Account supported scopes (CAIP-2 chain IDs).
*/
scopes: nonempty(array(CaipChainIdStruct)),

/**
* Account supported methods.
*/
Expand Down
12 changes: 6 additions & 6 deletions packages/keyring-api/src/events.test.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { is } from '@metamask/superstruct';

import { EthAccountType } from './api';
import { EthScopes } from './eth/constants';
import { EthScope } from './eth/constants';
import {
AccountCreatedEventStruct,
AccountDeletedEventStruct,
Expand All @@ -22,7 +22,7 @@ describe('events', () => {
address: '0x0123',
methods: [],
options: {},
scopes: [EthScopes.Namespace],
scopes: [EthScope.Namespace],
type: EthAccountType.Eoa,
},
},
Expand All @@ -40,7 +40,7 @@ describe('events', () => {
address: '0x0123',
methods: [],
options: {},
scopes: [EthScopes.Namespace],
scopes: [EthScope.Namespace],
type: EthAccountType.Eoa,
},
},
Expand All @@ -58,7 +58,7 @@ describe('events', () => {
address: '0x0123',
methods: [],
options: {},
scopes: [EthScopes.Namespace],
scopes: [EthScope.Namespace],
type: EthAccountType.Eoa,
},
displayConfirmation: true,
Expand All @@ -79,7 +79,7 @@ describe('events', () => {
address: '0x0123',
methods: [],
options: {},
scopes: [EthScopes.Namespace],
scopes: [EthScope.Namespace],
type: EthAccountType.Eoa,
},
},
Expand All @@ -97,7 +97,7 @@ describe('events', () => {
address: '0x0123',
methods: [],
options: {},
scopes: [EthScopes.Namespace],
scopes: [EthScope.Namespace],
type: EthAccountType.Eoa,
},
},
Expand Down
Loading

0 comments on commit db56cb5

Please sign in to comment.