@metamask/snaps-simulation
Version:
A simulation framework for MetaMask Snaps, enabling headless testing of Snaps in a controlled environment
208 lines • 10.7 kB
JavaScript
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.assertIsResponseWithInterface = exports.SnapResponseStruct = exports.SnapResponseWithInterfaceStruct = exports.SnapResponseWithoutInterfaceStruct = exports.InterfaceStruct = exports.JsonRpcMockOptionsStruct = exports.SnapOptionsStruct = exports.NameLookupOptionsStruct = exports.BaseNameLookupOptionsStruct = exports.SignatureOptionsStruct = exports.TransactionOptionsStruct = void 0;
const snaps_sdk_1 = require("@metamask/snaps-sdk");
const jsx_1 = require("@metamask/snaps-sdk/jsx");
const snaps_utils_1 = require("@metamask/snaps-utils");
const superstruct_1 = require("@metamask/superstruct");
const utils_1 = require("@metamask/utils");
const crypto_1 = require("crypto");
// TODO: Export this from `@metamask/utils` instead.
const BytesLikeStruct = (0, superstruct_1.union)([
(0, superstruct_1.bigint)(),
(0, superstruct_1.number)(),
(0, superstruct_1.string)(),
(0, superstruct_1.instance)(Uint8Array),
]);
exports.TransactionOptionsStruct = (0, superstruct_1.object)({
/**
* The CAIP-2 chain ID to send the transaction on. Defaults to `eip155:1`.
*/
chainId: (0, superstruct_1.defaulted)((0, superstruct_1.string)(), 'eip155:1'),
/**
* The origin to send the transaction from. Defaults to `metamask.io`.
*/
origin: (0, superstruct_1.defaulted)((0, superstruct_1.string)(), 'metamask.io'),
/**
* The address to send the transaction from. Defaults to a randomly generated
* address.
*/
// TODO: Move this coercer to `@metamask/utils`.
from: (0, superstruct_1.coerce)(utils_1.StrictHexStruct, (0, superstruct_1.optional)(BytesLikeStruct), (value) => {
if (value) {
return (0, utils_1.bytesToHex)((0, utils_1.valueToBytes)(value));
}
return (0, utils_1.bytesToHex)((0, crypto_1.randomBytes)(20));
}),
/**
* The address to send the transaction to. Defaults to a randomly generated
* address.
*/
// TODO: Move this coercer to `@metamask/utils`.
to: (0, superstruct_1.coerce)(utils_1.StrictHexStruct, (0, superstruct_1.optional)(BytesLikeStruct), (value) => {
if (value) {
return (0, utils_1.bytesToHex)((0, utils_1.valueToBytes)(value));
}
return (0, utils_1.bytesToHex)((0, crypto_1.randomBytes)(20));
}),
/**
* The value to send with the transaction. The value may be specified as a
* `number`, `bigint`, `string`, or `Uint8Array`. Defaults to `0`.
*/
value: (0, superstruct_1.defaulted)((0, superstruct_1.coerce)(utils_1.StrictHexStruct, BytesLikeStruct, (value) => (0, utils_1.bytesToHex)((0, utils_1.valueToBytes)(value))), '0x0'),
/**
* The gas limit to use for the transaction. The gas limit may be specified
* as a `number`, `bigint`, `string`, or `Uint8Array`. Defaults to `21_000`.
*/
gasLimit: (0, superstruct_1.defaulted)((0, superstruct_1.coerce)(utils_1.StrictHexStruct, BytesLikeStruct, (value) => (0, utils_1.bytesToHex)((0, utils_1.valueToBytes)(value))), (0, utils_1.valueToBytes)(21000)),
/**
* The max fee per gas (in Wei) to use for the transaction. The max fee per
* gas may be specified as a `number`, `bigint`, `string`, or `Uint8Array`.
* Defaults to `1`.
*/
maxFeePerGas: (0, superstruct_1.defaulted)((0, superstruct_1.coerce)(utils_1.StrictHexStruct, BytesLikeStruct, (value) => (0, utils_1.bytesToHex)((0, utils_1.valueToBytes)(value))), (0, utils_1.valueToBytes)(1)),
/**
* The max priority fee per gas (in Wei) to use for the transaction. The max
* priority fee per gas may be specified as a `number`, `bigint`, `string`,
* or `Uint8Array`. Defaults to `1`.
*/
maxPriorityFeePerGas: (0, superstruct_1.defaulted)((0, superstruct_1.coerce)(utils_1.StrictHexStruct, BytesLikeStruct, (value) => (0, utils_1.bytesToHex)((0, utils_1.valueToBytes)(value))), (0, utils_1.valueToBytes)(1)),
/**
* The nonce to use for the transaction. The nonce may be specified as a
* `number`, `bigint`, `string`, or `Uint8Array`. Defaults to `0`.
*/
nonce: (0, superstruct_1.defaulted)((0, superstruct_1.coerce)(utils_1.StrictHexStruct, BytesLikeStruct, (value) => (0, utils_1.bytesToHex)((0, utils_1.valueToBytes)(value))), (0, utils_1.valueToBytes)(0)),
/**
* The data to send with the transaction. The data may be specified as a
* `number`, `bigint`, `string`, or `Uint8Array`. Defaults to `0x`.
*/
data: (0, superstruct_1.defaulted)((0, superstruct_1.coerce)((0, superstruct_1.union)([utils_1.StrictHexStruct, (0, superstruct_1.literal)('0x')]), BytesLikeStruct, (value) => (0, utils_1.bytesToHex)((0, utils_1.valueToBytes)(value))), '0x'),
});
exports.SignatureOptionsStruct = (0, superstruct_1.object)({
/**
* The origin making the signature request.
*/
origin: (0, superstruct_1.defaulted)((0, superstruct_1.string)(), 'metamask.io'),
/**
* The address signing the signature request. Defaults to a randomly generated
* address.
*/
from: (0, superstruct_1.coerce)(utils_1.StrictHexStruct, (0, superstruct_1.optional)(BytesLikeStruct), (value) => {
if (value) {
return (0, utils_1.bytesToHex)((0, utils_1.valueToBytes)(value));
}
return (0, utils_1.bytesToHex)((0, crypto_1.randomBytes)(20));
}),
/**
* The data to send with the transaction. The data may be specified as a
* `string`, an object, or an array of objects. This covers the data types
* for the supported signature methods. Defaults to `0x`.
*/
data: (0, superstruct_1.defaulted)((0, superstruct_1.union)([
utils_1.StrictHexStruct,
(0, superstruct_1.literal)('0x'),
(0, superstruct_1.record)((0, superstruct_1.string)(), (0, superstruct_1.any)()),
(0, superstruct_1.array)((0, superstruct_1.record)((0, superstruct_1.string)(), (0, superstruct_1.any)())),
]), '0x'),
/**
* The signature method being used.
*/
signatureMethod: (0, superstruct_1.defaulted)((0, superstruct_1.union)([
(0, superstruct_1.literal)('personal_sign'),
(0, superstruct_1.literal)('eth_signTypedData'),
(0, superstruct_1.literal)('eth_signTypedData_v3'),
(0, superstruct_1.literal)('eth_signTypedData_v4'),
]), 'personal_sign'),
});
exports.BaseNameLookupOptionsStruct = (0, superstruct_1.object)({
/**
* The CAIP-2 chain ID. Defaults to `eip155:1`.
*/
chainId: (0, superstruct_1.defaulted)((0, superstruct_1.string)(), 'eip155:1'),
});
exports.NameLookupOptionsStruct = (0, superstruct_1.union)([
(0, superstruct_1.assign)(exports.BaseNameLookupOptionsStruct, (0, superstruct_1.object)({
/**
* Address to lookup.
*/
address: (0, superstruct_1.string)(),
})),
(0, superstruct_1.assign)(exports.BaseNameLookupOptionsStruct, (0, superstruct_1.object)({
/**
* Domain name to lookup.
*/
domain: (0, superstruct_1.string)(),
})),
]);
exports.SnapOptionsStruct = (0, superstruct_1.object)({
/**
* The timeout in milliseconds to use for requests to the snap. Defaults to
* `1000`.
*/
timeout: (0, superstruct_1.defaulted)((0, superstruct_1.optional)((0, superstruct_1.number)()), 1000),
});
exports.JsonRpcMockOptionsStruct = (0, superstruct_1.object)({
method: (0, superstruct_1.string)(),
result: utils_1.JsonStruct,
});
exports.InterfaceStruct = (0, superstruct_1.type)({
content: (0, superstruct_1.optional)(jsx_1.JSXElementStruct),
});
exports.SnapResponseWithoutInterfaceStruct = (0, superstruct_1.object)({
id: (0, superstruct_1.string)(),
response: (0, superstruct_1.union)([
(0, superstruct_1.object)({
result: utils_1.JsonStruct,
}),
(0, superstruct_1.object)({
error: utils_1.JsonStruct,
}),
]),
notifications: (0, superstruct_1.array)((0, superstruct_1.object)({
id: (0, superstruct_1.string)(),
message: (0, superstruct_1.string)(),
type: (0, superstruct_1.union)([
(0, snaps_sdk_1.enumValue)(snaps_sdk_1.NotificationType.InApp),
(0, snaps_sdk_1.enumValue)(snaps_sdk_1.NotificationType.Native),
]),
title: (0, superstruct_1.optional)((0, superstruct_1.string)()),
content: (0, superstruct_1.optional)((0, superstruct_1.string)()),
footerLink: (0, superstruct_1.optional)((0, superstruct_1.object)({
href: (0, superstruct_1.string)(),
text: (0, superstruct_1.string)(),
})),
})),
tracked: (0, superstruct_1.object)({
errors: (0, superstruct_1.array)(snaps_utils_1.TrackableErrorStruct),
events: (0, superstruct_1.array)((0, superstruct_1.object)({
event: (0, superstruct_1.string)(),
properties: (0, superstruct_1.optional)((0, superstruct_1.record)((0, superstruct_1.string)(), utils_1.JsonStruct)),
sensitiveProperties: (0, superstruct_1.optional)((0, superstruct_1.record)((0, superstruct_1.string)(), utils_1.JsonStruct)),
})),
traces: (0, superstruct_1.array)((0, superstruct_1.object)({
id: (0, superstruct_1.optional)((0, superstruct_1.string)()),
name: (0, superstruct_1.string)(),
parentContext: (0, superstruct_1.optional)(utils_1.JsonStruct),
startTime: (0, superstruct_1.optional)((0, superstruct_1.number)()),
data: (0, superstruct_1.optional)((0, superstruct_1.record)((0, superstruct_1.string)(), (0, superstruct_1.union)([(0, superstruct_1.string)(), (0, superstruct_1.number)(), (0, superstruct_1.boolean)()]))),
tags: (0, superstruct_1.optional)((0, superstruct_1.record)((0, superstruct_1.string)(), (0, superstruct_1.union)([(0, superstruct_1.string)(), (0, superstruct_1.number)(), (0, superstruct_1.boolean)()]))),
})),
}),
});
exports.SnapResponseWithInterfaceStruct = (0, superstruct_1.assign)(exports.SnapResponseWithoutInterfaceStruct, (0, superstruct_1.object)({
getInterface: (0, superstruct_1.func)(),
}));
exports.SnapResponseStruct = (0, superstruct_1.union)([
exports.SnapResponseWithoutInterfaceStruct,
exports.SnapResponseWithInterfaceStruct,
]);
/**
* Ensure that the actual response contains `getInterface`.
*
* @param response - The response of the handler.
*/
function assertIsResponseWithInterface(response) {
(0, utils_1.assertStruct)(response, exports.SnapResponseWithInterfaceStruct);
}
exports.assertIsResponseWithInterface = assertIsResponseWithInterface;
//# sourceMappingURL=structs.cjs.map