1import { Just, Nothing } from 'folktale/maybe';
2import { Invite } from 'lib/types/Invite';
3import { signTransactionHash } from 'lib/authToken';
4import { randomHex } from 'web3-utils';
16} from '@urbit/roller-api';
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';
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' || dom === 'spawn';
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);
322export const reticketL2Point = async () => {};
324export const hasInvite = (point: number) => (invite: Invite) =>
325 invite.planet === point && invite.ticket.length > 0;
327function isSpawn(tx: L2Data | undefined): tx is SpawnParams {
328 return (tx as SpawnParams) !== undefined;
331function isShipNumber(ship: number | string | undefined): ship is number {
332 return (ship as number) !== undefined;
335export const getPendingSpawns = (pendingTxs: PendingTransaction[]) =>
336 pendingTxs.reduce((acc, pending) => {
337 return isSpawn(pending.rawTx?.tx) &&
338 isShipNumber(pending.rawTx?.tx.data.ship)
339 ? acc.add(pending.rawTx?.tx.data.ship!)
341 }, 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