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);
71 // same issue as seen in getMetamaskAuthToken
72 if (window.ethereum) {
73 sig = window.ethereum.request({
74 method: 'personal_sign',
75 params: [hash, wallet.address],
79 sig = await web3.eth.personal.sign(hash, wallet.address, '');
83 } else if (walletType === WALLET_TYPES.WALLET_CONNECT) {
84 if (!connector || !connector.connected) {
85 throw new Error('Awaiting WalletConnect connection...');
88 const hash = await api.prepareForSigning(nonce, from, type, data);
89 const sig = await connector.signPersonalMessage([hash, wallet.address]);
92 const hash = await api.getUnsignedTx(nonce, from, type, data);
93 const sig = await signTransactionHash(hash, wallet.privateKey);
98export const configureKeys = async (
107 connector: WalletConnect | null,
109 force: boolean = false
112 ship: point, //ship to configure keys
116 // TODO: do something here?
117 if (Nothing.hasInstance(networkSeed)) {
118 console.log("Network key Error: couldn't derive network keys");
119 // throw new Error("Network key Error: couldn't derive network keys");
122 const seed = Just.hasInstance(networkSeed)
126 const pair = deriveNetworkKeys(seed);
129 encrypt: addHexPrefix(pair.crypt.public),
130 auth: addHexPrefix(pair.auth.public),
131 cryptoSuite: String(CRYPTO_SUITE_VERSION),
132 breach: breach || false,
135 const sig = await generateHashAndSign(
146 return api.configureKeys(sig, force, from, wallet.address, data);
149export const transferPointRequest = async (
152 pointToTransfer: Ship,
158 connector: WalletConnect | null,
160 force: boolean = false
163 ship: pointToTransfer,
169 reset: reset || false,
172 const sig = await generateHashAndSign(
184 return api.transferPoint(sig, force, from, wallet.address, data);
187const proxyType = (proxy: Proxy) => {
190 return 'setManagementProxy';
192 return 'setSpawnProxy';
194 return 'setTransferProxy';
196 throw new Error(`Unknown proxyType ${proxy}`);
200const setProxy = async (
202 proxyAddressType: string,
207 force: boolean = false
209 switch (proxyAddressType) {
211 return await api.setManagementProxy(sig, force, from, address, data);
213 return await api.setSpawnProxy(sig, force, from, address, data);
215 return await api.setTransferProxy(sig, force, from, address, data);
217 throw new Error(`Unknown proxyType ${proxyAddressType}`);
221export const registerProxyAddress = async (
223 wallet: any, // BridgeWallet
226 proxyAddressType: string,
231 connector: WalletConnect | null,
238 const data = { address };
240 const sig = await generateHashAndSign(
245 proxyType(proxyAddressType),
263const getDataAndMethod = (args: L2TransactionArgs) => {
275 const result: TransactionData = { data: { address }, method: async () => '' };
278 case 'transferPoint':
279 result.method = api.transferPoint;
280 result.data.reset = reset || false;
283 result.method = api.spawn;
284 result.data.ship = pointToSpawn;
286 case 'configureKeys':
287 // TODO: do something here?
288 if (Nothing.hasInstance(networkSeed)) {
289 console.log("Network key Error: couldn't derive network keys");
290 // throw new Error("Network key Error: couldn't derive network keys");
293 const seed = Just.hasInstance(networkSeed)
297 const pair = deriveNetworkKeys(seed);
300 encrypt: addHexPrefix(pair.crypt.public),
301 auth: addHexPrefix(pair.auth.public),
302 cryptoSuite: String(CRYPTO_SUITE_VERSION),
303 breach: breach || false,
305 result.method = api.configureKeys;
308 result.method = api.escape;
309 result.data.ship = newSponsor;
310 delete result.data.address;
313 result.method = api.cancelEscape;
314 result.data.ship = ship;
315 delete result.data.address;
318 result.method = api.adopt;
319 result.data.ship = sponsee;
320 delete result.data.address;
323 result.method = api.detach;
324 result.data.ship = sponsee;
325 delete result.data.address;
328 result.method = api.reject;
329 result.data.ship = sponsee;
330 delete result.data.address;
335export const submitL2Transaction = async (args: L2TransactionArgs) => {
348 const from = { ship, proxy };
349 const { data, method } = getDataAndMethod(args);
351 const sig = await generateHashAndSign(
363 return method(sig, false, from, wallet.address, data);
366export const reticketL2Point = async () => {};
368export const hasInvite = (point: number) => (invite: Invite) =>
369 invite.planet === point && invite.ticket.length > 0;
371function isSpawn(tx: L2Data | undefined): tx is SpawnParams {
372 return (tx as SpawnParams) !== undefined;
375function isShipNumber(ship: number | string | undefined): ship is number {
376 return (ship as number) !== undefined;
379export const getPendingSpawns = (pendingTxs: PendingTransaction[]) =>
380 pendingTxs.reduce((acc, pending) => {
381 return isSpawn(pending.rawTx?.tx) &&
382 isShipNumber(pending.rawTx?.tx.data.ship)
383 ? acc.add(pending.rawTx?.tx.data.ship!)
385 }, 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