on 2b07680 help sign in
bridge/src/lib/utils/roller.ts
153 lines 3.9 kB history raw
1import { signTransactionHash } from 'lib/authToken';
2import { Invite } from 'lib/types/Invite';
3import { Just, Nothing } from 'folktale/maybe';
4import { randomHex } from 'web3-utils';
5import {
6 deriveNetworkSeedFromUrbitWallet,
7 deriveNetworkKeys,
8 CRYPTO_SUITE_VERSION,
9} from 'lib/keys';
10import { addHexPrefix } from 'lib/utils/address';
11import { makeDeterministicTicket, generateWallet } from 'lib/walletgen';
13export const SECOND = 1000;
14export const MINUTE = SECOND * 60;
15export const HOUR = MINUTE * 60;
17export const padZero = (amount: number) =>
18 `${amount < 10 && amount > 0 ? '0' : ''}${amount}`;
20export const getTimeToNextBatch = (nextBatch: number, now: number) => {
21 const toNext = nextBatch - now;
22 const hours = Math.floor(toNext / HOUR);
23 const minutes = Math.floor((toNext - hours * HOUR) / MINUTE);
24 const seconds = Math.floor(
25 (toNext - hours * HOUR - minutes * MINUTE) / SECOND
26 );
28 return `${padZero(hours)}h ${padZero(minutes)}m ${padZero(seconds)}s`;
29};
31export const isL2 = (dom?: string) => dom === 'l2' || dom === 'spawn';
33export const generateInviteWallet = async (point: number, seed: string) => {
34 const ticket = makeDeterministicTicket(point, seed);
35 const inviteWallet = await generateWallet(point, ticket, true);
37 return { ticket, inviteWallet };
38};
40export const spawn = async (
41 api: any,
42 _wallet: any,
43 _point: number,
44 proxy: string,
45 nonce: number,
46 planet: number
47) => {
48 const from = {
49 ship: _point, //ship that is spawning the planet
50 proxy,
51 };
53 const data = {
54 address: _wallet.address,
55 ship: planet, // ship to spawn
56 };
58 const hash = await api.hashTransaction(nonce, from, 'spawn', data);
59 const sSig = signTransactionHash(hash, _wallet.privateKey);
60 return api.spawn(sSig, from, _wallet.address, data);
61};
63export const configureKeys = async (
64 api: any,
65 _wallet: any,
66 _point: number,
67 proxy: string,
68 nonce: number,
69 urbitWallet: any
70) => {
71 const from = {
72 ship: _point, //ship to configure keys
73 proxy,
74 };
76 const networkSeed = await deriveNetworkSeedFromUrbitWallet(urbitWallet, 1);
77 // TODO: do something here?
78 if (Nothing.hasInstance(networkSeed)) {
79 console.log("Network key Error: couldn't derive network keys");
80 // throw new Error("Network key Error: couldn't derive network keys");
81 }
83 const seed = Just.hasInstance(networkSeed)
84 ? networkSeed.value
85 : randomHex(32);
87 const pair = deriveNetworkKeys(seed);
89 const data = {
90 encrypt: addHexPrefix(pair.crypt.public),
91 auth: addHexPrefix(pair.auth.public),
92 cryptoSuite: String(CRYPTO_SUITE_VERSION),
93 breach: false,
94 };
96 const hash = await api.hashTransaction(nonce, from, 'configureKeys', data);
97 const sig = signTransactionHash(hash, _wallet.privateKey);
98 return api.configureKeys(sig, from, _wallet.address, data);
99};
101export const transferPoint = async (
102 api: any,
103 _wallet: any,
104 _point: number,
105 proxy: string,
106 nonce: number,
107 address: string
108) => {
109 const from = {
110 ship: _point, // ship to transfer
111 proxy,
112 };
114 const data = {
115 address,
116 reset: false,
117 };
119 const hash = await api.hashTransaction(nonce, from, 'transferPoint', data);
120 const sig = signTransactionHash(hash, _wallet.privateKey);
121 return api.transferPoint(sig, from, _wallet.address, data);
122};
124export const setSpawnProxy = async (
125 api: any,
126 _wallet: any,
127 _point: number,
128 proxy: string,
129 nonce: number,
130 address: string
131) => {
132 const from = {
133 ship: _point,
134 proxy,
135 };
136 const managementData = { address };
137 const managementHash = await api.hashTransaction(
138 nonce,
139 from,
140 'setManagementProxy',
141 managementData
142 );
144 return api.setManagementProxy(
145 signTransactionHash(managementHash, _wallet.privateKey),
146 from,
147 _wallet.address,
148 managementData
149 );
150};
152export const hasPoint = (point: number) => (invite: Invite) =>
153 invite.planet === point;