Skip to content
Open
Show file tree
Hide file tree
Changes from 2 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
16 changes: 16 additions & 0 deletions packages/snap/integration-test/keyring-request.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -167,6 +167,7 @@ describe('KeyringRequestHandler', () => {
request: {
method: AccountCapability.GetUtxo,
params: {
account: { address: account.address },
outpoint: utxos[0]?.outpoint,
},
},
Expand Down Expand Up @@ -221,6 +222,7 @@ describe('KeyringRequestHandler', () => {
request: {
method: AccountCapability.SignPsbt,
params: {
account: { address: account.address },
psbt: TEMPLATE_PSBT,
feeRate: 3,
options: {
Expand Down Expand Up @@ -253,6 +255,7 @@ describe('KeyringRequestHandler', () => {
request: {
method: AccountCapability.SignPsbt,
params: {
account: { address: account.address },
psbt: TEMPLATE_PSBT,
feeRate: 3,
options: {
Expand Down Expand Up @@ -285,6 +288,7 @@ describe('KeyringRequestHandler', () => {
request: {
method: AccountCapability.SignPsbt,
params: {
account: { address: account.address },
psbt: TEMPLATE_PSBT,
feeRate: 3,
options: {
Expand Down Expand Up @@ -317,6 +321,7 @@ describe('KeyringRequestHandler', () => {
request: {
method: AccountCapability.SignPsbt,
params: {
account: { address: account.address },
psbt: 'notAPsbt',
options: {
fill: true,
Expand Down Expand Up @@ -350,6 +355,7 @@ describe('KeyringRequestHandler', () => {
request: {
method: AccountCapability.SignPsbt,
params: {
account: { address: account.address },
psbt: TEMPLATE_PSBT,
},
},
Expand Down Expand Up @@ -382,6 +388,7 @@ describe('KeyringRequestHandler', () => {
request: {
method: AccountCapability.FillPsbt,
params: {
account: { address: account.address },
psbt: TEMPLATE_PSBT,
feeRate: 3,
},
Expand Down Expand Up @@ -409,6 +416,7 @@ describe('KeyringRequestHandler', () => {
request: {
method: AccountCapability.FillPsbt,
params: {
account: { address: account.address },
psbt: 'notAPsbt',
},
},
Expand Down Expand Up @@ -444,6 +452,7 @@ describe('KeyringRequestHandler', () => {
request: {
method: AccountCapability.ComputeFee,
params: {
account: { address: account.address },
psbt: TEMPLATE_PSBT,
feeRate: 3,
},
Expand Down Expand Up @@ -471,6 +480,7 @@ describe('KeyringRequestHandler', () => {
request: {
method: AccountCapability.ComputeFee,
params: {
account: { address: account.address },
psbt: 'notAPsbt',
},
},
Expand Down Expand Up @@ -507,6 +517,7 @@ describe('KeyringRequestHandler', () => {
request: {
method: AccountCapability.SignPsbt,
params: {
account: { address: account.address },
psbt: TEMPLATE_PSBT,
feeRate: 3,
options: {
Expand All @@ -533,6 +544,7 @@ describe('KeyringRequestHandler', () => {
request: {
method: AccountCapability.BroadcastPsbt,
params: {
account: { address: account.address },
psbt: result.psbt,
},
},
Expand All @@ -559,6 +571,7 @@ describe('KeyringRequestHandler', () => {
request: {
method: AccountCapability.BroadcastPsbt,
params: {
account: { address: account.address },
psbt: 'notAPsbt',
},
},
Expand Down Expand Up @@ -590,6 +603,7 @@ describe('KeyringRequestHandler', () => {
request: {
method: AccountCapability.SendTransfer,
params: {
account: { address: account.address },
recipients: [
{
address: 'bcrt1qstku2y3pfh9av50lxj55arm8r5gj8tf2yv5nxz',
Expand Down Expand Up @@ -626,6 +640,7 @@ describe('KeyringRequestHandler', () => {
request: {
method: AccountCapability.SendTransfer,
params: {
account: { address: account.address },
recipients: [{ address: 'notAnAddress', amount: '1000' }],
},
},
Expand Down Expand Up @@ -654,6 +669,7 @@ describe('KeyringRequestHandler', () => {
request: {
method: AccountCapability.SignMessage,
params: {
account: { address: account.address },
message: 'Hello, world!',
},
},
Expand Down
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-bitcoin-wallet.git"
},
"source": {
"shasum": "o9ZJmt7WEmIOXnmPlqmqRbGWztcCkDTKkW2VaBza56E=",
"shasum": "Ph0Ibbk8XLQfwi4HRKZx4SQ3itVjUHsMVQG/+/HIHbE=",
"location": {
"npm": {
"filePath": "dist/bundle.js",
Expand Down
43 changes: 36 additions & 7 deletions packages/snap/src/handlers/KeyringRequestHandler.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,21 +4,27 @@ import { mock } from 'jest-mock-extended';
import { assert } from 'superstruct';

import type { AccountUseCases } from '../use-cases';
import { KeyringRequestHandler } from './KeyringRequestHandler';
import {
BroadcastPsbtRequest,
ComputeFeeRequest,
FillPsbtRequest,
GetUtxoRequest,
KeyringRequestHandler,
SendTransferRequest,
SignPsbtRequest,
} from './KeyringRequestHandler';
} from './validation';
import type { BitcoinAccount } from '../entities';
import { AccountCapability } from '../entities';
import type { Utxo } from './mappings';
import { mapToUtxo } from './mappings';
import { parsePsbt } from './parsers';

/* eslint-disable @typescript-eslint/naming-convention */
jest.mock('@metamask/bitcoindevkit', () => ({
Address: { from_string: jest.fn() },
Amount: { from_btc: jest.fn() },
}));

jest.mock('superstruct', () => ({
...jest.requireActual('superstruct'),
assert: jest.fn(),
Expand All @@ -36,6 +42,11 @@ describe('KeyringRequestHandler', () => {
const mockAccountsUseCases = mock<AccountUseCases>();
const origin = 'metamask';

const ACCOUNT_ADDRESS = 'test-account-address';
const accountParam = {
account: { address: ACCOUNT_ADDRESS },
};

const handler = new KeyringRequestHandler(mockAccountsUseCases);

beforeEach(() => {
Expand Down Expand Up @@ -65,6 +76,7 @@ describe('KeyringRequestHandler', () => {
request: {
method: AccountCapability.SignPsbt,
params: {
...accountParam,
psbt: 'psbtBase64',
feeRate: 3,
options: mockOptions,
Expand Down Expand Up @@ -109,7 +121,10 @@ describe('KeyringRequestHandler', () => {
await expect(
handler.route({
...mockRequest,
request: { ...mockRequest.request, params: { psbt: 'invalidPsbt' } },
request: {
...mockRequest.request,
params: { ...accountParam, psbt: 'invalidPsbt' },
},
}),
).rejects.toThrow(error);

Expand All @@ -131,6 +146,7 @@ describe('KeyringRequestHandler', () => {
request: {
method: AccountCapability.ComputeFee,
params: {
...accountParam,
psbt: 'psbtBase64',
feeRate: 3,
},
Expand All @@ -141,7 +157,6 @@ describe('KeyringRequestHandler', () => {
it('executes computeFee', async () => {
mockAccountsUseCases.computeFee.mockResolvedValue(
mock<Amount>({
// eslint-disable-next-line @typescript-eslint/naming-convention
to_sat: () => BigInt(1000),
}),
);
Expand Down Expand Up @@ -172,7 +187,10 @@ describe('KeyringRequestHandler', () => {
await expect(
handler.route({
...mockRequest,
request: { ...mockRequest.request, params: { psbt: 'invalidPsbt' } },
request: {
...mockRequest.request,
params: { ...accountParam, psbt: 'invalidPsbt' },
},
}),
).rejects.toThrow(error);

Expand All @@ -194,6 +212,7 @@ describe('KeyringRequestHandler', () => {
request: {
method: AccountCapability.FillPsbt,
params: {
...accountParam,
psbt: 'psbtBase64',
feeRate: 3,
},
Expand Down Expand Up @@ -233,7 +252,10 @@ describe('KeyringRequestHandler', () => {
await expect(
handler.route({
...mockRequest,
request: { ...mockRequest.request, params: { psbt: 'invalidPsbt' } },
request: {
...mockRequest.request,
params: { ...accountParam, psbt: 'invalidPsbt' },
},
}),
).rejects.toThrow(error);

Expand All @@ -256,6 +278,7 @@ describe('KeyringRequestHandler', () => {
request: {
method: AccountCapability.BroadcastPsbt,
params: {
...accountParam,
psbt: 'psbtBase64',
feeRate: 3,
},
Expand Down Expand Up @@ -295,7 +318,10 @@ describe('KeyringRequestHandler', () => {
await expect(
handler.route({
...mockRequest,
request: { ...mockRequest.request, params: { psbt: 'invalidPsbt' } },
request: {
...mockRequest.request,
params: { ...accountParam, psbt: 'invalidPsbt' },
},
}),
).rejects.toThrow(error);

Expand Down Expand Up @@ -324,6 +350,7 @@ describe('KeyringRequestHandler', () => {
request: {
method: AccountCapability.SendTransfer,
params: {
...accountParam,
recipients,
feeRate: 3,
},
Expand Down Expand Up @@ -376,6 +403,7 @@ describe('KeyringRequestHandler', () => {
request: {
method: AccountCapability.GetUtxo,
params: {
...accountParam,
outpoint: 'mytxid:0',
},
},
Expand Down Expand Up @@ -470,6 +498,7 @@ describe('KeyringRequestHandler', () => {
request: {
method: AccountCapability.SignMessage,
params: {
...accountParam,
message: 'message',
},
},
Expand Down
60 changes: 10 additions & 50 deletions packages/snap/src/handlers/KeyringRequestHandler.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,6 @@
import type { KeyringRequest, KeyringResponse } from '@metamask/keyring-api';
import type { Json } from '@metamask/snaps-sdk';
import {
array,
assert,
boolean,
number,
object,
optional,
string,
} from 'superstruct';
import { assert } from 'superstruct';

import {
AccountCapability,
Expand All @@ -17,67 +9,35 @@ import {
} from '../entities';
import { mapToUtxo } from './mappings';
import { parsePsbt } from './parsers';
import {
BroadcastPsbtRequest,
ComputeFeeRequest,
FillPsbtRequest,
GetUtxoRequest,
SendTransferRequest,
SignMessageRequest,
SignPsbtRequest,
} from './validation';
import type { AccountUseCases } from '../use-cases/AccountUseCases';

export const SignPsbtRequest = object({
psbt: string(),
feeRate: optional(number()),
options: object({
fill: boolean(),
broadcast: boolean(),
}),
});

export type SignPsbtResponse = {
psbt: string;
txid: string | null;
};

export const ComputeFeeRequest = object({
psbt: string(),
feeRate: optional(number()),
});

export type ComputeFeeResponse = {
// Fee in satoshis
fee: string;
};

export const BroadcastPsbtRequest = object({
psbt: string(),
});

export type BroadcastPsbtResponse = {
txid: string;
};

export const FillPsbtRequest = object({
psbt: string(),
feeRate: optional(number()),
});

export type FillPsbtResponse = {
psbt: string;
};

export const SendTransferRequest = object({
recipients: array(
object({
address: string(),
amount: string(),
}),
),
feeRate: optional(number()),
});

export const GetUtxoRequest = object({
outpoint: string(),
});

export const SignMessageRequest = object({
message: string(),
});

export type SignMessageResponse = {
signature: string;
};
Expand Down
3 changes: 3 additions & 0 deletions packages/snap/src/handlers/caip.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import type { AddressType, Network } from '@metamask/bitcoindevkit';
import { BtcAccountType, BtcScope } from '@metamask/keyring-api';
import { enums } from 'superstruct';

const reverseMapping = <
From extends string | number | symbol,
Expand Down Expand Up @@ -37,6 +38,8 @@ export enum Caip19Asset {
Regtest = 'bip122:regtest/slip44:0',
}

export const NetworkStruct = enums(Object.values(BtcScope));

export const networkToCaip19: Record<Network, Caip19Asset> = {
bitcoin: Caip19Asset.Bitcoin,
testnet: Caip19Asset.Testnet,
Expand Down
Loading
Loading