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
1 change: 1 addition & 0 deletions packages/snap-networks-utils/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

### Added

- Add `ExtendedKeyringAccount`, `KeyringAccountExtension`, and `asStrictKeyringAccount` for snap keyring accounts that persist `entropySource`, `derivationPath`, and `index`. ([#300](https://github.com/MetaMask/internal-snaps/pull/300))
- Add shared snap state helpers `IStateManager`, `State`, and `InMemoryState` (Tron-style write mutex plus blob/path locking). ([#288](https://github.com/MetaMask/internal-snaps/pull/288))
- Add shared caching utilities for network snaps ([#287](https://github.com/MetaMask/internal-snaps/pull/287))
- `ICache`, `CacheEntry`, and `TimestampMilliseconds` for describing a generic cache
Expand Down
1 change: 1 addition & 0 deletions packages/snap-networks-utils/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,7 @@
},
"dependencies": {
"@metamask/assets-controller": "^13.0.0",
"@metamask/keyring-api": "^24.1.0",
"@metamask/remote-feature-flag-controller": "^5.0.0",
"@metamask/snaps-sdk": "^12.0.1",
"@metamask/superstruct": "^3.4.1",
Expand Down
5 changes: 5 additions & 0 deletions packages/snap-networks-utils/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,11 @@ export {
} from './utils/sanitize/sanitize';
export { UrlStruct } from './utils/urlStruct/urlStruct';
export { UuidStruct } from './utils/uuidStruct/uuidStruct';
export { asStrictKeyringAccount } from './utils/keyringAccount/keyringAccount';
export type {
ExtendedKeyringAccount,
KeyringAccountExtension,
} from './utils/keyringAccount/keyringAccount';
export {
batchesAll,
batchesAllSettled,
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
import { asStrictKeyringAccount } from './keyringAccount';
import type { ExtendedKeyringAccount } from './keyringAccount';

describe('asStrictKeyringAccount', () => {
const account: ExtendedKeyringAccount = {
type: 'eip155:eoa',
id: '4b445722-6766-4f99-ade5-c2c9295f21d0',
address: 'TAddress0',
options: {
entropy: {
type: 'mnemonic',
id: 'entropy-source',
derivationPath: "m/44'/195'/0'/0/0",
groupIndex: 0,
},
},
methods: ['personal_sign'],
scopes: ['tron:mainnet'],
entropySource: 'entropy-source',
derivationPath: "m/44'/195'/0'/0/0",
index: 0,
};

it('returns only KeyringAccount fields', () => {
expect(asStrictKeyringAccount(account)).toStrictEqual({
type: account.type,
id: account.id,
address: account.address,
options: account.options,
methods: account.methods,
scopes: account.scopes,
});
});

it('does not include entropySource, derivationPath, or index', () => {
expect(asStrictKeyringAccount(account)).not.toHaveProperty('entropySource');
expect(asStrictKeyringAccount(account)).not.toHaveProperty(
'derivationPath',
);
expect(asStrictKeyringAccount(account)).not.toHaveProperty('index');
});
});
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
import type { EntropySourceId, KeyringAccount } from '@metamask/keyring-api';

/**
* Keyring account fields snaps persist in addition to `@metamask/keyring-api`'s
* `KeyringAccount`. `index` is stored so an account can be restored at a
* previously used derivation index.
*/
export type KeyringAccountExtension = {
entropySource: EntropySourceId;
derivationPath: `m/${string}`;
index: number;
};

/**
* A `KeyringAccount` plus the derivation fields used by Solana, Tron, and Stellar.
*
* A snap may narrow `derivationPath` (for example Stellar's BIP-44 coin type).
*/
export type ExtendedKeyringAccount = KeyringAccount & KeyringAccountExtension;

/**
* Converts an extended keyring account to the Keyring API shape (no extra fields).
*
* @param account - A keyring account, possibly with snap-specific fields.
* @returns A strict keyring account.
*/
export function asStrictKeyringAccount(
account: KeyringAccount,
): KeyringAccount {
const { id, address, type, options, methods, scopes } = account;
return {
id,
address,
type,
options,
methods,
scopes,
};
}
1 change: 1 addition & 0 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -3327,6 +3327,7 @@ __metadata:
dependencies:
"@metamask/assets-controller": "npm:^13.0.0"
"@metamask/auto-changelog": "npm:^6.1.1"
"@metamask/keyring-api": "npm:^24.1.0"
"@metamask/messenger": "npm:^2.0.0"
"@metamask/remote-feature-flag-controller": "npm:^5.0.0"
"@metamask/snaps-sdk": "npm:^12.0.1"
Expand Down