@metamask/snaps-execution-environments 1.0.2 → 2.0.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +37 -278
- package/dist/browserify/iframe/bundle.js +6 -4
- package/dist/browserify/iframe/index.html +2 -0
- package/dist/browserify/node-process/bundle.js +8 -2
- package/dist/browserify/node-thread/bundle.js +8 -2
- package/dist/browserify/offscreen/bundle.js +6 -9
- package/dist/browserify/offscreen/index.html +2 -0
- package/dist/browserify/worker-executor/bundle.js +12157 -0
- package/dist/browserify/worker-pool/bundle.js +7 -0
- package/dist/browserify/worker-pool/index.html +12152 -0
- package/dist/cjs/common/BaseSnapExecutor.js +423 -0
- package/dist/cjs/common/BaseSnapExecutor.js.map +1 -0
- package/dist/cjs/common/commands.js +81 -0
- package/dist/cjs/common/commands.js.map +1 -0
- package/dist/cjs/common/endowments/commonEndowmentFactory.js +147 -0
- package/dist/cjs/common/endowments/commonEndowmentFactory.js.map +1 -0
- package/dist/cjs/common/endowments/console.js +134 -0
- package/dist/cjs/common/endowments/console.js.map +1 -0
- package/dist/cjs/common/endowments/crypto.js +44 -0
- package/dist/cjs/common/endowments/crypto.js.map +1 -0
- package/dist/cjs/common/endowments/date.js +53 -0
- package/dist/cjs/common/endowments/date.js.map +1 -0
- package/dist/cjs/common/endowments/index.js +91 -0
- package/dist/cjs/common/endowments/index.js.map +1 -0
- package/dist/cjs/common/endowments/interval.js +61 -0
- package/dist/cjs/common/endowments/interval.js.map +1 -0
- package/dist/cjs/common/endowments/math.js +67 -0
- package/dist/cjs/common/endowments/math.js.map +1 -0
- package/dist/cjs/common/endowments/network.js +216 -0
- package/dist/cjs/common/endowments/network.js.map +1 -0
- package/dist/cjs/common/endowments/textDecoder.js +28 -0
- package/dist/cjs/common/endowments/textDecoder.js.map +1 -0
- package/dist/cjs/common/endowments/textEncoder.js +28 -0
- package/dist/cjs/common/endowments/textEncoder.js.map +1 -0
- package/dist/cjs/common/endowments/timeout.js +63 -0
- package/dist/cjs/common/endowments/timeout.js.map +1 -0
- package/dist/cjs/common/globalEvents.js +39 -0
- package/dist/cjs/common/globalEvents.js.map +1 -0
- package/dist/cjs/common/globalObject.js +53 -0
- package/dist/{common → cjs/common}/globalObject.js.map +1 -1
- package/dist/cjs/common/lockdown/lockdown-events.js +74 -0
- package/dist/cjs/common/lockdown/lockdown-events.js.map +1 -0
- package/dist/cjs/common/lockdown/lockdown-more.js +73 -0
- package/dist/cjs/common/lockdown/lockdown-more.js.map +1 -0
- package/dist/cjs/common/lockdown/lockdown.js +30 -0
- package/dist/cjs/common/lockdown/lockdown.js.map +1 -0
- package/dist/cjs/common/sortParams.js +26 -0
- package/dist/cjs/common/sortParams.js.map +1 -0
- package/dist/cjs/common/utils.js +132 -0
- package/dist/cjs/common/utils.js.map +1 -0
- package/dist/cjs/common/validation.js +118 -0
- package/dist/cjs/common/validation.js.map +1 -0
- package/dist/cjs/iframe/IFrameSnapExecutor.js +50 -0
- package/dist/cjs/iframe/IFrameSnapExecutor.js.map +1 -0
- package/dist/cjs/iframe/index.js +13 -0
- package/dist/cjs/iframe/index.js.map +1 -0
- package/dist/cjs/logging.js +15 -0
- package/dist/cjs/logging.js.map +1 -0
- package/dist/cjs/node-process/ChildProcessSnapExecutor.js +39 -0
- package/dist/cjs/node-process/ChildProcessSnapExecutor.js.map +1 -0
- package/dist/cjs/node-process/index.js +11 -0
- package/dist/cjs/node-process/index.js.map +1 -0
- package/dist/cjs/node-thread/ThreadSnapExecutor.js +39 -0
- package/dist/cjs/node-thread/ThreadSnapExecutor.js.map +1 -0
- package/dist/cjs/node-thread/index.js +11 -0
- package/dist/cjs/node-thread/index.js.map +1 -0
- package/dist/cjs/offscreen/OffscreenSnapExecutor.js +173 -0
- package/dist/cjs/offscreen/OffscreenSnapExecutor.js.map +1 -0
- package/dist/cjs/offscreen/index.js +19 -0
- package/dist/cjs/offscreen/index.js.map +1 -0
- package/dist/cjs/webworker/executor/WebWorkerSnapExecutor.js +45 -0
- package/dist/cjs/webworker/executor/WebWorkerSnapExecutor.js.map +1 -0
- package/dist/cjs/webworker/executor/index.js +13 -0
- package/dist/cjs/webworker/executor/index.js.map +1 -0
- package/dist/cjs/webworker/pool/WebWorkerPool.js +248 -0
- package/dist/cjs/webworker/pool/WebWorkerPool.js.map +1 -0
- package/dist/cjs/webworker/pool/index.js +13 -0
- package/dist/cjs/webworker/pool/index.js.map +1 -0
- package/dist/esm/common/BaseSnapExecutor.js +408 -0
- package/dist/esm/common/BaseSnapExecutor.js.map +1 -0
- package/dist/esm/common/commands.js +80 -0
- package/dist/esm/common/commands.js.map +1 -0
- package/dist/esm/common/endowments/commonEndowmentFactory.js +132 -0
- package/dist/esm/common/endowments/commonEndowmentFactory.js.map +1 -0
- package/dist/esm/common/endowments/console.js +116 -0
- package/dist/esm/common/endowments/console.js.map +1 -0
- package/dist/esm/common/endowments/crypto.js +26 -0
- package/dist/esm/common/endowments/crypto.js.map +1 -0
- package/dist/{common → esm/common}/endowments/date.js +20 -16
- package/dist/esm/common/endowments/date.js.map +1 -0
- package/dist/{common → esm/common}/endowments/index.js +34 -36
- package/dist/esm/common/endowments/index.js.map +1 -0
- package/dist/{common → esm/common}/endowments/interval.js +13 -12
- package/dist/esm/common/endowments/interval.js.map +1 -0
- package/dist/{common → esm/common}/endowments/math.js +21 -15
- package/dist/esm/common/endowments/math.js.map +1 -0
- package/dist/esm/common/endowments/network.js +206 -0
- package/dist/esm/common/endowments/network.js.map +1 -0
- package/dist/esm/common/endowments/textDecoder.js +18 -0
- package/dist/esm/common/endowments/textDecoder.js.map +1 -0
- package/dist/esm/common/endowments/textEncoder.js +18 -0
- package/dist/esm/common/endowments/textEncoder.js.map +1 -0
- package/dist/{common → esm/common}/endowments/timeout.js +14 -13
- package/dist/esm/common/endowments/timeout.js.map +1 -0
- package/dist/esm/common/globalEvents.js +35 -0
- package/dist/esm/common/globalEvents.js.map +1 -0
- package/dist/esm/common/globalObject.js +36 -0
- package/dist/esm/common/globalObject.js.map +1 -0
- package/dist/esm/common/lockdown/lockdown-events.js +66 -0
- package/dist/esm/common/lockdown/lockdown-events.js.map +1 -0
- package/dist/{common → esm/common}/lockdown/lockdown-more.js +15 -22
- package/dist/esm/common/lockdown/lockdown-more.js.map +1 -0
- package/dist/{common → esm/common}/lockdown/lockdown.js +6 -11
- package/dist/esm/common/lockdown/lockdown.js.map +1 -0
- package/dist/{common → esm/common}/sortParams.js +7 -13
- package/dist/esm/common/sortParams.js.map +1 -0
- package/dist/{common → esm/common}/utils.js +43 -56
- package/dist/esm/common/utils.js.map +1 -0
- package/dist/esm/common/validation.js +94 -0
- package/dist/esm/common/validation.js.map +1 -0
- package/dist/esm/iframe/IFrameSnapExecutor.js +35 -0
- package/dist/esm/iframe/IFrameSnapExecutor.js.map +1 -0
- package/dist/esm/iframe/index.js +9 -0
- package/dist/esm/iframe/index.js.map +1 -0
- package/dist/esm/logging.js +10 -0
- package/dist/esm/logging.js.map +1 -0
- package/dist/esm/node-process/ChildProcessSnapExecutor.js +24 -0
- package/dist/esm/node-process/ChildProcessSnapExecutor.js.map +1 -0
- package/dist/esm/node-process/index.js +7 -0
- package/dist/esm/node-process/index.js.map +1 -0
- package/dist/esm/node-thread/ThreadSnapExecutor.js +24 -0
- package/dist/esm/node-thread/ThreadSnapExecutor.js.map +1 -0
- package/dist/esm/node-thread/index.js +7 -0
- package/dist/esm/node-thread/index.js.map +1 -0
- package/dist/esm/offscreen/OffscreenSnapExecutor.js +176 -0
- package/dist/esm/offscreen/OffscreenSnapExecutor.js.map +1 -0
- package/dist/esm/offscreen/index.js +15 -0
- package/dist/esm/offscreen/index.js.map +1 -0
- package/dist/esm/webworker/executor/WebWorkerSnapExecutor.js +30 -0
- package/dist/esm/webworker/executor/WebWorkerSnapExecutor.js.map +1 -0
- package/dist/esm/webworker/executor/index.js +9 -0
- package/dist/esm/webworker/executor/index.js.map +1 -0
- package/dist/esm/webworker/pool/WebWorkerPool.js +245 -0
- package/dist/esm/webworker/pool/WebWorkerPool.js.map +1 -0
- package/dist/esm/webworker/pool/index.js +9 -0
- package/dist/esm/webworker/pool/index.js.map +1 -0
- package/dist/{common → types/common}/BaseSnapExecutor.d.ts +7 -7
- package/dist/{common → types/common}/commands.d.ts +2 -2
- package/dist/{common → types/common}/endowments/commonEndowmentFactory.d.ts +5 -1
- package/dist/types/common/endowments/console.d.ts +45 -0
- package/dist/{common → types/common}/endowments/crypto.d.ts +4 -0
- package/dist/{common → types/common}/endowments/index.d.ts +5 -3
- package/dist/{common → types/common}/globalEvents.d.ts +1 -0
- package/dist/{common → types/common}/sortParams.d.ts +1 -1
- package/dist/{common → types/common}/utils.d.ts +5 -4
- package/dist/{common → types/common}/validation.d.ts +26 -10
- package/dist/{iframe → types/iframe}/IFrameSnapExecutor.d.ts +1 -1
- package/dist/{offscreen → types/offscreen}/OffscreenSnapExecutor.d.ts +2 -1
- package/dist/types/webworker/executor/WebWorkerSnapExecutor.d.ts +13 -0
- package/dist/types/webworker/executor/index.d.ts +1 -0
- package/dist/types/webworker/pool/WebWorkerPool.d.ts +23 -0
- package/dist/types/webworker/pool/index.d.ts +1 -0
- package/package.json +52 -41
- package/dist/common/BaseSnapExecutor.js +0 -370
- package/dist/common/BaseSnapExecutor.js.map +0 -1
- package/dist/common/commands.js +0 -65
- package/dist/common/commands.js.map +0 -1
- package/dist/common/endowments/commonEndowmentFactory.js +0 -75
- package/dist/common/endowments/commonEndowmentFactory.js.map +0 -1
- package/dist/common/endowments/crypto.js +0 -28
- package/dist/common/endowments/crypto.js.map +0 -1
- package/dist/common/endowments/date.js.map +0 -1
- package/dist/common/endowments/index.js.map +0 -1
- package/dist/common/endowments/interval.js.map +0 -1
- package/dist/common/endowments/math.js.map +0 -1
- package/dist/common/endowments/network.js +0 -177
- package/dist/common/endowments/network.js.map +0 -1
- package/dist/common/endowments/textDecoder.js +0 -18
- package/dist/common/endowments/textDecoder.js.map +0 -1
- package/dist/common/endowments/textEncoder.js +0 -18
- package/dist/common/endowments/textEncoder.js.map +0 -1
- package/dist/common/endowments/timeout.js.map +0 -1
- package/dist/common/globalEvents.js +0 -47
- package/dist/common/globalEvents.js.map +0 -1
- package/dist/common/globalObject.js +0 -50
- package/dist/common/keyring.d.ts +0 -12
- package/dist/common/keyring.js +0 -42
- package/dist/common/keyring.js.map +0 -1
- package/dist/common/lockdown/lockdown-events.js +0 -60
- package/dist/common/lockdown/lockdown-events.js.map +0 -1
- package/dist/common/lockdown/lockdown-more.js.map +0 -1
- package/dist/common/lockdown/lockdown.js.map +0 -1
- package/dist/common/sortParams.js.map +0 -1
- package/dist/common/utils.js.map +0 -1
- package/dist/common/validation.js +0 -109
- package/dist/common/validation.js.map +0 -1
- package/dist/iframe/IFrameSnapExecutor.js +0 -42
- package/dist/iframe/IFrameSnapExecutor.js.map +0 -1
- package/dist/iframe/index.js +0 -10
- package/dist/iframe/index.js.map +0 -1
- package/dist/logging.js +0 -13
- package/dist/logging.js.map +0 -1
- package/dist/node-process/ChildProcessSnapExecutor.js +0 -30
- package/dist/node-process/ChildProcessSnapExecutor.js.map +0 -1
- package/dist/node-process/index.js +0 -8
- package/dist/node-process/index.js.map +0 -1
- package/dist/node-thread/ThreadSnapExecutor.js +0 -30
- package/dist/node-thread/ThreadSnapExecutor.js.map +0 -1
- package/dist/node-thread/index.js +0 -8
- package/dist/node-thread/index.js.map +0 -1
- package/dist/offscreen/OffscreenSnapExecutor.js +0 -104
- package/dist/offscreen/OffscreenSnapExecutor.js.map +0 -1
- package/dist/offscreen/index.js +0 -16
- package/dist/offscreen/index.js.map +0 -1
- package/dist/openrpc.json +0 -210
- /package/dist/{common → types/common}/endowments/date.d.ts +0 -0
- /package/dist/{common → types/common}/endowments/interval.d.ts +0 -0
- /package/dist/{common → types/common}/endowments/math.d.ts +0 -0
- /package/dist/{common → types/common}/endowments/network.d.ts +0 -0
- /package/dist/{common → types/common}/endowments/textDecoder.d.ts +0 -0
- /package/dist/{common → types/common}/endowments/textEncoder.d.ts +0 -0
- /package/dist/{common → types/common}/endowments/timeout.d.ts +0 -0
- /package/dist/{common → types/common}/globalObject.d.ts +0 -0
- /package/dist/{common → types/common}/lockdown/lockdown-events.d.ts +0 -0
- /package/dist/{common → types/common}/lockdown/lockdown-more.d.ts +0 -0
- /package/dist/{common → types/common}/lockdown/lockdown.d.ts +0 -0
- /package/dist/{iframe → types/iframe}/index.d.ts +0 -0
- /package/dist/{logging.d.ts → types/logging.d.ts} +0 -0
- /package/dist/{node-process → types/node-process}/ChildProcessSnapExecutor.d.ts +0 -0
- /package/dist/{node-process → types/node-process}/index.d.ts +0 -0
- /package/dist/{node-thread → types/node-thread}/ThreadSnapExecutor.d.ts +0 -0
- /package/dist/{node-thread → types/node-thread}/index.d.ts +0 -0
- /package/dist/{offscreen → types/offscreen}/index.d.ts +0 -0
|
@@ -0,0 +1,132 @@
|
|
|
1
|
+
import { rootRealmGlobal } from '../globalObject';
|
|
2
|
+
import consoleEndowment from './console';
|
|
3
|
+
import crypto from './crypto';
|
|
4
|
+
import date from './date';
|
|
5
|
+
import interval from './interval';
|
|
6
|
+
import math from './math';
|
|
7
|
+
import network from './network';
|
|
8
|
+
import textDecoder from './textDecoder';
|
|
9
|
+
import textEncoder from './textEncoder';
|
|
10
|
+
import timeout from './timeout';
|
|
11
|
+
// Array of common endowments
|
|
12
|
+
const commonEndowments = [
|
|
13
|
+
{
|
|
14
|
+
endowment: AbortController,
|
|
15
|
+
name: 'AbortController'
|
|
16
|
+
},
|
|
17
|
+
{
|
|
18
|
+
endowment: AbortSignal,
|
|
19
|
+
name: 'AbortSignal'
|
|
20
|
+
},
|
|
21
|
+
{
|
|
22
|
+
endowment: ArrayBuffer,
|
|
23
|
+
name: 'ArrayBuffer'
|
|
24
|
+
},
|
|
25
|
+
{
|
|
26
|
+
endowment: atob,
|
|
27
|
+
name: 'atob',
|
|
28
|
+
bind: true
|
|
29
|
+
},
|
|
30
|
+
{
|
|
31
|
+
endowment: BigInt,
|
|
32
|
+
name: 'BigInt'
|
|
33
|
+
},
|
|
34
|
+
{
|
|
35
|
+
endowment: BigInt64Array,
|
|
36
|
+
name: 'BigInt64Array'
|
|
37
|
+
},
|
|
38
|
+
{
|
|
39
|
+
endowment: BigUint64Array,
|
|
40
|
+
name: 'BigUint64Array'
|
|
41
|
+
},
|
|
42
|
+
{
|
|
43
|
+
endowment: btoa,
|
|
44
|
+
name: 'btoa',
|
|
45
|
+
bind: true
|
|
46
|
+
},
|
|
47
|
+
{
|
|
48
|
+
endowment: DataView,
|
|
49
|
+
name: 'DataView'
|
|
50
|
+
},
|
|
51
|
+
{
|
|
52
|
+
endowment: Float32Array,
|
|
53
|
+
name: 'Float32Array'
|
|
54
|
+
},
|
|
55
|
+
{
|
|
56
|
+
endowment: Float64Array,
|
|
57
|
+
name: 'Float64Array'
|
|
58
|
+
},
|
|
59
|
+
{
|
|
60
|
+
endowment: Int8Array,
|
|
61
|
+
name: 'Int8Array'
|
|
62
|
+
},
|
|
63
|
+
{
|
|
64
|
+
endowment: Int16Array,
|
|
65
|
+
name: 'Int16Array'
|
|
66
|
+
},
|
|
67
|
+
{
|
|
68
|
+
endowment: Int32Array,
|
|
69
|
+
name: 'Int32Array'
|
|
70
|
+
},
|
|
71
|
+
{
|
|
72
|
+
endowment: Uint8Array,
|
|
73
|
+
name: 'Uint8Array'
|
|
74
|
+
},
|
|
75
|
+
{
|
|
76
|
+
endowment: Uint8ClampedArray,
|
|
77
|
+
name: 'Uint8ClampedArray'
|
|
78
|
+
},
|
|
79
|
+
{
|
|
80
|
+
endowment: Uint16Array,
|
|
81
|
+
name: 'Uint16Array'
|
|
82
|
+
},
|
|
83
|
+
{
|
|
84
|
+
endowment: Uint32Array,
|
|
85
|
+
name: 'Uint32Array'
|
|
86
|
+
},
|
|
87
|
+
{
|
|
88
|
+
endowment: URL,
|
|
89
|
+
name: 'URL'
|
|
90
|
+
},
|
|
91
|
+
{
|
|
92
|
+
endowment: WebAssembly,
|
|
93
|
+
name: 'WebAssembly'
|
|
94
|
+
}
|
|
95
|
+
];
|
|
96
|
+
/**
|
|
97
|
+
* Creates a consolidated collection of common endowments.
|
|
98
|
+
* This function will return factories for all common endowments including
|
|
99
|
+
* the additionally attenuated. All hardened with SES.
|
|
100
|
+
*
|
|
101
|
+
* @returns An object with common endowments.
|
|
102
|
+
*/ const buildCommonEndowments = ()=>{
|
|
103
|
+
const endowmentFactories = [
|
|
104
|
+
crypto,
|
|
105
|
+
interval,
|
|
106
|
+
math,
|
|
107
|
+
network,
|
|
108
|
+
timeout,
|
|
109
|
+
textDecoder,
|
|
110
|
+
textEncoder,
|
|
111
|
+
date,
|
|
112
|
+
consoleEndowment
|
|
113
|
+
];
|
|
114
|
+
commonEndowments.forEach((endowmentSpecification)=>{
|
|
115
|
+
const endowment = {
|
|
116
|
+
names: [
|
|
117
|
+
endowmentSpecification.name
|
|
118
|
+
],
|
|
119
|
+
factory: ()=>{
|
|
120
|
+
const boundEndowment = typeof endowmentSpecification.endowment === 'function' && endowmentSpecification.bind ? endowmentSpecification.endowment.bind(rootRealmGlobal) : endowmentSpecification.endowment;
|
|
121
|
+
return {
|
|
122
|
+
[endowmentSpecification.name]: harden(boundEndowment)
|
|
123
|
+
};
|
|
124
|
+
}
|
|
125
|
+
};
|
|
126
|
+
endowmentFactories.push(endowment);
|
|
127
|
+
});
|
|
128
|
+
return endowmentFactories;
|
|
129
|
+
};
|
|
130
|
+
export default buildCommonEndowments;
|
|
131
|
+
|
|
132
|
+
//# sourceMappingURL=commonEndowmentFactory.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["../../../../src/common/endowments/commonEndowmentFactory.ts"],"sourcesContent":["import type { SnapId } from '@metamask/snaps-utils';\n\nimport { rootRealmGlobal } from '../globalObject';\nimport consoleEndowment from './console';\nimport crypto from './crypto';\nimport date from './date';\nimport interval from './interval';\nimport math from './math';\nimport network from './network';\nimport textDecoder from './textDecoder';\nimport textEncoder from './textEncoder';\nimport timeout from './timeout';\n\nexport type EndowmentFactoryOptions = {\n snapId?: SnapId;\n};\n\nexport type EndowmentFactory = {\n names: readonly string[];\n factory: (options?: EndowmentFactoryOptions) => { [key: string]: unknown };\n};\n\nexport type CommonEndowmentSpecification = {\n endowment: unknown;\n name: string;\n bind?: boolean;\n};\n\n// Array of common endowments\nconst commonEndowments: CommonEndowmentSpecification[] = [\n { endowment: AbortController, name: 'AbortController' },\n { endowment: AbortSignal, name: 'AbortSignal' },\n { endowment: ArrayBuffer, name: 'ArrayBuffer' },\n { endowment: atob, name: 'atob', bind: true },\n { endowment: BigInt, name: 'BigInt' },\n { endowment: BigInt64Array, name: 'BigInt64Array' },\n { endowment: BigUint64Array, name: 'BigUint64Array' },\n { endowment: btoa, name: 'btoa', bind: true },\n { endowment: DataView, name: 'DataView' },\n { endowment: Float32Array, name: 'Float32Array' },\n { endowment: Float64Array, name: 'Float64Array' },\n { endowment: Int8Array, name: 'Int8Array' },\n { endowment: Int16Array, name: 'Int16Array' },\n { endowment: Int32Array, name: 'Int32Array' },\n { endowment: Uint8Array, name: 'Uint8Array' },\n { endowment: Uint8ClampedArray, name: 'Uint8ClampedArray' },\n { endowment: Uint16Array, name: 'Uint16Array' },\n { endowment: Uint32Array, name: 'Uint32Array' },\n { endowment: URL, name: 'URL' },\n { endowment: WebAssembly, name: 'WebAssembly' },\n];\n\n/**\n * Creates a consolidated collection of common endowments.\n * This function will return factories for all common endowments including\n * the additionally attenuated. All hardened with SES.\n *\n * @returns An object with common endowments.\n */\nconst buildCommonEndowments = (): EndowmentFactory[] => {\n const endowmentFactories: EndowmentFactory[] = [\n crypto,\n interval,\n math,\n network,\n timeout,\n textDecoder,\n textEncoder,\n date,\n consoleEndowment,\n ];\n\n commonEndowments.forEach((endowmentSpecification) => {\n const endowment = {\n names: [endowmentSpecification.name] as const,\n factory: () => {\n const boundEndowment =\n typeof endowmentSpecification.endowment === 'function' &&\n endowmentSpecification.bind\n ? endowmentSpecification.endowment.bind(rootRealmGlobal)\n : endowmentSpecification.endowment;\n return {\n [endowmentSpecification.name]: harden(boundEndowment),\n } as const;\n },\n };\n endowmentFactories.push(endowment);\n });\n\n return endowmentFactories;\n};\n\nexport default buildCommonEndowments;\n"],"names":["rootRealmGlobal","consoleEndowment","crypto","date","interval","math","network","textDecoder","textEncoder","timeout","commonEndowments","endowment","AbortController","name","AbortSignal","ArrayBuffer","atob","bind","BigInt","BigInt64Array","BigUint64Array","btoa","DataView","Float32Array","Float64Array","Int8Array","Int16Array","Int32Array","Uint8Array","Uint8ClampedArray","Uint16Array","Uint32Array","URL","WebAssembly","buildCommonEndowments","endowmentFactories","forEach","endowmentSpecification","names","factory","boundEndowment","harden","push"],"mappings":"AAEA,SAASA,eAAe,QAAQ,kBAAkB;AAClD,OAAOC,sBAAsB,YAAY;AACzC,OAAOC,YAAY,WAAW;AAC9B,OAAOC,UAAU,SAAS;AAC1B,OAAOC,cAAc,aAAa;AAClC,OAAOC,UAAU,SAAS;AAC1B,OAAOC,aAAa,YAAY;AAChC,OAAOC,iBAAiB,gBAAgB;AACxC,OAAOC,iBAAiB,gBAAgB;AACxC,OAAOC,aAAa,YAAY;AAiBhC,6BAA6B;AAC7B,MAAMC,mBAAmD;IACvD;QAAEC,WAAWC;QAAiBC,MAAM;IAAkB;IACtD;QAAEF,WAAWG;QAAaD,MAAM;IAAc;IAC9C;QAAEF,WAAWI;QAAaF,MAAM;IAAc;IAC9C;QAAEF,WAAWK;QAAMH,MAAM;QAAQI,MAAM;IAAK;IAC5C;QAAEN,WAAWO;QAAQL,MAAM;IAAS;IACpC;QAAEF,WAAWQ;QAAeN,MAAM;IAAgB;IAClD;QAAEF,WAAWS;QAAgBP,MAAM;IAAiB;IACpD;QAAEF,WAAWU;QAAMR,MAAM;QAAQI,MAAM;IAAK;IAC5C;QAAEN,WAAWW;QAAUT,MAAM;IAAW;IACxC;QAAEF,WAAWY;QAAcV,MAAM;IAAe;IAChD;QAAEF,WAAWa;QAAcX,MAAM;IAAe;IAChD;QAAEF,WAAWc;QAAWZ,MAAM;IAAY;IAC1C;QAAEF,WAAWe;QAAYb,MAAM;IAAa;IAC5C;QAAEF,WAAWgB;QAAYd,MAAM;IAAa;IAC5C;QAAEF,WAAWiB;QAAYf,MAAM;IAAa;IAC5C;QAAEF,WAAWkB;QAAmBhB,MAAM;IAAoB;IAC1D;QAAEF,WAAWmB;QAAajB,MAAM;IAAc;IAC9C;QAAEF,WAAWoB;QAAalB,MAAM;IAAc;IAC9C;QAAEF,WAAWqB;QAAKnB,MAAM;IAAM;IAC9B;QAAEF,WAAWsB;QAAapB,MAAM;IAAc;CAC/C;AAED;;;;;;CAMC,GACD,MAAMqB,wBAAwB;IAC5B,MAAMC,qBAAyC;QAC7CjC;QACAE;QACAC;QACAC;QACAG;QACAF;QACAC;QACAL;QACAF;KACD;IAEDS,iBAAiB0B,OAAO,CAAC,CAACC;QACxB,MAAM1B,YAAY;YAChB2B,OAAO;gBAACD,uBAAuBxB,IAAI;aAAC;YACpC0B,SAAS;gBACP,MAAMC,iBACJ,OAAOH,uBAAuB1B,SAAS,KAAK,cAC5C0B,uBAAuBpB,IAAI,GACvBoB,uBAAuB1B,SAAS,CAACM,IAAI,CAACjB,mBACtCqC,uBAAuB1B,SAAS;gBACtC,OAAO;oBACL,CAAC0B,uBAAuBxB,IAAI,CAAC,EAAE4B,OAAOD;gBACxC;YACF;QACF;QACAL,mBAAmBO,IAAI,CAAC/B;IAC1B;IAEA,OAAOwB;AACT;AAEA,eAAeD,sBAAsB"}
|
|
@@ -0,0 +1,116 @@
|
|
|
1
|
+
import { assert } from '@metamask/utils';
|
|
2
|
+
import { rootRealmGlobal } from '../globalObject';
|
|
3
|
+
export const consoleAttenuatedMethods = new Set([
|
|
4
|
+
'log',
|
|
5
|
+
'assert',
|
|
6
|
+
'error',
|
|
7
|
+
'debug',
|
|
8
|
+
'info',
|
|
9
|
+
'warn'
|
|
10
|
+
]);
|
|
11
|
+
/**
|
|
12
|
+
* A set of all the `console` values that will be passed to the snap. This has
|
|
13
|
+
* all the values that are available in both the browser and Node.js.
|
|
14
|
+
*/ export const consoleMethods = new Set([
|
|
15
|
+
'debug',
|
|
16
|
+
'error',
|
|
17
|
+
'info',
|
|
18
|
+
'log',
|
|
19
|
+
'warn',
|
|
20
|
+
'dir',
|
|
21
|
+
'dirxml',
|
|
22
|
+
'table',
|
|
23
|
+
'trace',
|
|
24
|
+
'group',
|
|
25
|
+
'groupCollapsed',
|
|
26
|
+
'groupEnd',
|
|
27
|
+
'clear',
|
|
28
|
+
'count',
|
|
29
|
+
'countReset',
|
|
30
|
+
'assert',
|
|
31
|
+
'profile',
|
|
32
|
+
'profileEnd',
|
|
33
|
+
'time',
|
|
34
|
+
'timeLog',
|
|
35
|
+
'timeEnd',
|
|
36
|
+
'timeStamp',
|
|
37
|
+
'context'
|
|
38
|
+
]);
|
|
39
|
+
const consoleFunctions = [
|
|
40
|
+
'log',
|
|
41
|
+
'error',
|
|
42
|
+
'debug',
|
|
43
|
+
'info',
|
|
44
|
+
'warn'
|
|
45
|
+
];
|
|
46
|
+
/**
|
|
47
|
+
* Gets the appropriate (prepended) message to pass to one of the attenuated
|
|
48
|
+
* method calls.
|
|
49
|
+
*
|
|
50
|
+
* @param snapId - Id of the snap that we're getting a message for.
|
|
51
|
+
* @param message - The id of the snap that will interact with the endowment.
|
|
52
|
+
* @param args - The array of additional arguments.
|
|
53
|
+
* @returns An array of arguments to be passed into an attenuated console method call.
|
|
54
|
+
*/ function getMessage(snapId, message, ...args) {
|
|
55
|
+
const prefix = `[Snap: ${snapId}]`;
|
|
56
|
+
// If the first argument is a string, prepend the prefix to the message, and keep the
|
|
57
|
+
// rest of the arguments as-is.
|
|
58
|
+
if (typeof message === 'string') {
|
|
59
|
+
return [
|
|
60
|
+
`${prefix} ${message}`,
|
|
61
|
+
...args
|
|
62
|
+
];
|
|
63
|
+
}
|
|
64
|
+
// Otherwise, the `message` is an object, array, etc., so add the prefix as a separate
|
|
65
|
+
// message to the arguments.
|
|
66
|
+
return [
|
|
67
|
+
prefix,
|
|
68
|
+
message,
|
|
69
|
+
...args
|
|
70
|
+
];
|
|
71
|
+
}
|
|
72
|
+
/**
|
|
73
|
+
* Create a a {@link console} object, with the same properties as the global
|
|
74
|
+
* {@link console} object, but with some methods replaced.
|
|
75
|
+
*
|
|
76
|
+
* @param options - Factory options used in construction of the endowment.
|
|
77
|
+
* @param options.snapId - The id of the snap that will interact with the endowment.
|
|
78
|
+
* @returns The {@link console} object with the replaced methods.
|
|
79
|
+
*/ function createConsole({ snapId } = {}) {
|
|
80
|
+
assert(snapId !== undefined);
|
|
81
|
+
const keys = Object.getOwnPropertyNames(rootRealmGlobal.console);
|
|
82
|
+
const attenuatedConsole = keys.reduce((target, key)=>{
|
|
83
|
+
if (consoleMethods.has(key) && !consoleAttenuatedMethods.has(key)) {
|
|
84
|
+
return {
|
|
85
|
+
...target,
|
|
86
|
+
[key]: rootRealmGlobal.console[key]
|
|
87
|
+
};
|
|
88
|
+
}
|
|
89
|
+
return target;
|
|
90
|
+
}, {});
|
|
91
|
+
return harden({
|
|
92
|
+
console: {
|
|
93
|
+
...attenuatedConsole,
|
|
94
|
+
assert: (value, message, ...optionalParams)=>{
|
|
95
|
+
rootRealmGlobal.console.assert(value, ...getMessage(snapId, message, ...optionalParams));
|
|
96
|
+
},
|
|
97
|
+
...consoleFunctions.reduce((target, key)=>{
|
|
98
|
+
return {
|
|
99
|
+
...target,
|
|
100
|
+
[key]: (message, ...optionalParams)=>{
|
|
101
|
+
rootRealmGlobal.console[key](...getMessage(snapId, message, ...optionalParams));
|
|
102
|
+
}
|
|
103
|
+
};
|
|
104
|
+
}, {})
|
|
105
|
+
}
|
|
106
|
+
});
|
|
107
|
+
}
|
|
108
|
+
const endowmentModule = {
|
|
109
|
+
names: [
|
|
110
|
+
'console'
|
|
111
|
+
],
|
|
112
|
+
factory: createConsole
|
|
113
|
+
};
|
|
114
|
+
export default endowmentModule;
|
|
115
|
+
|
|
116
|
+
//# sourceMappingURL=console.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["../../../../src/common/endowments/console.ts"],"sourcesContent":["import type { SnapId } from '@metamask/snaps-utils';\nimport { assert } from '@metamask/utils';\n\nimport { rootRealmGlobal } from '../globalObject';\nimport type { EndowmentFactoryOptions } from './commonEndowmentFactory';\n\nexport const consoleAttenuatedMethods = new Set([\n 'log',\n 'assert',\n 'error',\n 'debug',\n 'info',\n 'warn',\n]);\n\n/**\n * A set of all the `console` values that will be passed to the snap. This has\n * all the values that are available in both the browser and Node.js.\n */\nexport const consoleMethods = new Set([\n 'debug',\n 'error',\n 'info',\n 'log',\n 'warn',\n 'dir',\n 'dirxml',\n 'table',\n 'trace',\n 'group',\n 'groupCollapsed',\n 'groupEnd',\n 'clear',\n 'count',\n 'countReset',\n 'assert',\n 'profile',\n 'profileEnd',\n 'time',\n 'timeLog',\n 'timeEnd',\n 'timeStamp',\n 'context',\n]);\n\nconst consoleFunctions = ['log', 'error', 'debug', 'info', 'warn'] as const;\n\ntype ConsoleFunctions = {\n [Key in typeof consoleFunctions[number]]: typeof rootRealmGlobal.console[Key];\n};\n\n/**\n * Gets the appropriate (prepended) message to pass to one of the attenuated\n * method calls.\n *\n * @param snapId - Id of the snap that we're getting a message for.\n * @param message - The id of the snap that will interact with the endowment.\n * @param args - The array of additional arguments.\n * @returns An array of arguments to be passed into an attenuated console method call.\n */\nfunction getMessage(snapId: SnapId, message: unknown, ...args: unknown[]) {\n const prefix = `[Snap: ${snapId}]`;\n\n // If the first argument is a string, prepend the prefix to the message, and keep the\n // rest of the arguments as-is.\n if (typeof message === 'string') {\n return [`${prefix} ${message}`, ...args];\n }\n\n // Otherwise, the `message` is an object, array, etc., so add the prefix as a separate\n // message to the arguments.\n return [prefix, message, ...args];\n}\n\n/**\n * Create a a {@link console} object, with the same properties as the global\n * {@link console} object, but with some methods replaced.\n *\n * @param options - Factory options used in construction of the endowment.\n * @param options.snapId - The id of the snap that will interact with the endowment.\n * @returns The {@link console} object with the replaced methods.\n */\nfunction createConsole({ snapId }: EndowmentFactoryOptions = {}) {\n assert(snapId !== undefined);\n const keys = Object.getOwnPropertyNames(\n rootRealmGlobal.console,\n ) as (keyof typeof console)[];\n\n const attenuatedConsole = keys.reduce((target, key) => {\n if (consoleMethods.has(key) && !consoleAttenuatedMethods.has(key)) {\n return { ...target, [key]: rootRealmGlobal.console[key] };\n }\n\n return target;\n }, {});\n\n return harden({\n console: {\n ...attenuatedConsole,\n assert: (\n value: any,\n message?: string | undefined,\n ...optionalParams: any[]\n ) => {\n rootRealmGlobal.console.assert(\n value,\n ...getMessage(snapId, message, ...optionalParams),\n );\n },\n ...consoleFunctions.reduce<ConsoleFunctions>((target, key) => {\n return {\n ...target,\n [key]: (message?: unknown, ...optionalParams: any[]) => {\n rootRealmGlobal.console[key](\n ...getMessage(snapId, message, ...optionalParams),\n );\n },\n };\n }, {} as ConsoleFunctions),\n },\n });\n}\n\nconst endowmentModule = {\n names: ['console'] as const,\n factory: createConsole,\n};\n\nexport default endowmentModule;\n"],"names":["assert","rootRealmGlobal","consoleAttenuatedMethods","Set","consoleMethods","consoleFunctions","getMessage","snapId","message","args","prefix","createConsole","undefined","keys","Object","getOwnPropertyNames","console","attenuatedConsole","reduce","target","key","has","harden","value","optionalParams","endowmentModule","names","factory"],"mappings":"AACA,SAASA,MAAM,QAAQ,kBAAkB;AAEzC,SAASC,eAAe,QAAQ,kBAAkB;AAGlD,OAAO,MAAMC,2BAA2B,IAAIC,IAAI;IAC9C;IACA;IACA;IACA;IACA;IACA;CACD,EAAE;AAEH;;;CAGC,GACD,OAAO,MAAMC,iBAAiB,IAAID,IAAI;IACpC;IACA;IACA;IACA;IACA;IACA;IACA;IACA;IACA;IACA;IACA;IACA;IACA;IACA;IACA;IACA;IACA;IACA;IACA;IACA;IACA;IACA;IACA;CACD,EAAE;AAEH,MAAME,mBAAmB;IAAC;IAAO;IAAS;IAAS;IAAQ;CAAO;AAMlE;;;;;;;;CAQC,GACD,SAASC,WAAWC,MAAc,EAAEC,OAAgB,EAAE,GAAGC,IAAe;IACtE,MAAMC,SAAS,CAAC,OAAO,EAAEH,OAAO,CAAC,CAAC;IAElC,qFAAqF;IACrF,+BAA+B;IAC/B,IAAI,OAAOC,YAAY,UAAU;QAC/B,OAAO;YAAC,CAAC,EAAEE,OAAO,CAAC,EAAEF,QAAQ,CAAC;eAAKC;SAAK;IAC1C;IAEA,sFAAsF;IACtF,4BAA4B;IAC5B,OAAO;QAACC;QAAQF;WAAYC;KAAK;AACnC;AAEA;;;;;;;CAOC,GACD,SAASE,cAAc,EAAEJ,MAAM,EAA2B,GAAG,CAAC,CAAC;IAC7DP,OAAOO,WAAWK;IAClB,MAAMC,OAAOC,OAAOC,mBAAmB,CACrCd,gBAAgBe,OAAO;IAGzB,MAAMC,oBAAoBJ,KAAKK,MAAM,CAAC,CAACC,QAAQC;QAC7C,IAAIhB,eAAeiB,GAAG,CAACD,QAAQ,CAAClB,yBAAyBmB,GAAG,CAACD,MAAM;YACjE,OAAO;gBAAE,GAAGD,MAAM;gBAAE,CAACC,IAAI,EAAEnB,gBAAgBe,OAAO,CAACI,IAAI;YAAC;QAC1D;QAEA,OAAOD;IACT,GAAG,CAAC;IAEJ,OAAOG,OAAO;QACZN,SAAS;YACP,GAAGC,iBAAiB;YACpBjB,QAAQ,CACNuB,OACAf,SACA,GAAGgB;gBAEHvB,gBAAgBe,OAAO,CAAChB,MAAM,CAC5BuB,UACGjB,WAAWC,QAAQC,YAAYgB;YAEtC;YACA,GAAGnB,iBAAiBa,MAAM,CAAmB,CAACC,QAAQC;gBACpD,OAAO;oBACL,GAAGD,MAAM;oBACT,CAACC,IAAI,EAAE,CAACZ,SAAmB,GAAGgB;wBAC5BvB,gBAAgBe,OAAO,CAACI,IAAI,IACvBd,WAAWC,QAAQC,YAAYgB;oBAEtC;gBACF;YACF,GAAG,CAAC,EAAsB;QAC5B;IACF;AACF;AAEA,MAAMC,kBAAkB;IACtBC,OAAO;QAAC;KAAU;IAClBC,SAAShB;AACX;AAEA,eAAec,gBAAgB"}
|
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
import { rootRealmGlobal } from '../globalObject';
|
|
2
|
+
export const createCrypto = ()=>{
|
|
3
|
+
if ('crypto' in rootRealmGlobal && typeof rootRealmGlobal.crypto === 'object' && 'SubtleCrypto' in rootRealmGlobal && typeof rootRealmGlobal.SubtleCrypto === 'function') {
|
|
4
|
+
return {
|
|
5
|
+
crypto: harden(rootRealmGlobal.crypto),
|
|
6
|
+
SubtleCrypto: harden(rootRealmGlobal.SubtleCrypto)
|
|
7
|
+
};
|
|
8
|
+
}
|
|
9
|
+
// For now, we expose the experimental webcrypto API for Node.js execution environments
|
|
10
|
+
// TODO: Figure out if this is enough long-term or if we should use a polyfill.
|
|
11
|
+
/* eslint-disable-next-line @typescript-eslint/no-require-imports, @typescript-eslint/no-var-requires, n/global-require */ const crypto = require('crypto').webcrypto;
|
|
12
|
+
return {
|
|
13
|
+
crypto: harden(crypto),
|
|
14
|
+
SubtleCrypto: harden(crypto.subtle.constructor)
|
|
15
|
+
};
|
|
16
|
+
};
|
|
17
|
+
const endowmentModule = {
|
|
18
|
+
names: [
|
|
19
|
+
'crypto',
|
|
20
|
+
'SubtleCrypto'
|
|
21
|
+
],
|
|
22
|
+
factory: createCrypto
|
|
23
|
+
};
|
|
24
|
+
export default endowmentModule;
|
|
25
|
+
|
|
26
|
+
//# sourceMappingURL=crypto.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["../../../../src/common/endowments/crypto.ts"],"sourcesContent":["import { rootRealmGlobal } from '../globalObject';\n\nexport const createCrypto = () => {\n if (\n 'crypto' in rootRealmGlobal &&\n typeof rootRealmGlobal.crypto === 'object' &&\n 'SubtleCrypto' in rootRealmGlobal &&\n typeof rootRealmGlobal.SubtleCrypto === 'function'\n ) {\n return {\n crypto: harden(rootRealmGlobal.crypto),\n SubtleCrypto: harden(rootRealmGlobal.SubtleCrypto),\n };\n }\n // For now, we expose the experimental webcrypto API for Node.js execution environments\n // TODO: Figure out if this is enough long-term or if we should use a polyfill.\n /* eslint-disable-next-line @typescript-eslint/no-require-imports, @typescript-eslint/no-var-requires, n/global-require */\n const crypto = require('crypto').webcrypto;\n return {\n crypto: harden(crypto),\n SubtleCrypto: harden(crypto.subtle.constructor),\n } as const;\n};\n\nconst endowmentModule = {\n names: ['crypto', 'SubtleCrypto'] as const,\n factory: createCrypto,\n};\nexport default endowmentModule;\n"],"names":["rootRealmGlobal","createCrypto","crypto","SubtleCrypto","harden","require","webcrypto","subtle","constructor","endowmentModule","names","factory"],"mappings":"AAAA,SAASA,eAAe,QAAQ,kBAAkB;AAElD,OAAO,MAAMC,eAAe;IAC1B,IACE,YAAYD,mBACZ,OAAOA,gBAAgBE,MAAM,KAAK,YAClC,kBAAkBF,mBAClB,OAAOA,gBAAgBG,YAAY,KAAK,YACxC;QACA,OAAO;YACLD,QAAQE,OAAOJ,gBAAgBE,MAAM;YACrCC,cAAcC,OAAOJ,gBAAgBG,YAAY;QACnD;IACF;IACA,uFAAuF;IACvF,+EAA+E;IAC/E,wHAAwH,GACxH,MAAMD,SAASG,QAAQ,UAAUC,SAAS;IAC1C,OAAO;QACLJ,QAAQE,OAAOF;QACfC,cAAcC,OAAOF,OAAOK,MAAM,CAACC,WAAW;IAChD;AACF,EAAE;AAEF,MAAMC,kBAAkB;IACtBC,OAAO;QAAC;QAAU;KAAe;IACjCC,SAASV;AACX;AACA,eAAeQ,gBAAgB"}
|
|
@@ -1,39 +1,43 @@
|
|
|
1
|
-
|
|
2
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
const globalObject_1 = require("../globalObject");
|
|
1
|
+
import { rootRealmGlobal } from '../globalObject';
|
|
4
2
|
/**
|
|
5
3
|
* Creates a {@link Date} constructor, with most of the same properties as the global object.
|
|
6
4
|
* The Date.now() function has added noise as to limit its precision and prevent potential timing attacks.
|
|
7
5
|
* The Date constructor uses this now() function to seed itself if no arguments are given to the constructor.
|
|
8
6
|
*
|
|
9
7
|
* @returns A modified {@link Date} constructor with limited precision.
|
|
10
|
-
*/
|
|
11
|
-
|
|
12
|
-
const keys = Object.getOwnPropertyNames(globalObject_1.rootRealmGlobal.Date);
|
|
8
|
+
*/ function createDate() {
|
|
9
|
+
const keys = Object.getOwnPropertyNames(rootRealmGlobal.Date);
|
|
13
10
|
let currentTime = 0;
|
|
14
|
-
const now = ()
|
|
15
|
-
const actual =
|
|
11
|
+
const now = ()=>{
|
|
12
|
+
const actual = rootRealmGlobal.Date.now();
|
|
16
13
|
const newTime = Math.round(actual + Math.random());
|
|
17
14
|
if (newTime > currentTime) {
|
|
18
15
|
currentTime = newTime;
|
|
19
16
|
}
|
|
20
17
|
return currentTime;
|
|
21
18
|
};
|
|
22
|
-
const NewDate = function
|
|
23
|
-
return Reflect.construct(
|
|
19
|
+
const NewDate = function(...args) {
|
|
20
|
+
return Reflect.construct(rootRealmGlobal.Date, args.length === 0 ? [
|
|
21
|
+
now()
|
|
22
|
+
] : args, new.target);
|
|
24
23
|
};
|
|
25
|
-
keys.forEach((key)
|
|
24
|
+
keys.forEach((key)=>{
|
|
26
25
|
Reflect.defineProperty(NewDate, key, {
|
|
27
26
|
configurable: false,
|
|
28
27
|
writable: false,
|
|
29
|
-
value: key === 'now' ? now :
|
|
28
|
+
value: key === 'now' ? now : rootRealmGlobal.Date[key]
|
|
30
29
|
});
|
|
31
30
|
});
|
|
32
|
-
return {
|
|
31
|
+
return {
|
|
32
|
+
Date: harden(NewDate)
|
|
33
|
+
};
|
|
33
34
|
}
|
|
34
35
|
const endowmentModule = {
|
|
35
|
-
names: [
|
|
36
|
-
|
|
36
|
+
names: [
|
|
37
|
+
'Date'
|
|
38
|
+
],
|
|
39
|
+
factory: createDate
|
|
37
40
|
};
|
|
38
|
-
|
|
41
|
+
export default endowmentModule;
|
|
42
|
+
|
|
39
43
|
//# sourceMappingURL=date.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["../../../../src/common/endowments/date.ts"],"sourcesContent":["import { rootRealmGlobal } from '../globalObject';\n\n/**\n * Creates a {@link Date} constructor, with most of the same properties as the global object.\n * The Date.now() function has added noise as to limit its precision and prevent potential timing attacks.\n * The Date constructor uses this now() function to seed itself if no arguments are given to the constructor.\n *\n * @returns A modified {@link Date} constructor with limited precision.\n */\nfunction createDate() {\n const keys = Object.getOwnPropertyNames(\n rootRealmGlobal.Date,\n ) as (keyof typeof Date)[];\n\n let currentTime = 0;\n const now = () => {\n const actual = rootRealmGlobal.Date.now();\n const newTime = Math.round(actual + Math.random());\n if (newTime > currentTime) {\n currentTime = newTime;\n }\n return currentTime;\n };\n\n const NewDate = function (...args: unknown[]) {\n return Reflect.construct(\n rootRealmGlobal.Date,\n args.length === 0 ? [now()] : args,\n new.target,\n );\n } as DateConstructor;\n\n keys.forEach((key) => {\n Reflect.defineProperty(NewDate, key, {\n configurable: false,\n writable: false,\n value: key === 'now' ? now : rootRealmGlobal.Date[key],\n });\n });\n\n return { Date: harden(NewDate) };\n}\n\nconst endowmentModule = {\n names: ['Date'] as const,\n factory: createDate,\n};\n\nexport default endowmentModule;\n"],"names":["rootRealmGlobal","createDate","keys","Object","getOwnPropertyNames","Date","currentTime","now","actual","newTime","Math","round","random","NewDate","args","Reflect","construct","length","forEach","key","defineProperty","configurable","writable","value","harden","endowmentModule","names","factory"],"mappings":"AAAA,SAASA,eAAe,QAAQ,kBAAkB;AAElD;;;;;;CAMC,GACD,SAASC;IACP,MAAMC,OAAOC,OAAOC,mBAAmB,CACrCJ,gBAAgBK,IAAI;IAGtB,IAAIC,cAAc;IAClB,MAAMC,MAAM;QACV,MAAMC,SAASR,gBAAgBK,IAAI,CAACE,GAAG;QACvC,MAAME,UAAUC,KAAKC,KAAK,CAACH,SAASE,KAAKE,MAAM;QAC/C,IAAIH,UAAUH,aAAa;YACzBA,cAAcG;QAChB;QACA,OAAOH;IACT;IAEA,MAAMO,UAAU,SAAU,GAAGC,IAAe;QAC1C,OAAOC,QAAQC,SAAS,CACtBhB,gBAAgBK,IAAI,EACpBS,KAAKG,MAAM,KAAK,IAAI;YAACV;SAAM,GAAGO,MAC9B;IAEJ;IAEAZ,KAAKgB,OAAO,CAAC,CAACC;QACZJ,QAAQK,cAAc,CAACP,SAASM,KAAK;YACnCE,cAAc;YACdC,UAAU;YACVC,OAAOJ,QAAQ,QAAQZ,MAAMP,gBAAgBK,IAAI,CAACc,IAAI;QACxD;IACF;IAEA,OAAO;QAAEd,MAAMmB,OAAOX;IAAS;AACjC;AAEA,MAAMY,kBAAkB;IACtBC,OAAO;QAAC;KAAO;IACfC,SAAS1B;AACX;AAEA,eAAewB,gBAAgB"}
|
|
@@ -1,24 +1,16 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
6
|
-
exports.createEndowments = void 0;
|
|
7
|
-
const snaps_utils_1 = require("@metamask/snaps-utils");
|
|
8
|
-
const utils_1 = require("@metamask/utils");
|
|
9
|
-
const globalObject_1 = require("../globalObject");
|
|
10
|
-
const commonEndowmentFactory_1 = __importDefault(require("./commonEndowmentFactory"));
|
|
1
|
+
import { logWarning } from '@metamask/snaps-utils';
|
|
2
|
+
import { hasProperty } from '@metamask/utils';
|
|
3
|
+
import { rootRealmGlobal } from '../globalObject';
|
|
4
|
+
import buildCommonEndowments from './commonEndowmentFactory';
|
|
11
5
|
/**
|
|
12
6
|
* Retrieve consolidated endowment factories for common endowments.
|
|
13
|
-
*/
|
|
14
|
-
const registeredEndowments = (0, commonEndowmentFactory_1.default)();
|
|
7
|
+
*/ const registeredEndowments = buildCommonEndowments();
|
|
15
8
|
/**
|
|
16
9
|
* A map of endowment names to their factory functions. Some endowments share
|
|
17
10
|
* the same factory function, but we only call each factory once for each snap.
|
|
18
11
|
* See {@link createEndowments} for details.
|
|
19
|
-
*/
|
|
20
|
-
|
|
21
|
-
builder.names.forEach((name) => {
|
|
12
|
+
*/ const endowmentFactories = registeredEndowments.reduce((factories, builder)=>{
|
|
13
|
+
builder.names.forEach((name)=>{
|
|
22
14
|
factories.set(name, builder.factory);
|
|
23
15
|
});
|
|
24
16
|
return factories;
|
|
@@ -32,59 +24,65 @@ const endowmentFactories = registeredEndowments.reduce((factories, builder) => {
|
|
|
32
24
|
*
|
|
33
25
|
* @param snap - The Snaps global API object.
|
|
34
26
|
* @param ethereum - The Snap's EIP-1193 provider object.
|
|
27
|
+
* @param snapId - The id of the snap that will use the created endowments.
|
|
35
28
|
* @param endowments - The list of endowments to provide to the snap.
|
|
36
29
|
* @returns An object containing the Snap's endowments.
|
|
37
|
-
*/
|
|
38
|
-
function createEndowments(snap, ethereum, endowments = []) {
|
|
30
|
+
*/ export function createEndowments(snap, ethereum, snapId, endowments = []) {
|
|
39
31
|
const attenuatedEndowments = {};
|
|
40
32
|
// TODO: All endowments should be hardened to prevent covert communication
|
|
41
33
|
// channels. Hardening the returned objects breaks tests elsewhere in the
|
|
42
34
|
// monorepo, so further research is needed.
|
|
43
|
-
const result = endowments.reduce(({ allEndowments, teardowns }, endowmentName)
|
|
35
|
+
const result = endowments.reduce(({ allEndowments, teardowns }, endowmentName)=>{
|
|
44
36
|
// First, check if the endowment has a factory, and default to that.
|
|
45
37
|
if (endowmentFactories.has(endowmentName)) {
|
|
46
|
-
if (!
|
|
38
|
+
if (!hasProperty(attenuatedEndowments, endowmentName)) {
|
|
47
39
|
// Call the endowment factory for the current endowment. If the factory
|
|
48
40
|
// creates multiple endowments, they will all be assigned to the
|
|
49
41
|
// `attenuatedEndowments` object, but will only be passed on to the snap
|
|
50
42
|
// if explicitly listed among its endowment.
|
|
51
43
|
// This may not have an actual use case, but, safety first.
|
|
52
44
|
// We just confirmed that endowmentFactories has the specified key.
|
|
53
|
-
const { teardownFunction, ...endowment } =
|
|
54
45
|
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion
|
|
55
|
-
endowmentFactories.get(endowmentName)(
|
|
46
|
+
const { teardownFunction, ...endowment } = endowmentFactories.get(endowmentName)({
|
|
47
|
+
snapId
|
|
48
|
+
});
|
|
56
49
|
Object.assign(attenuatedEndowments, endowment);
|
|
57
50
|
if (teardownFunction) {
|
|
58
51
|
teardowns.push(teardownFunction);
|
|
59
52
|
}
|
|
60
53
|
}
|
|
61
54
|
allEndowments[endowmentName] = attenuatedEndowments[endowmentName];
|
|
62
|
-
}
|
|
63
|
-
else if (endowmentName === 'ethereum') {
|
|
55
|
+
} else if (endowmentName === 'ethereum') {
|
|
64
56
|
// Special case for adding the EIP-1193 provider.
|
|
65
57
|
allEndowments[endowmentName] = ethereum;
|
|
66
|
-
}
|
|
67
|
-
|
|
68
|
-
(0, snaps_utils_1.logWarning)(`Access to unhardened global ${endowmentName}.`);
|
|
58
|
+
} else if (endowmentName in rootRealmGlobal) {
|
|
59
|
+
logWarning(`Access to unhardened global ${endowmentName}.`);
|
|
69
60
|
// If the endowment doesn't have a factory, just use whatever is on the
|
|
70
61
|
// global object.
|
|
71
|
-
const globalValue =
|
|
62
|
+
const globalValue = rootRealmGlobal[endowmentName];
|
|
72
63
|
allEndowments[endowmentName] = globalValue;
|
|
73
|
-
}
|
|
74
|
-
else {
|
|
64
|
+
} else {
|
|
75
65
|
// If we get to this point, we've been passed an endowment that doesn't
|
|
76
66
|
// exist in our current environment.
|
|
77
67
|
throw new Error(`Unknown endowment: "${endowmentName}".`);
|
|
78
68
|
}
|
|
79
|
-
return {
|
|
69
|
+
return {
|
|
70
|
+
allEndowments,
|
|
71
|
+
teardowns
|
|
72
|
+
};
|
|
80
73
|
}, {
|
|
81
|
-
allEndowments: {
|
|
82
|
-
|
|
74
|
+
allEndowments: {
|
|
75
|
+
snap
|
|
76
|
+
},
|
|
77
|
+
teardowns: []
|
|
83
78
|
});
|
|
84
|
-
const teardown = async ()
|
|
85
|
-
await Promise.all(result.teardowns.map((teardownFunction)
|
|
79
|
+
const teardown = async ()=>{
|
|
80
|
+
await Promise.all(result.teardowns.map((teardownFunction)=>teardownFunction()));
|
|
81
|
+
};
|
|
82
|
+
return {
|
|
83
|
+
endowments: result.allEndowments,
|
|
84
|
+
teardown
|
|
86
85
|
};
|
|
87
|
-
return { endowments: result.allEndowments, teardown };
|
|
88
86
|
}
|
|
89
|
-
|
|
87
|
+
|
|
90
88
|
//# sourceMappingURL=index.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["../../../../src/common/endowments/index.ts"],"sourcesContent":["import type { StreamProvider } from '@metamask/providers';\nimport type { SnapsGlobalObject } from '@metamask/rpc-methods';\nimport type { SnapId } from '@metamask/snaps-utils';\nimport { logWarning } from '@metamask/snaps-utils';\nimport { hasProperty } from '@metamask/utils';\n\nimport { rootRealmGlobal } from '../globalObject';\nimport type { EndowmentFactoryOptions } from './commonEndowmentFactory';\nimport buildCommonEndowments from './commonEndowmentFactory';\n\ntype EndowmentFactoryResult = {\n /**\n * A function that performs any necessary teardown when the snap becomes idle.\n *\n * NOTE:** The endowments are not reconstructed if the snap is re-invoked\n * before being terminated, so the teardown operation must not render the\n * endowments unusable; it should simply restore the endowments to their\n * original state.\n */\n teardownFunction?: () => Promise<void> | void;\n [key: string]: unknown;\n};\n\n/**\n * Retrieve consolidated endowment factories for common endowments.\n */\nconst registeredEndowments = buildCommonEndowments();\n\n/**\n * A map of endowment names to their factory functions. Some endowments share\n * the same factory function, but we only call each factory once for each snap.\n * See {@link createEndowments} for details.\n */\nconst endowmentFactories = registeredEndowments.reduce((factories, builder) => {\n builder.names.forEach((name) => {\n factories.set(name, builder.factory);\n });\n return factories;\n}, new Map<string, (options?: EndowmentFactoryOptions) => EndowmentFactoryResult>());\n\n/**\n * Gets the endowments for a particular Snap. Some endowments, like `setTimeout`\n * and `clearTimeout`, must be attenuated so that they can only affect behavior\n * within the Snap's own realm. Therefore, we use factory functions to create\n * such attenuated / modified endowments. Otherwise, the value that's on the\n * root realm global will be used.\n *\n * @param snap - The Snaps global API object.\n * @param ethereum - The Snap's EIP-1193 provider object.\n * @param snapId - The id of the snap that will use the created endowments.\n * @param endowments - The list of endowments to provide to the snap.\n * @returns An object containing the Snap's endowments.\n */\nexport function createEndowments(\n snap: SnapsGlobalObject,\n ethereum: StreamProvider,\n snapId: SnapId,\n endowments: string[] = [],\n): { endowments: Record<string, unknown>; teardown: () => Promise<void> } {\n const attenuatedEndowments: Record<string, unknown> = {};\n\n // TODO: All endowments should be hardened to prevent covert communication\n // channels. Hardening the returned objects breaks tests elsewhere in the\n // monorepo, so further research is needed.\n const result = endowments.reduce<{\n allEndowments: Record<string, unknown>;\n teardowns: (() => Promise<void> | void)[];\n }>(\n ({ allEndowments, teardowns }, endowmentName) => {\n // First, check if the endowment has a factory, and default to that.\n if (endowmentFactories.has(endowmentName)) {\n if (!hasProperty(attenuatedEndowments, endowmentName)) {\n // Call the endowment factory for the current endowment. If the factory\n // creates multiple endowments, they will all be assigned to the\n // `attenuatedEndowments` object, but will only be passed on to the snap\n // if explicitly listed among its endowment.\n // This may not have an actual use case, but, safety first.\n\n // We just confirmed that endowmentFactories has the specified key.\n // eslint-disable-next-line @typescript-eslint/no-non-null-assertion\n const { teardownFunction, ...endowment } = endowmentFactories.get(\n endowmentName,\n )!({ snapId });\n Object.assign(attenuatedEndowments, endowment);\n if (teardownFunction) {\n teardowns.push(teardownFunction);\n }\n }\n allEndowments[endowmentName] = attenuatedEndowments[endowmentName];\n } else if (endowmentName === 'ethereum') {\n // Special case for adding the EIP-1193 provider.\n allEndowments[endowmentName] = ethereum;\n } else if (endowmentName in rootRealmGlobal) {\n logWarning(`Access to unhardened global ${endowmentName}.`);\n // If the endowment doesn't have a factory, just use whatever is on the\n // global object.\n const globalValue = (rootRealmGlobal as Record<string, unknown>)[\n endowmentName\n ];\n allEndowments[endowmentName] = globalValue;\n } else {\n // If we get to this point, we've been passed an endowment that doesn't\n // exist in our current environment.\n throw new Error(`Unknown endowment: \"${endowmentName}\".`);\n }\n return { allEndowments, teardowns };\n },\n {\n allEndowments: { snap },\n teardowns: [],\n },\n );\n\n const teardown = async () => {\n await Promise.all(\n result.teardowns.map((teardownFunction) => teardownFunction()),\n );\n };\n return { endowments: result.allEndowments, teardown };\n}\n"],"names":["logWarning","hasProperty","rootRealmGlobal","buildCommonEndowments","registeredEndowments","endowmentFactories","reduce","factories","builder","names","forEach","name","set","factory","Map","createEndowments","snap","ethereum","snapId","endowments","attenuatedEndowments","result","allEndowments","teardowns","endowmentName","has","teardownFunction","endowment","get","Object","assign","push","globalValue","Error","teardown","Promise","all","map"],"mappings":"AAGA,SAASA,UAAU,QAAQ,wBAAwB;AACnD,SAASC,WAAW,QAAQ,kBAAkB;AAE9C,SAASC,eAAe,QAAQ,kBAAkB;AAElD,OAAOC,2BAA2B,2BAA2B;AAe7D;;CAEC,GACD,MAAMC,uBAAuBD;AAE7B;;;;CAIC,GACD,MAAME,qBAAqBD,qBAAqBE,MAAM,CAAC,CAACC,WAAWC;IACjEA,QAAQC,KAAK,CAACC,OAAO,CAAC,CAACC;QACrBJ,UAAUK,GAAG,CAACD,MAAMH,QAAQK,OAAO;IACrC;IACA,OAAON;AACT,GAAG,IAAIO;AAEP;;;;;;;;;;;;CAYC,GACD,OAAO,SAASC,iBACdC,IAAuB,EACvBC,QAAwB,EACxBC,MAAc,EACdC,aAAuB,EAAE;IAEzB,MAAMC,uBAAgD,CAAC;IAEvD,0EAA0E;IAC1E,yEAAyE;IACzE,2CAA2C;IAC3C,MAAMC,SAASF,WAAWb,MAAM,CAI9B,CAAC,EAAEgB,aAAa,EAAEC,SAAS,EAAE,EAAEC;QAC7B,oEAAoE;QACpE,IAAInB,mBAAmBoB,GAAG,CAACD,gBAAgB;YACzC,IAAI,CAACvB,YAAYmB,sBAAsBI,gBAAgB;gBACrD,uEAAuE;gBACvE,gEAAgE;gBAChE,wEAAwE;gBACxE,4CAA4C;gBAC5C,2DAA2D;gBAE3D,mEAAmE;gBACnE,oEAAoE;gBACpE,MAAM,EAAEE,gBAAgB,EAAE,GAAGC,WAAW,GAAGtB,mBAAmBuB,GAAG,CAC/DJ,eACC;oBAAEN;gBAAO;gBACZW,OAAOC,MAAM,CAACV,sBAAsBO;gBACpC,IAAID,kBAAkB;oBACpBH,UAAUQ,IAAI,CAACL;gBACjB;YACF;YACAJ,aAAa,CAACE,cAAc,GAAGJ,oBAAoB,CAACI,cAAc;QACpE,OAAO,IAAIA,kBAAkB,YAAY;YACvC,iDAAiD;YACjDF,aAAa,CAACE,cAAc,GAAGP;QACjC,OAAO,IAAIO,iBAAiBtB,iBAAiB;YAC3CF,WAAW,CAAC,4BAA4B,EAAEwB,cAAc,CAAC,CAAC;YAC1D,uEAAuE;YACvE,iBAAiB;YACjB,MAAMQ,cAAc,AAAC9B,eAA2C,CAC9DsB,cACD;YACDF,aAAa,CAACE,cAAc,GAAGQ;QACjC,OAAO;YACL,uEAAuE;YACvE,oCAAoC;YACpC,MAAM,IAAIC,MAAM,CAAC,oBAAoB,EAAET,cAAc,EAAE,CAAC;QAC1D;QACA,OAAO;YAAEF;YAAeC;QAAU;IACpC,GACA;QACED,eAAe;YAAEN;QAAK;QACtBO,WAAW,EAAE;IACf;IAGF,MAAMW,WAAW;QACf,MAAMC,QAAQC,GAAG,CACff,OAAOE,SAAS,CAACc,GAAG,CAAC,CAACX,mBAAqBA;IAE/C;IACA,OAAO;QAAEP,YAAYE,OAAOC,aAAa;QAAEY;IAAS;AACtD"}
|
|
@@ -1,5 +1,3 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
1
|
const MINIMUM_INTERVAL = 10;
|
|
4
2
|
/**
|
|
5
3
|
* Creates a pair of `setInterval` and `clearInterval` functions attenuated such
|
|
@@ -10,10 +8,9 @@ const MINIMUM_INTERVAL = 10;
|
|
|
10
8
|
*
|
|
11
9
|
* @returns An object with the attenuated `setInterval` and `clearInterval`
|
|
12
10
|
* functions.
|
|
13
|
-
*/
|
|
14
|
-
const createInterval = () => {
|
|
11
|
+
*/ const createInterval = ()=>{
|
|
15
12
|
const registeredHandles = new Map();
|
|
16
|
-
const _setInterval = (handler, timeout)
|
|
13
|
+
const _setInterval = (handler, timeout)=>{
|
|
17
14
|
if (typeof handler !== 'function') {
|
|
18
15
|
throw new Error(`The interval handler must be a function. Received: ${typeof handler}`);
|
|
19
16
|
}
|
|
@@ -23,7 +20,7 @@ const createInterval = () => {
|
|
|
23
20
|
registeredHandles.set(handle, platformHandle);
|
|
24
21
|
return handle;
|
|
25
22
|
};
|
|
26
|
-
const _clearInterval = (handle)
|
|
23
|
+
const _clearInterval = (handle)=>{
|
|
27
24
|
harden(handle);
|
|
28
25
|
const platformHandle = registeredHandles.get(handle);
|
|
29
26
|
if (platformHandle !== undefined) {
|
|
@@ -31,20 +28,24 @@ const createInterval = () => {
|
|
|
31
28
|
registeredHandles.delete(handle);
|
|
32
29
|
}
|
|
33
30
|
};
|
|
34
|
-
const teardownFunction = ()
|
|
35
|
-
for (const handle of registeredHandles.keys())
|
|
31
|
+
const teardownFunction = ()=>{
|
|
32
|
+
for (const handle of registeredHandles.keys()){
|
|
36
33
|
_clearInterval(handle);
|
|
37
34
|
}
|
|
38
35
|
};
|
|
39
36
|
return {
|
|
40
37
|
setInterval: harden(_setInterval),
|
|
41
38
|
clearInterval: harden(_clearInterval),
|
|
42
|
-
teardownFunction
|
|
39
|
+
teardownFunction
|
|
43
40
|
};
|
|
44
41
|
};
|
|
45
42
|
const endowmentModule = {
|
|
46
|
-
names: [
|
|
47
|
-
|
|
43
|
+
names: [
|
|
44
|
+
'setInterval',
|
|
45
|
+
'clearInterval'
|
|
46
|
+
],
|
|
47
|
+
factory: createInterval
|
|
48
48
|
};
|
|
49
|
-
|
|
49
|
+
export default endowmentModule;
|
|
50
|
+
|
|
50
51
|
//# sourceMappingURL=interval.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["../../../../src/common/endowments/interval.ts"],"sourcesContent":["const MINIMUM_INTERVAL = 10;\n\n/**\n * Creates a pair of `setInterval` and `clearInterval` functions attenuated such\n * that:\n * - `setInterval` throws if its \"handler\" parameter is not a function.\n * - `clearInterval` only clears timeouts created by its sibling `setInterval`,\n * or else no-ops.\n *\n * @returns An object with the attenuated `setInterval` and `clearInterval`\n * functions.\n */\nconst createInterval = () => {\n const registeredHandles = new Map<unknown, unknown>();\n\n const _setInterval = (handler: TimerHandler, timeout?: number): unknown => {\n if (typeof handler !== 'function') {\n throw new Error(\n `The interval handler must be a function. Received: ${typeof handler}`,\n );\n }\n harden(handler);\n const handle = Object.freeze(Object.create(null));\n const platformHandle = setInterval(\n handler,\n Math.max(MINIMUM_INTERVAL, timeout ?? 0),\n );\n registeredHandles.set(handle, platformHandle);\n return handle;\n };\n\n const _clearInterval = (handle: unknown): void => {\n harden(handle);\n const platformHandle = registeredHandles.get(handle);\n if (platformHandle !== undefined) {\n clearInterval(platformHandle as any);\n registeredHandles.delete(handle);\n }\n };\n\n const teardownFunction = (): void => {\n for (const handle of registeredHandles.keys()) {\n _clearInterval(handle);\n }\n };\n\n return {\n setInterval: harden(_setInterval),\n clearInterval: harden(_clearInterval),\n teardownFunction,\n } as const;\n};\n\nconst endowmentModule = {\n names: ['setInterval', 'clearInterval'] as const,\n factory: createInterval,\n};\nexport default endowmentModule;\n"],"names":["MINIMUM_INTERVAL","createInterval","registeredHandles","Map","_setInterval","handler","timeout","Error","harden","handle","Object","freeze","create","platformHandle","setInterval","Math","max","set","_clearInterval","get","undefined","clearInterval","delete","teardownFunction","keys","endowmentModule","names","factory"],"mappings":"AAAA,MAAMA,mBAAmB;AAEzB;;;;;;;;;CASC,GACD,MAAMC,iBAAiB;IACrB,MAAMC,oBAAoB,IAAIC;IAE9B,MAAMC,eAAe,CAACC,SAAuBC;QAC3C,IAAI,OAAOD,YAAY,YAAY;YACjC,MAAM,IAAIE,MACR,CAAC,mDAAmD,EAAE,OAAOF,QAAQ,CAAC;QAE1E;QACAG,OAAOH;QACP,MAAMI,SAASC,OAAOC,MAAM,CAACD,OAAOE,MAAM,CAAC;QAC3C,MAAMC,iBAAiBC,YACrBT,SACAU,KAAKC,GAAG,CAAChB,kBAAkBM,WAAW;QAExCJ,kBAAkBe,GAAG,CAACR,QAAQI;QAC9B,OAAOJ;IACT;IAEA,MAAMS,iBAAiB,CAACT;QACtBD,OAAOC;QACP,MAAMI,iBAAiBX,kBAAkBiB,GAAG,CAACV;QAC7C,IAAII,mBAAmBO,WAAW;YAChCC,cAAcR;YACdX,kBAAkBoB,MAAM,CAACb;QAC3B;IACF;IAEA,MAAMc,mBAAmB;QACvB,KAAK,MAAMd,UAAUP,kBAAkBsB,IAAI,GAAI;YAC7CN,eAAeT;QACjB;IACF;IAEA,OAAO;QACLK,aAAaN,OAAOJ;QACpBiB,eAAeb,OAAOU;QACtBK;IACF;AACF;AAEA,MAAME,kBAAkB;IACtBC,OAAO;QAAC;QAAe;KAAgB;IACvCC,SAAS1B;AACX;AACA,eAAewB,gBAAgB"}
|
|
@@ -1,27 +1,30 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
const globalObject_1 = require("../globalObject");
|
|
1
|
+
import { rootRealmGlobal } from '../globalObject';
|
|
2
|
+
import { createCrypto } from './crypto';
|
|
4
3
|
/**
|
|
5
4
|
* Create a {@link Math} object, with the same properties as the global
|
|
6
5
|
* {@link Math} object, but with the {@link Math.random} method replaced.
|
|
7
6
|
*
|
|
8
7
|
* @returns The {@link Math} object with the {@link Math.random} method
|
|
9
8
|
* replaced.
|
|
10
|
-
*/
|
|
11
|
-
function createMath() {
|
|
9
|
+
*/ function createMath() {
|
|
12
10
|
// `Math` does not work with `Object.keys`, `Object.entries`, etc., so we
|
|
13
11
|
// need to create a new object with the same properties.
|
|
14
|
-
const keys = Object.getOwnPropertyNames(
|
|
15
|
-
const math = keys.reduce((target, key)
|
|
12
|
+
const keys = Object.getOwnPropertyNames(rootRealmGlobal.Math);
|
|
13
|
+
const math = keys.reduce((target, key)=>{
|
|
16
14
|
if (key === 'random') {
|
|
17
15
|
return target;
|
|
18
16
|
}
|
|
19
|
-
return {
|
|
17
|
+
return {
|
|
18
|
+
...target,
|
|
19
|
+
[key]: rootRealmGlobal.Math[key]
|
|
20
|
+
};
|
|
20
21
|
}, {});
|
|
22
|
+
// Since the math endowment requires crypto, we can leverage the crypto endowment factory to get a hardened and platform agnostic instance of webcrypto
|
|
23
|
+
const { crypto: hardenedCrypto } = createCrypto();
|
|
21
24
|
return harden({
|
|
22
25
|
Math: {
|
|
23
26
|
...math,
|
|
24
|
-
random: ()
|
|
27
|
+
random: ()=>{
|
|
25
28
|
// NOTE: This is not intended to be a secure replacement for the weak
|
|
26
29
|
// random number generator used by `Math.random`. It is only intended to
|
|
27
30
|
// prevent side channel attacks of `Math.random` by replacing it with an
|
|
@@ -38,14 +41,17 @@ function createMath() {
|
|
|
38
41
|
//
|
|
39
42
|
// - https://developer.mozilla.org/en-US/docs/Web/API/SubtleCrypto/generateKey
|
|
40
43
|
// - https://developer.mozilla.org/en-US/docs/Web/API/Crypto/getRandomValues
|
|
41
|
-
return
|
|
42
|
-
}
|
|
43
|
-
}
|
|
44
|
+
return hardenedCrypto.getRandomValues(new Uint32Array(1))[0] / 2 ** 32;
|
|
45
|
+
}
|
|
46
|
+
}
|
|
44
47
|
});
|
|
45
48
|
}
|
|
46
49
|
const endowmentModule = {
|
|
47
|
-
names: [
|
|
48
|
-
|
|
50
|
+
names: [
|
|
51
|
+
'Math'
|
|
52
|
+
],
|
|
53
|
+
factory: createMath
|
|
49
54
|
};
|
|
50
|
-
|
|
55
|
+
export default endowmentModule;
|
|
56
|
+
|
|
51
57
|
//# sourceMappingURL=math.js.map
|