1import { Just, Nothing } from 'folktale/maybe';
2import { randomHex } from 'web3-utils';
3import WalletConnect from '@walletconnect/client';
14} from '@urbit/roller-api';
16import { Invite } from 'lib/types/Invite';
17import { signTransactionHash } from 'lib/authToken';
18import { deriveNetworkKeys, CRYPTO_SUITE_VERSION } from 'lib/keys';
19import { addHexPrefix } from 'lib/utils/address';
20import { makeDeterministicTicket, generateWallet } from 'lib/walletgen';
21import { WALLET_TYPES } from 'lib/constants';
22import { L2TransactionArgs, TransactionData } from 'lib/types/L2Transaction';
24export const SECOND = 1000;
25export const MINUTE = SECOND * 60;
26export const HOUR = MINUTE * 60;
28export const padZero = (amount: number) =>
29 `${amount < 10 && amount >= 0 ? '0' : ''}${amount}`;
31export const hasPoint = (point: number) => (invite: Invite) =>
32 invite.planet === point;
34export const getTimeToNextBatch = (nextBatch: number, now: number) => {
35 const toNext = Math.max(nextBatch - now, 0);
36 const hours = Math.floor(toNext / HOUR);
37 const minutes = Math.floor((toNext - hours * HOUR) / MINUTE);
38 const seconds = Math.floor(
39 (toNext - hours * HOUR - minutes * MINUTE) / SECOND
42 return `${padZero(hours)}h ${padZero(minutes)}m ${padZero(seconds)}s`;
45export const isL2 = (dom?: string) => dom === 'l2';
47export const isL2Spawn = (dom?: string) => dom === 'l2' || dom === 'spawn';
49export const generateInviteWallet = async (point: number, seed: string) => {
50 const ticket = makeDeterministicTicket(point, seed);
51 const inviteWallet = await generateWallet(point, ticket, true);
53 return { ticket, inviteWallet };
56export const generateHashAndSign = async (
65 connector: WalletConnect | null
67 if (walletType === WALLET_TYPES.METAMASK) {
68 const hash = await api.prepareForSigning(nonce, from, type, data);
69 const sig = await web3.eth.personal.sign(hash, wallet.address, '');
71 } else if (walletType === WALLET_TYPES.WALLET_CONNECT) {
72 if (!connector || !connector.connected) {
73 throw new Error('Awaiting WalletConnect connection...');
76 const hash = await api.prepareForSigning(nonce, from, type, data);
77 const sig = await connector.signPersonalMessage([hash, wallet.address]);
80 const hash = await api.getUnsignedTx(nonce, from, type, data);
81 const sig = await signTransactionHash(hash, wallet.privateKey);
86export const spawn = async (
95 connector: WalletConnect | null
98 ship: _point, //ship that is spawning the planet
103 address: wallet.address,
104 ship: planet, // ship to spawn
107 const sig = await generateHashAndSign(
118 return api.spawn(sig, from, wallet.address, data);
121export const configureKeys = async (
130 connector: WalletConnect | null,
134 ship: point, //ship to configure keys
138 // TODO: do something here?
139 if (Nothing.hasInstance(networkSeed)) {
140 console.log("Network key Error: couldn't derive network keys");
141 // throw new Error("Network key Error: couldn't derive network keys");
144 const seed = Just.hasInstance(networkSeed)
148 const pair = deriveNetworkKeys(seed);
151 encrypt: addHexPrefix(pair.crypt.public),
152 auth: addHexPrefix(pair.auth.public),
153 cryptoSuite: String(CRYPTO_SUITE_VERSION),
154 breach: breach || false,
157 const sig = await generateHashAndSign(
168 return api.configureKeys(sig, from, wallet.address, data);
171export const transferPointRequest = async (
174 pointToTransfer: Ship,
180 connector: WalletConnect | null,
184 ship: pointToTransfer,
190 reset: reset || false,
193 const sig = await generateHashAndSign(
205 return api.transferPoint(sig, from, wallet.address, data);
208export const adopt = async (
217 ship: sponsor, // ship that is adopting the planet
222 ship, // ship to adopt
225 const hash = await api.getUnsignedTx(nonce, from, 'detach', data);
226 const sSig = signTransactionHash(hash, _wallet.privateKey);
227 return api.adopt(sSig, from, _wallet.address, data);
230export const detach = async (
239 ship: sponsor, // ship that is detaching the planet
244 ship, // ship to detach
247 const hash = await api.getUnsignedTx(nonce, from, 'detach', data);
248 const sSig = signTransactionHash(hash, _wallet.privateKey);
249 return api.detach(sSig, from, _wallet.address, data);
252export const reject = async (
261 ship: sponsor, // ship that is rejecting the planet
266 ship, // ship to reject
269 const hash = await api.getUnsignedTx(nonce, from, 'detach', data);
270 const sSig = signTransactionHash(hash, _wallet.privateKey);
271 return api.reject(sSig, from, _wallet.address, data);
274const proxyType = (proxy: Proxy) => {
277 return 'setManagementProxy';
279 return 'setSpawnProxy';
281 return 'setTransferProxy';
283 throw new Error(`Unknown proxyType ${proxy}`);
287const setProxy = async (
289 proxyAddressType: string,
295 switch (proxyAddressType) {
297 return await api.setManagementProxy(sig, from, address, data);
299 return await api.setSpawnProxy(sig, from, address, data);
301 return await api.setTransferProxy(sig, from, address, data);
303 throw new Error(`Unknown proxyType ${proxyAddressType}`);
307export const registerProxyAddress = async (
312 proxyAddressType: string,
317 connector: WalletConnect | null
323 const data = { address };
325 const sig = await generateHashAndSign(
330 proxyType(proxyAddressType),
337 return setProxy(api, proxyAddressType, sig, from, wallet.address, data);
340const getDataAndMethod = (args: L2TransactionArgs) => {
350 const result: TransactionData = { data: { address }, method: () => null };
353 case 'transferPoint':
354 result.method = api.transferPoint;
355 result.data.reset = reset || false;
358 result.method = api.spawn;
359 result.data.ship = pointToSpawn;
361 case 'configureKeys':
362 // TODO: do something here?
363 if (Nothing.hasInstance(networkSeed)) {
364 console.log("Network key Error: couldn't derive network keys");
365 // throw new Error("Network key Error: couldn't derive network keys");
368 const seed = Just.hasInstance(networkSeed)
372 const pair = deriveNetworkKeys(seed);
375 encrypt: addHexPrefix(pair.crypt.public),
376 auth: addHexPrefix(pair.auth.public),
377 cryptoSuite: String(CRYPTO_SUITE_VERSION),
378 breach: breach || false,
380 result.method = api.configureKeys;
383 result.method = api.escape;
384 result.data.ship = newSponsor;
385 delete result.data.address;
390export const submitL2Transaction = async (args: L2TransactionArgs) => {
403 const from = { ship, proxy };
404 const { data, method } = getDataAndMethod(args);
406 const sig = await generateHashAndSign(
418 return method(sig, from, wallet.address, data);
421export const reticketL2Point = async () => {};
423export const hasInvite = (point: number) => (invite: Invite) =>
424 invite.planet === point && invite.ticket.length > 0;
426function isSpawn(tx: L2Data | undefined): tx is SpawnParams {
427 return (tx as SpawnParams) !== undefined;
430function isShipNumber(ship: number | string | undefined): ship is number {
431 return (ship as number) !== undefined;
434export const getPendingSpawns = (pendingTxs: PendingTransaction[]) =>
435 pendingTxs.reduce((acc, pending) => {
436 return isSpawn(pending.rawTx?.tx) &&
437 isShipNumber(pending.rawTx?.tx.data.ship)
438 ? acc.add(pending.rawTx?.tx.data.ship!)
440 }, 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