on 40b752c help sign in
bridge/src/lib/utils/roller.ts
124 lines 3.0 kB history raw
1import { signTransactionHash } from 'lib/authToken';
2import { Invite } from 'lib/types/Invite';
3import { Just } 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';
12export const SECOND = 1000;
13export const MINUTE = SECOND * 60;
14export const HOUR = MINUTE * 60;
16export const padZero = (amount: number) =>
17 `${amount < 10 && amount > 0 ? '0' : ''}${amount}`;
19export const getTimeToNextBatch = (nextBatch: number, now: number) => {
20 const toNext = nextBatch - now;
21 const hours = Math.floor(toNext / HOUR);
22 const minutes = Math.floor((toNext - hours * HOUR) / MINUTE);
23 const seconds = Math.floor(
24 (toNext - hours * HOUR - minutes * MINUTE) / SECOND
25 );
27 return `${padZero(hours)}h ${padZero(minutes)}m ${padZero(seconds)}s`;
28};
30export const isL2 = (dom?: string) => dom === 'l2' || dom === 'spawn';
32export const spawn = async (
33 api: any,
34 _wallet: any,
35 _point: number,
36 proxy: string,
37 nonce: number,
38 planet: number
39) => {
40 const from = {
41 ship: _point, //ship that is spawning the planet
42 proxy,
43 };
45 const data = {
46 address: _wallet.address,
47 ship: planet, // ship to spawn
48 };
50 const hash = await api.hashTransaction(nonce, from, 'spawn', data);
51 const sSig = signTransactionHash(hash, _wallet.privateKey);
52 return api.spawn(sSig, from, _wallet.address, data);
53};
55export const configureKeys = async (
56 api: any,
57 _wallet: any,
58 _point: number,
59 proxy: string,
60 nonce: number,
61 details: any,
62 authToken: string,
63 authMnemonic: string,
64 wallet: any,
65 urbitWallet: any
66) => {
67 const from = {
68 ship: _point, //ship to configure keys
69 proxy,
70 };
72 const networkSeed = await attemptNetworkSeedDerivation({
73 urbitWallet,
74 wallet,
75 authMnemonic,
76 details,
77 point: _point,
78 authToken,
79 revision: 1,
80 });
82 const seed = Just.hasInstance(networkSeed)
83 ? networkSeed.value
84 : randomHex(32);
86 const pair = deriveNetworkKeys(seed);
88 const data = {
89 encrypt: addHexPrefix(pair.crypt.public),
90 auth: addHexPrefix(pair.auth.public),
91 cryptoSuite: String(CRYPTO_SUITE_VERSION),
92 breach: false,
93 };
95 const hash = await api.hashTransaction(nonce, from, 'configureKeys', data);
96 const sig = signTransactionHash(hash, _wallet.privateKey);
97 return api.configureKeys(sig, from, _wallet.address, data);
98};
100export const transferPoint = async (
101 api: any,
102 _wallet: any,
103 _point: number,
104 proxy: string,
105 nonce: number,
106 address: string
107) => {
108 const from = {
109 ship: _point, // ship to transfer
110 proxy,
111 };
113 const data = {
114 address,
115 reset: false,
116 };
118 const hash = await api.hashTransaction(nonce, from, 'transferPoint', data);
119 const sig = signTransactionHash(hash, _wallet.privateKey);
120 return api.transferPoint(sig, from, _wallet.address, data);
121};
123export const hasPoint = (point: number) => (invite: Invite) =>
124 invite.planet === point;