This repository was archived by the owner on Oct 6, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy pathindex.html
188 lines (176 loc) · 6.85 KB
/
index.html
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
<html>
<head>
<script src="/dist/index.browser.umd.js"></script>
<script src="/test/browser/idls/test-contract.js"></script>
<script>
window.onload = function() {
/**
* Makes a service request and places the stringified request into the
* `#test` html element.
*
* Tests the same service both with and without confidentiality.
*/
async function runTest() {
let nonConf = await runCase(nonConfidentialTest);
document.getElementById('plaintext-test').innerHTML =
`<div class="result">${JSON.stringify(nonConf)}</div>`;
// ^ Place the results in the html for consumption by the test runner.
let conf = await runCase(confidentialTest);
document.getElementById('confidential-test').innerHTML =
`<div class="result">${JSON.stringify(conf)}</div>`;
}
async function runCase(test) {
let txDataPromise = new Promise(async resolve => {
// Given a service.
let service = new oasis.Service(idl, address, {
gateway: test.gateway(resolve),
db: test.db(),
});
// When I make an rpc request.
await service.the(input1, input2);
});
let request = await txDataPromise;
// Then I should have given the gateway the encoded wire format of the request.
return test.coder.decode({}, request.data);
}
/**
* Returns inputs for the `DefTy` service `the` rpc to be used for testing.
* See test/idls/test-contract.ts.
*/
function serviceInputs() {
let input1 = {
f1: 1,
f3: {
test: 0
},
f4: [
oasis.utils.bytes.parseHex('0x0000000000000000000000000000000000000000000000000000000000000001'),
oasis.utils.bytes.parseHex('0x0000000000000000000000000000000000000000000000000000000000000002'),
oasis.utils.bytes.parseHex('0x0000000000000000000000000000000000000003'),
]
};
let input2 = oasis.utils.bytes.parseHex('0x1234');
return [input1, input2];
}
/**
* Implements the Gateway interface to make requests for a service.
*/
class RequestMockOasisGateway {
/**
* @param requestResolve is a promise's resolve function returning the
* request received by this gateway.
*/
constructor(requestResolve) {
this.requestResolve = requestResolve;
}
async rpc(request) {
this.requestResolve(request);
return {
output: oasis.utils.bytes.toHex(oasis.utils.cbor.encode({ success: true}))
}
}
async publicKey(address) {
return {};
}
async getCode(request) {
return {
code: new Uint8Array([0])
};
}
hasSigner() {
return false;
}
}
/**
* Gateway for the confidential contract.
*/
class ConfidentialMockOasisGateway {
constructor(requestResolve) {
this.requestResolve = requestResolve;
}
async rpc(request) {
this.requestResolve(request);
return { output: oasis.utils.bytes.parseHex("0x8f4f6346d2dafe2a6a0cb664d4fa17130284e2e123459471e1f0863fb78ad4a45b110d15bcb3dbfe956ebcb14947324c76ec5f571b1a42643cb03bce81aae3f38e68a3") }
}
async publicKey(address) {
return { publicKey: keys.peerPublicKey.bytes };
}
async getCode(request) {
// Just return any bytecode with the deploy header with confidential === true.
return {
code: new Uint8Array(oasis.utils.bytes.parseHex('0x00736973000100247b22636f6e666964656e7469616c223a747275652c22657870697279223a31323334357d0102039999816f636f6e7374727563746f722d617267'))
};
}
hasSigner() {
return false;
}
}
/**
* Storage to use for the test cases.
*/
class DummyStorage {
constructor() {
this.storage = new Map();
}
get(key) {
return this.storage.get(key);
}
set(key, value) {
this.storage.set(key, value);
}
}
// Constants for the test case follow.
// Dummy service address.
const address = '0x372FF3aeA1fc69B9C440A5fE0B4c23c38226Da68';
// Inputs to the rpc function called.
const [input1, input2] = serviceInputs();
// Keys used for encryption.
const keys = {
publicKey: new oasis.utils.PublicKey(new Uint8Array(
[76, 194, 101, 195, 41, 86, 188, 68, 20, 196, 45, 88, 50, 28, 101, 65, 169, 62, 20, 86, 188, 169, 250, 131, 121, 184, 83, 198, 108, 127, 191, 59]
)),
privateKey: new oasis.utils.PrivateKey(new Uint8Array(
[77, 65, 100, 57, 158, 249, 115, 170, 228, 223, 8, 122, 34, 16, 7, 109, 121, 80, 221, 98, 147, 57, 33, 10, 117, 181, 183, 181, 119, 248, 6, 97]
)),
peerPublicKey: new oasis.utils.PublicKey(new Uint8Array(
[11, 22, 95, 106, 208, 178, 217, 236, 126, 30, 21, 232, 31, 89, 61, 20, 62, 53, 45, 10, 43, 25, 109, 77, 213, 84, 134, 55, 254, 242, 21, 87]
)),
peerPrivateKey: new oasis.utils.PrivateKey(new Uint8Array(
[102, 213, 202, 145, 129, 99, 154, 30, 39, 120, 107, 223, 154, 170, 91, 51, 180, 126, 147, 208, 28, 232, 221, 65, 142, 189, 187, 37, 158, 134, 218, 171]
)),
};
// Non-confidential test case.
let nonConfidentialTest = {
gateway: (resolve) => new RequestMockOasisGateway(resolve),
db: () => new DummyStorage(),
coder: oasis.utils.OasisCoder.plaintext(),
};
// Confidential test case.
let confidentialTest = {
gateway: (resolve) => new ConfidentialMockOasisGateway(resolve, keys.publicKey),
db: () => {
// Create a db with the local keys preset, so that we can
// always decrypt on the receiving end.
let db = new DummyStorage();
db.set('me', JSON.stringify({
publicKey: keys.publicKey.hex,
privateKey: keys.privateKey.hex,
}));
return db;
},
coder: oasis.utils.OasisCoder.confidential({
publicKey: keys.peerPublicKey,
privateKey: keys.peerPrivateKey,
peerPublicKey: keys.publicKey,
peerPrivateKey: keys.peerPrivateKey,
})
};
runTest();
}
</script>
</head>
<body>
<h1 id="plaintext-test">Running Test</h1>
<h1 id="confidential-test">Running Test</h1>
</body>
</html>