1import { Just, Nothing } from 'folktale/maybe';
2import { randomHex } from 'web3-utils';
13} from '@urbit/roller-api';
15import { Invite } from 'lib/types/Invite';
16import { signTransactionHash } from 'lib/authToken';
17import { deriveNetworkKeys, CRYPTO_SUITE_VERSION } from 'lib/keys';
18import { addHexPrefix } from 'lib/utils/address';
19import { makeDeterministicTicket, generateWallet } from 'lib/walletgen';
20import { WALLET_TYPES } from 'lib/constants';
21import { L2TransactionArgs, TransactionData } from 'lib/types/L2Transaction';
23export const SECOND = 1000;
24export const MINUTE = SECOND * 60;
25export const HOUR = MINUTE * 60;
27export const padZero = (amount: number) =>
28 `${amount < 10 && amount >= 0 ? '0' : ''}${amount}`;
30export const hasPoint = (point: number) => (invite: Invite) =>
31 invite.planet === point;
33export const getTimeToNextBatch = (nextBatch: number, now: number) => {
34 const toNext = Math.max(nextBatch - now, 0);
35 const hours = Math.floor(toNext / HOUR);
36 const minutes = Math.floor((toNext - hours * HOUR) / MINUTE);
37 const seconds = Math.floor(
38 (toNext - hours * HOUR - minutes * MINUTE) / SECOND
41 return `${padZero(hours)}h ${padZero(minutes)}m ${padZero(seconds)}s`;
44export const isL2 = (dom?: string) => dom === 'l2';
46export const isL2Spawn = (dom?: string) => dom === 'spawn';
48export const generateInviteWallet = async (point: number, seed: string) => {
49 const ticket = makeDeterministicTicket(point, seed);
50 const inviteWallet = await generateWallet(point, ticket, true);
52 return { ticket, inviteWallet };
55export const generateHashAndSign = async (
65 if (walletType === WALLET_TYPES.METAMASK) {
66 const hash = await api.prepareForSigning(nonce, from, type, data);
67 const sig = await web3.eth.personal.sign(hash, wallet.address, '');
70 const hash = await api.getUnsignedTx(nonce, from, type, data);
71 const sig = await signTransactionHash(hash, wallet.privateKey);
76export const spawn = async (
87 ship: _point, //ship that is spawning the planet
92 address: wallet.address,
93 ship: planet, // ship to spawn
96 const sig = await generateHashAndSign(
106 return api.spawn(sig, from, wallet.address, data);
109export const configureKeys = async (
121 ship: point, //ship to configure keys
125 // TODO: do something here?
126 if (Nothing.hasInstance(networkSeed)) {
127 console.log("Network key Error: couldn't derive network keys");
128 // throw new Error("Network key Error: couldn't derive network keys");
131 const seed = Just.hasInstance(networkSeed)
135 const pair = deriveNetworkKeys(seed);
138 encrypt: addHexPrefix(pair.crypt.public),
139 auth: addHexPrefix(pair.auth.public),
140 cryptoSuite: String(CRYPTO_SUITE_VERSION),
141 breach: breach || false,
144 const sig = await generateHashAndSign(
154 return api.configureKeys(sig, from, wallet.address, data);
157export const transferPointRequest = async (
160 pointToTransfer: Ship,
169 ship: pointToTransfer,
175 reset: reset || false,
178 const sig = await generateHashAndSign(
189 return api.transferPoint(sig, from, wallet.address, data);
192export const adopt = async (
201 ship: sponsor, // ship that is adopting the planet
206 ship, // ship to adopt
209 const hash = await api.getUnsignedTx(nonce, from, 'detach', data);
210 const sSig = signTransactionHash(hash, _wallet.privateKey);
211 return api.adopt(sSig, from, _wallet.address, data);
214export const detach = async (
223 ship: sponsor, // ship that is detaching the planet
228 ship, // ship to detach
231 const hash = await api.getUnsignedTx(nonce, from, 'detach', data);
232 const sSig = signTransactionHash(hash, _wallet.privateKey);
233 return api.detach(sSig, from, _wallet.address, data);
236export const reject = async (
245 ship: sponsor, // ship that is rejecting the planet
250 ship, // ship to reject
253 const hash = await api.getUnsignedTx(nonce, from, 'detach', data);
254 const sSig = signTransactionHash(hash, _wallet.privateKey);
255 return api.reject(sSig, from, _wallet.address, data);
258const proxyType = (proxy: Proxy) => {
261 return 'setManagementProxy';
263 return 'setSpawnProxy';
265 return 'setTransferProxy';
267 throw new Error(`Unknown proxyType ${proxy}`);
271const setProxy = async (
273 proxyAddressType: string,
279 switch (proxyAddressType) {
281 return await api.setManagementProxy(sig, from, address, data);
283 return await api.setSpawnProxy(sig, from, address, data);
285 return await api.setTransferProxy(sig, from, address, data);
287 throw new Error(`Unknown proxyType ${proxyAddressType}`);
291export const registerProxyAddress = async (
296 proxyAddressType: string,
306 const data = { address };
308 const sig = await generateHashAndSign(
313 proxyType(proxyAddressType),
319 return setProxy(api, proxyAddressType, sig, from, wallet.address, data);
322const getDataAndMethod = (args: L2TransactionArgs) => {
332 const result: TransactionData = { data: { address }, method: () => null };
335 case 'transferPoint':
336 result.method = api.transferPoint;
337 result.data.reset = reset || false;
340 result.method = api.spawn;
341 result.data.ship = pointToSpawn;
343 case 'configureKeys':
344 // TODO: do something here?
345 if (Nothing.hasInstance(networkSeed)) {
346 console.log("Network key Error: couldn't derive network keys");
347 // throw new Error("Network key Error: couldn't derive network keys");
350 const seed = Just.hasInstance(networkSeed)
354 const pair = deriveNetworkKeys(seed);
357 encrypt: addHexPrefix(pair.crypt.public),
358 auth: addHexPrefix(pair.auth.public),
359 cryptoSuite: String(CRYPTO_SUITE_VERSION),
360 breach: breach || false,
362 result.method = api.configureKeys;
365 result.method = api.escape;
366 result.data.ship = newSponsor;
367 delete result.data.address;
372export const submitL2Transaction = async (args: L2TransactionArgs) => {
373 const { api, wallet, ship, type, proxy, nonce, walletType, web3 } = args;
375 const from = { ship, proxy };
376 const { data, method } = getDataAndMethod(args);
378 const sig = await generateHashAndSign(
389 return method(sig, from, wallet.address, data);
392export const reticketL2Point = async () => {};
394export const hasInvite = (point: number) => (invite: Invite) =>
395 invite.planet === point && invite.ticket.length > 0;
397function isSpawn(tx: L2Data | undefined): tx is SpawnParams {
398 return (tx as SpawnParams) !== undefined;
401function isShipNumber(ship: number | string | undefined): ship is number {
402 return (ship as number) !== undefined;
405export const getPendingSpawns = (pendingTxs: PendingTransaction[]) =>
406 pendingTxs.reduce((acc, pending) => {
407 return isSpawn(pending.rawTx?.tx) &&
408 isShipNumber(pending.rawTx?.tx.data.ship)
409 ? acc.add(pending.rawTx?.tx.data.ship!)
411 }, new Set<number>());
keysesc to close
- j k
- move down, up
- enter
- open
- esc
- back, or up a level
- 1 2 3 4
- readme, files, commits, refs
- /
- filter
- y
- copy the clone command
- c
- compare the chosen commit with another (or shift-click both)
- ?
- show or hide this list