on 5a2bc24 help sign in
bridge/src/lib/utils/roller.ts
165 lines 4.1 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 attemptNetworkSeedDerivation,
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 details: any,
70 authToken: string,
71 authMnemonic: string,
72 wallet: any,
73 urbitWallet: any
74) => {
75 const from = {
76 ship: _point, //ship to configure keys
77 proxy,
78 };
80 const networkSeed = await attemptNetworkSeedDerivation({
81 urbitWallet,
82 wallet,
83 authMnemonic,
84 details,
85 point: _point,
86 authToken,
87 revision: 1,
88 });
89 // TODO: do something here?
90 if (Nothing.hasInstance(networkSeed)) {
91 console.log("Network key Error: couldn't derive network keys");
92 // throw new Error("Network key Error: couldn't derive network keys");
93 }
95 const seed = Just.hasInstance(networkSeed)
96 ? networkSeed.value
97 : randomHex(32);
99 const pair = deriveNetworkKeys(seed);
101 const data = {
102 encrypt: addHexPrefix(pair.crypt.public),
103 auth: addHexPrefix(pair.auth.public),
104 cryptoSuite: String(CRYPTO_SUITE_VERSION),
105 breach: false,
106 };
108 const hash = await api.hashTransaction(nonce, from, 'configureKeys', data);
109 const sig = signTransactionHash(hash, _wallet.privateKey);
110 return api.configureKeys(sig, from, _wallet.address, data);
111};
113export const transferPoint = async (
114 api: any,
115 _wallet: any,
116 _point: number,
117 proxy: string,
118 nonce: number,
119 address: string
120) => {
121 const from = {
122 ship: _point, // ship to transfer
123 proxy,
124 };
126 const data = {
127 address,
128 reset: false,
129 };
131 const hash = await api.hashTransaction(nonce, from, 'transferPoint', data);
132 const sig = signTransactionHash(hash, _wallet.privateKey);
133 return api.transferPoint(sig, from, _wallet.address, data);
134};
136export const setSpawnProxy = async (
137 api: any,
138 _wallet: any,
139 _point: number,
140 proxy: string,
141 nonce: number,
142 address: string
143) => {
144 const from = {
145 ship: _point,
146 proxy,
147 };
148 const managementData = { address };
149 const managementHash = await api.hashTransaction(
150 nonce,
151 from,
152 'setManagementProxy',
153 managementData
154 );
156 return api.setManagementProxy(
157 signTransactionHash(managementHash, _wallet.privateKey),
158 from,
159 _wallet.address,
160 managementData
161 );
162};
164export const hasPoint = (point: number) => (invite: Invite) =>
165 invite.planet === point;