UNPKG

@metamask/snaps-simulation

Version:

A simulation framework for MetaMask Snaps, enabling headless testing of Snaps in a controlled environment

1 lines 13.9 kB
{"version":3,"file":"structs.cjs","sourceRoot":"","sources":["../src/structs.ts"],"names":[],"mappings":";;;AAAA,mDAAkE;AAClE,iDAA2D;AAC3D,uDAA6D;AAC7D,uDAkB+B;AAC/B,2CAMyB;AACzB,mCAAqC;AAIrC,oDAAoD;AACpD,MAAM,eAAe,GAAG,IAAA,mBAAK,EAAC;IAC5B,IAAA,oBAAM,GAAE;IACR,IAAA,oBAAM,GAAE;IACR,IAAA,oBAAM,GAAE;IACR,IAAA,sBAAQ,EAAC,UAAU,CAAC;CACrB,CAAC,CAAC;AAEU,QAAA,wBAAwB,GAAG,IAAA,oBAAM,EAAC;IAC7C;;OAEG;IACH,OAAO,EAAE,IAAA,uBAAS,EAAC,IAAA,oBAAM,GAAE,EAAE,UAAU,CAAC;IAExC;;OAEG;IACH,MAAM,EAAE,IAAA,uBAAS,EAAC,IAAA,oBAAM,GAAE,EAAE,aAAa,CAAC;IAE1C;;;OAGG;IACH,gDAAgD;IAChD,IAAI,EAAE,IAAA,oBAAM,EAAC,uBAAe,EAAE,IAAA,sBAAQ,EAAC,eAAe,CAAC,EAAE,CAAC,KAAK,EAAE,EAAE;QACjE,IAAI,KAAK,EAAE,CAAC;YACV,OAAO,IAAA,kBAAU,EAAC,IAAA,oBAAY,EAAC,KAAK,CAAC,CAAC,CAAC;QACzC,CAAC;QAED,OAAO,IAAA,kBAAU,EAAC,IAAA,oBAAW,EAAC,EAAE,CAAC,CAAC,CAAC;IACrC,CAAC,CAAC;IAEF;;;OAGG;IACH,gDAAgD;IAChD,EAAE,EAAE,IAAA,oBAAM,EAAC,uBAAe,EAAE,IAAA,sBAAQ,EAAC,eAAe,CAAC,EAAE,CAAC,KAAK,EAAE,EAAE;QAC/D,IAAI,KAAK,EAAE,CAAC;YACV,OAAO,IAAA,kBAAU,EAAC,IAAA,oBAAY,EAAC,KAAK,CAAC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,IAAA,sBAAQ,EAAC,IAAA,oBAAM,GAAE,CAAC;YAC7B,IAAI,EAAE,IAAA,sBAAQ,EACZ,IAAA,oBAAM,EAAC,IAAA,oBAAM,GAAE,EAAE,IAAA,mBAAK,EAAC,CAAC,IAAA,oBAAM,GAAE,EAAE,IAAA,oBAAM,GAAE,EAAE,IAAA,qBAAO,GAAE,CAAC,CAAC,CAAC,CACzD;YACD,IAAI,EAAE,IAAA,sBAAQ,EACZ,IAAA,oBAAM,EAAC,IAAA,oBAAM,GAAE,EAAE,IAAA,mBAAK,EAAC,CAAC,IAAA,oBAAM,GAAE,EAAE,IAAA,oBAAM,GAAE,EAAE,IAAA,qBAAO,GAAE,CAAC,CAAC,CAAC,CACzD;SACF,CAAC,CACH;KACF,CAAC;CACH,CAAC,CAAC;AAEU,QAAA,+BAA+B,GAAG,IAAA,oBAAM,EACnD,0CAAkC,EAClC,IAAA,oBAAM,EAAC;IACL,YAAY,EAAE,IAAA,kBAAI,GAAE;CACrB,CAAC,CACH,CAAC;AAEW,QAAA,kBAAkB,GAAG,IAAA,mBAAK,EAAC;IACtC,0CAAkC;IAClC,uCAA+B;CAChC,CAAC,CAAC;AAEH;;;;GAIG;AACH,SAAgB,6BAA6B,CAC3C,QAAsB;IAEtB,IAAA,oBAAY,EAAC,QAAQ,EAAE,uCAA+B,CAAC,CAAC;AAC1D,CAAC;AAJD,sEAIC","sourcesContent":["import { NotificationType, enumValue } from '@metamask/snaps-sdk';\nimport { JSXElementStruct } from '@metamask/snaps-sdk/jsx';\nimport { TrackableErrorStruct } from '@metamask/snaps-utils';\nimport {\n array,\n assign,\n bigint,\n coerce,\n defaulted,\n instance,\n literal,\n number,\n object,\n optional,\n string,\n union,\n record,\n any,\n func,\n type,\n boolean,\n} from '@metamask/superstruct';\nimport {\n assertStruct,\n bytesToHex,\n JsonStruct,\n StrictHexStruct,\n valueToBytes,\n} from '@metamask/utils';\nimport { randomBytes } from 'crypto';\n\nimport type { SnapResponse, SnapResponseWithInterface } from './types';\n\n// TODO: Export this from `@metamask/utils` instead.\nconst BytesLikeStruct = union([\n bigint(),\n number(),\n string(),\n instance(Uint8Array),\n]);\n\nexport const TransactionOptionsStruct = object({\n /**\n * The CAIP-2 chain ID to send the transaction on. Defaults to `eip155:1`.\n */\n chainId: defaulted(string(), 'eip155:1'),\n\n /**\n * The origin to send the transaction from. Defaults to `metamask.io`.\n */\n origin: defaulted(string(), 'metamask.io'),\n\n /**\n * The address to send the transaction from. Defaults to a randomly generated\n * address.\n */\n // TODO: Move this coercer to `@metamask/utils`.\n from: coerce(StrictHexStruct, optional(BytesLikeStruct), (value) => {\n if (value) {\n return bytesToHex(valueToBytes(value));\n }\n\n return bytesToHex(randomBytes(20));\n }),\n\n /**\n * The address to send the transaction to. Defaults to a randomly generated\n * address.\n */\n // TODO: Move this coercer to `@metamask/utils`.\n to: coerce(StrictHexStruct, optional(BytesLikeStruct), (value) => {\n if (value) {\n return bytesToHex(valueToBytes(value));\n }\n\n return bytesToHex(randomBytes(20));\n }),\n\n /**\n * The value to send with the transaction. The value may be specified as a\n * `number`, `bigint`, `string`, or `Uint8Array`. Defaults to `0`.\n */\n value: defaulted(\n coerce(StrictHexStruct, BytesLikeStruct, (value) =>\n bytesToHex(valueToBytes(value)),\n ),\n '0x0',\n ),\n\n /**\n * The gas limit to use for the transaction. The gas limit may be specified\n * as a `number`, `bigint`, `string`, or `Uint8Array`. Defaults to `21_000`.\n */\n gasLimit: defaulted(\n coerce(StrictHexStruct, BytesLikeStruct, (value) =>\n bytesToHex(valueToBytes(value)),\n ),\n valueToBytes(21_000),\n ),\n\n /**\n * The max fee per gas (in Wei) to use for the transaction. The max fee per\n * gas may be specified as a `number`, `bigint`, `string`, or `Uint8Array`.\n * Defaults to `1`.\n */\n maxFeePerGas: defaulted(\n coerce(StrictHexStruct, BytesLikeStruct, (value) =>\n bytesToHex(valueToBytes(value)),\n ),\n valueToBytes(1),\n ),\n\n /**\n * The max priority fee per gas (in Wei) to use for the transaction. The max\n * priority fee per gas may be specified as a `number`, `bigint`, `string`,\n * or `Uint8Array`. Defaults to `1`.\n */\n maxPriorityFeePerGas: defaulted(\n coerce(StrictHexStruct, BytesLikeStruct, (value) =>\n bytesToHex(valueToBytes(value)),\n ),\n valueToBytes(1),\n ),\n\n /**\n * The nonce to use for the transaction. The nonce may be specified as a\n * `number`, `bigint`, `string`, or `Uint8Array`. Defaults to `0`.\n */\n nonce: defaulted(\n coerce(StrictHexStruct, BytesLikeStruct, (value) =>\n bytesToHex(valueToBytes(value)),\n ),\n valueToBytes(0),\n ),\n\n /**\n * The data to send with the transaction. The data may be specified as a\n * `number`, `bigint`, `string`, or `Uint8Array`. Defaults to `0x`.\n */\n data: defaulted(\n coerce(union([StrictHexStruct, literal('0x')]), BytesLikeStruct, (value) =>\n bytesToHex(valueToBytes(value)),\n ),\n '0x',\n ),\n});\n\nexport const SignatureOptionsStruct = object({\n /**\n * The origin making the signature request.\n */\n origin: defaulted(string(), 'metamask.io'),\n\n /**\n * The address signing the signature request. Defaults to a randomly generated\n * address.\n */\n from: coerce(StrictHexStruct, optional(BytesLikeStruct), (value) => {\n if (value) {\n return bytesToHex(valueToBytes(value));\n }\n\n return bytesToHex(randomBytes(20));\n }),\n\n /**\n * The data to send with the transaction. The data may be specified as a\n * `string`, an object, or an array of objects. This covers the data types\n * for the supported signature methods. Defaults to `0x`.\n */\n data: defaulted(\n union([\n StrictHexStruct,\n literal('0x'),\n record(string(), any()),\n array(record(string(), any())),\n ]),\n '0x',\n ),\n\n /**\n * The signature method being used.\n */\n signatureMethod: defaulted(\n union([\n literal('personal_sign'),\n literal('eth_signTypedData'),\n literal('eth_signTypedData_v3'),\n literal('eth_signTypedData_v4'),\n ]),\n 'personal_sign',\n ),\n});\n\nexport const BaseNameLookupOptionsStruct = object({\n /**\n * The CAIP-2 chain ID. Defaults to `eip155:1`.\n */\n chainId: defaulted(string(), 'eip155:1'),\n});\n\nexport const NameLookupOptionsStruct = union([\n assign(\n BaseNameLookupOptionsStruct,\n object({\n /**\n * Address to lookup.\n */\n address: string(),\n }),\n ),\n assign(\n BaseNameLookupOptionsStruct,\n object({\n /**\n * Domain name to lookup.\n */\n domain: string(),\n }),\n ),\n]);\n\nexport const SnapOptionsStruct = object({\n /**\n * The timeout in milliseconds to use for requests to the snap. Defaults to\n * `1000`.\n */\n timeout: defaulted(optional(number()), 1000),\n});\n\nexport const JsonRpcMockOptionsStruct = object({\n method: string(),\n result: JsonStruct,\n});\n\nexport const InterfaceStruct = type({\n content: optional(JSXElementStruct),\n});\n\nexport const SnapResponseWithoutInterfaceStruct = object({\n id: string(),\n\n response: union([\n object({\n result: JsonStruct,\n }),\n object({\n error: JsonStruct,\n }),\n ]),\n\n notifications: array(\n object({\n id: string(),\n message: string(),\n type: union([\n enumValue(NotificationType.InApp),\n enumValue(NotificationType.Native),\n ]),\n title: optional(string()),\n content: optional(string()),\n footerLink: optional(\n object({\n href: string(),\n text: string(),\n }),\n ),\n }),\n ),\n\n tracked: object({\n errors: array(TrackableErrorStruct),\n\n events: array(\n object({\n event: string(),\n properties: optional(record(string(), JsonStruct)),\n sensitiveProperties: optional(record(string(), JsonStruct)),\n }),\n ),\n\n traces: array(\n object({\n id: optional(string()),\n name: string(),\n parentContext: optional(JsonStruct),\n startTime: optional(number()),\n data: optional(\n record(string(), union([string(), number(), boolean()])),\n ),\n tags: optional(\n record(string(), union([string(), number(), boolean()])),\n ),\n }),\n ),\n }),\n});\n\nexport const SnapResponseWithInterfaceStruct = assign(\n SnapResponseWithoutInterfaceStruct,\n object({\n getInterface: func(),\n }),\n);\n\nexport const SnapResponseStruct = union([\n SnapResponseWithoutInterfaceStruct,\n SnapResponseWithInterfaceStruct,\n]);\n\n/**\n * Ensure that the actual response contains `getInterface`.\n *\n * @param response - The response of the handler.\n */\nexport function assertIsResponseWithInterface(\n response: SnapResponse,\n): asserts response is SnapResponseWithInterface {\n assertStruct(response, SnapResponseWithInterfaceStruct);\n}\n"]}