-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy pathapi.js
280 lines (268 loc) · 10.6 KB
/
api.js
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
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
const _fetchJson = u => fetch(u)
.then(res => res.json());
const _pad64 = s => '0x' + '0'.repeat(64 - s.length) + s;
const _uint8ArrayToHex = uint8Array => Array.from(uint8Array).map(n => n.toString(16).padStart(2, '0')).join('');
const _hexToUint8Array = s => {
const b = new Uint8Array(s.length/2);
for (let i = 0; i < b.length; i++) {
b[i] = parseInt(s.slice(i*2, i*2+2), 16);
}
return b;
};
const _encodeApps = apps => {
let s = '0x';
for (let i = 0; i < apps.length; i++) {
const app = apps[i];
const {appType, url, position, orientation} = app;
s += appType === 'browser' ? '02' : '03';
s += _uint8ArrayToHex(new TextEncoder().encode(url)) + '00';
const positionArrayBuffer = new ArrayBuffer(3*Float32Array.BYTES_PER_ELEMENT);
const positionFloat32Array = new Float32Array(positionArrayBuffer);
for (let i = 0; i < positionFloat32Array.length; i++) {
positionFloat32Array[i] = position[i];
}
s += _uint8ArrayToHex(new Uint8Array(positionArrayBuffer));
const orientationArrayBuffer = new ArrayBuffer(4*Float32Array.BYTES_PER_ELEMENT);
const orientationFloat32Array = new Float32Array(orientationArrayBuffer);
for (let i = 0; i < orientationFloat32Array.length; i++) {
orientationFloat32Array[i] = orientation[i];
}
s += _uint8ArrayToHex(new Uint8Array(orientationArrayBuffer));
}
return s;
};
const _decodeApps = s => {
const result = [];
let index = 0;
index += 2; // 0x
while (index < s.length) {
const appTypeHex = s.slice(index, index+2);
if (appTypeHex.length !== 2) {
throw new Error('failed to parse app type');
}
const appType = appTypeHex === '02' ? 'browser' : 'volume';
index += appTypeHex.length;
const zeroIndex = s.indexOf('00', index);
if (zeroIndex === -1) {
throw new Error('failed to parse url');
}
const urlHex = s.slice(index, zeroIndex);
const urlBuffer = _hexToUint8Array(urlHex);
const url = new TextDecoder().decode(urlBuffer);
index = zeroIndex + 2;
const positionHexString = s.slice(index, index+3*4*2);
if (positionHexString.length !== 3*4*2) {
throw new Error('failed to parse position');
}
const positionUint8Array = _hexToUint8Array(positionHexString);
const position = Array.from(new Float32Array(positionUint8Array.buffer, positionUint8Array.byteOffset, positionUint8Array.byteLength/Float32Array.BYTES_PER_ELEMENT));
index += positionHexString.length;
const orientationHexString = s.slice(index, index+4*4*2);
if (orientationHexString.length !== 4*4*2) {
throw new Error('failed to parse orientation');
}
const orientationUint8Array = _hexToUint8Array(orientationHexString);
const orientation = Array.from(new Float32Array(orientationUint8Array.buffer, orientationUint8Array.byteOffset, orientationUint8Array.byteLength/Float32Array.BYTES_PER_ELEMENT));
index += orientationHexString.length;
const app = {
appType,
url,
position,
orientation,
};
result.push(app);
}
return result;
};
const _makeContracts = async web3 => {
const [webaverseAbi, webaverseAddress, webasceneAbi, webasceneAddress] = await Promise.all([
_fetchJson('./webaverse-contracts/abis/webaverse.json'),
_fetchJson('./webaverse-contracts/addresses/webaverse.json'),
_fetchJson('./webaverse-contracts/abis/webascene.json'),
_fetchJson('./webaverse-contracts/addresses/webascene.json'),
]);
return {
webaverse: new web3.eth.Contract(webaverseAbi, webaverseAddress),
webascene: new web3.eth.Contract(webasceneAbi, webasceneAddress),
};
};
async function _execute(spec) {
const _waitTransaction = txId => new Promise((accept, reject) => {
const _recurse = () => {
this.eth.getTransactionReceipt(txId, (err, result) => {
if (!err) {
if (result !== null) {
accept(result);
} else {
_recurse();
}
} else {
reject(err);
}
});
};
_recurse();
});
const {method, data} = spec;
switch (method) {
case 'send': {
const {address, value, currency} = data;
await this.eth.sendTransaction({from: this.eth.defaultAccount, to: address, value});
break;
}
case 'mintTokenFromSignature': {
const {addr, x, y, v, r, s} = data;
const gas = await this.contracts.webaverse.methods.mintTokenFromSignature(addr, x, y, v, r, s).estimateGas({from: this.eth.defaultAccount});
console.log('estimate gas', gas);
const {transactionHash} = await this.contracts.webaverse.methods.mintTokenFromSignature(addr, x, y, v, r, s).send({from: this.eth.defaultAccount, gas});
console.log('got txid', transactionHash);
const rx = await _waitTransaction(transactionHash);
console.log('got rx', rx);
return rx;
/* const result = await this.contracts.webascene.methods.getSceneByCoord(x, y).call();
console.log('got scene id', parseInt(result[0], 10));
return parseInt(result[0], 10); */
}
case 'getToken': {
const {x, y} = data;
const [tokenResult, sceneResult] = await Promise.all([
this.contracts.webaverse.methods.getTokenByCoord(x, y).call(),
this.contracts.webascene.methods.getSceneByCoord(x, y).call(),
]);
const owner = tokenResult[0];
const sceneId = parseInt(sceneResult[0], 10);
const coords = (() => {
if (sceneId) {
const coordsData = sceneResult[1];
const result = [];
for (let i = 0; i < coordsData.length; i += 2) {
result.push([parseInt(coordsData[i], 10), parseInt(coordsData[i+1], 10)]);
}
return result;
} else {
return [[x, y]];
}
})();
const apps = (() => {
if (sceneId) {
const appsData = sceneResult[2];
return appsData ? _decodeApps(appsData) : [];
} else {
return [];
}
})();
/* if (coords.some(coord => coord[0] === -1 && coord[1] === 0)) {
console.log('get scene', sceneResult, owner, coords, apps);
} */
return {
owner,
id: parseInt(tokenResult[1], 10),
x: parseInt(tokenResult[2], 10),
y: parseInt(tokenResult[3], 10),
lastTimestamp: parseInt(tokenResult[4], 10),
scene: {
id: sceneId,
coords,
apps,
owner,
},
/* sceneId,
scene: sceneId ? await _execute({
method: 'getScene',
data: {
sceneId,
},
}) : null, */
};
}
/* case 'getScene': {
const {sceneId} = data;
console.log('get scene 1', sceneId);
const result = await this.contracts.webascene.methods.getSceneById(sceneId).call();
console.log('get scene 2', result);
return {
id: parseInt(result[0], 10),
coords: result[1],
apps: result[2],
};
} */
case 'setScene': {
const {coords: coordsData, apps: appsData} = data;
const coords = (() => {
const result = Array(coordsData.length * 2);
for (let i = 0; i < coordsData.length; i++) {
const coord = coordsData[i];
result[i*2] = new BigNumber(coord[0]);
result[i*2 + 1] = new BigNumber(coord[1]);
}
return result;
})();
const apps = _encodeApps(appsData);
console.log('set scene', {coordsData, appsData, coords, apps});
const gas = await this.contracts.webascene.methods.setScene(coords, apps).estimateGas({from: this.eth.defaultAccount});
const balance = await this.eth.getBalance(this.eth.defaultAccount);
const {transactionHash} = await this.contracts.webascene.methods.setScene(coords, apps).send({from: this.eth.defaultAccount, gas});
console.log('got txid', transactionHash);
const rx = await _waitTransaction(transactionHash);
console.log('got rx', rx);
const result = await this.contracts.webascene.methods.getSceneByCoord(coords[0], coords[1]).call();
console.log('got scene id', parseInt(result[0], 10));
return parseInt(result[0], 10);
}
case 'setSceneApps': {
const {sceneId, apps: appsData} = data;
const apps = _encodeApps(appsData);
console.log('set scene apps', {sceneId, appsData, apps});
const gas = await this.contracts.webascene.methods.setSceneApps(sceneId, apps).estimateGas({from: this.eth.defaultAccount});
const balance = await this.eth.getBalance(this.eth.defaultAccount);
const {transactionHash} = await this.contracts.webascene.methods.setSceneApps(sceneId, apps).send({from: this.eth.defaultAccount, gas});
console.log('got txid', transactionHash);
const rx = await _waitTransaction(transactionHash);
console.log('got rx', rx);
return rx;
}
case 'buyToken': {
const {x, y, apps: appsData, tokenPrice} = data;
const apps = _encodeApps(appsData);
console.log('buy token', x, y, appsData, apps, tokenPrice);
const gas = await this.contracts.webascene.methods.buyToken(x, y, apps).estimateGas({from: this.eth.defaultAccount, value: tokenPrice});
const balance = await this.eth.getBalance(this.eth.defaultAccount);
const {transactionHash} = await this.contracts.webascene.methods.buyToken(x, y, apps).send({from: this.eth.defaultAccount, value: tokenPrice, gas});
console.log('got txid', transactionHash);
const rx = await _waitTransaction(transactionHash);
console.log('got rx', rx);
return rx;
}
default: throw new Error(`unknown execute method ${method}`);
}
}
const getWeb3 = async () => {
if (window.ethereum) {
await window.ethereum.enable()
const web3 = new window.Web3(window.ethereum);
web3.eth.defaultAccount = window.ethereum.selectedAddress;
web3.contracts = await _makeContracts(web3);
web3.execute = _execute;
return web3;
} else {
return null;
}
};
const makeWeb3 = async (password = '', approve = () => Promise.reject()) => {
const INFURA_API_KEY = '';
const rpcUrl = `https://rinkeby.infura.io/v3/${INFURA_API_KEY}`;
const web3 = new Web3(new Web3.providers.HttpProvider(rpcUrl));
const hash = ethUtil.sha3(password);
const mnemonic = bip39.entropyToMnemonic(hash);
const seed = bip39.mnemonicToSeedSync(mnemonic, '');
const privateKey = '0x' + bip32.fromSeed(seed).derivePath("m/44'/60'/0'/0").derive(0).privateKey.toString('hex');
const account = web3.eth.accounts.privateKeyToAccount(privateKey);
web3.eth.accounts.wallet.add(account);
web3.eth.defaultAccount = account.address;
web3.contracts = await _makeContracts(web3);
web3.execute = async spec => {
await approve(spec);
return await execute(spec);
};
return web3;
};