on 5cc7169 help sign in
bridge/src/lib/utils/roller.ts
202 lines 5.1 kB history raw
1import { Just, Nothing } from 'folktale/maybe';
2import { Invite } from 'lib/types/Invite';
3import { signTransactionHash } from 'lib/authToken';
4import { randomHex } from 'web3-utils';
6import RollerRPCAPI, {
7 Proxy,
8 Signature,
9 From,
10 EthAddress,
11 AddressParams,
12 Ship,
13} from '@urbit/roller-api';
15import { deriveNetworkKeys, CRYPTO_SUITE_VERSION } from 'lib/keys';
16import { addHexPrefix } from 'lib/utils/address';
17import { makeDeterministicTicket, generateWallet } from 'lib/walletgen';
19export const SECOND = 1000;
20export const MINUTE = SECOND * 60;
21export const HOUR = MINUTE * 60;
23export const padZero = (amount: number) =>
24 `${amount < 10 && amount > 0 ? '0' : ''}${amount}`;
26export const hasPoint = (point: number) => (invite: Invite) =>
27 invite.planet === point;
29export const getTimeToNextBatch = (nextBatch: number, now: number) => {
30 const toNext = nextBatch - now;
31 const hours = Math.floor(toNext / HOUR);
32 const minutes = Math.floor((toNext - hours * HOUR) / MINUTE);
33 const seconds = Math.floor(
34 (toNext - hours * HOUR - minutes * MINUTE) / SECOND
35 );
37 return `${padZero(hours)}h ${padZero(minutes)}m ${padZero(seconds)}s`;
38};
40export const isL2 = (dom?: string) => dom === 'l2' || dom === 'spawn';
42export const isL2Spawn = (dom?: string) => dom === 'spawn';
44export const generateInviteWallet = async (point: number, seed: string) => {
45 const ticket = makeDeterministicTicket(point, seed);
46 const inviteWallet = await generateWallet(point, ticket, true);
48 return { ticket, inviteWallet };
49};
51export const spawn = async (
52 api: RollerRPCAPI,
53 _wallet: any,
54 _point: number,
55 proxy: string,
56 nonce: number,
57 planet: number
58) => {
59 const from = {
60 ship: _point, //ship that is spawning the planet
61 proxy,
62 };
64 const data = {
65 address: _wallet.address,
66 ship: planet, // ship to spawn
67 };
69 const hash = await api.hashTransaction(nonce, from, 'spawn', data);
70 const sSig = signTransactionHash(hash, _wallet.privateKey);
71 return api.spawn(sSig, from, _wallet.address, data);
72};
74export const configureKeys = async (
75 api: RollerRPCAPI,
76 wallet: any,
77 point: Ship,
78 proxy: string,
79 nonce: number,
80 networkSeed: any,
81 breach?: boolean
82) => {
83 const from = {
84 ship: point, //ship to configure keys
85 proxy,
86 };
88 // TODO: do something here?
89 if (Nothing.hasInstance(networkSeed)) {
90 console.log("Network key Error: couldn't derive network keys");
91 // throw new Error("Network key Error: couldn't derive network keys");
92 }
94 const seed = Just.hasInstance(networkSeed)
95 ? networkSeed.value
96 : randomHex(32);
98 const pair = deriveNetworkKeys(seed);
100 const data = {
101 encrypt: addHexPrefix(pair.crypt.public),
102 auth: addHexPrefix(pair.auth.public),
103 cryptoSuite: String(CRYPTO_SUITE_VERSION),
104 breach: breach || false,
105 };
107 const hash = await api.hashTransaction(nonce, from, 'configureKeys', data);
108 const sig = signTransactionHash(hash, wallet.privateKey);
109 return api.configureKeys(sig, from, wallet.address, data);
110};
112export const transferPointRequest = async (
113 api: RollerRPCAPI,
114 _wallet: any,
115 pointToTransfer: Ship,
116 proxy: string,
117 nonce: number,
118 address: string,
119 reset?: boolean
120) => {
121 const from = {
122 ship: pointToTransfer,
123 proxy,
124 };
126 const data = {
127 address,
128 reset: reset || false,
129 };
130 const hash = await api.hashTransaction(nonce, from, 'transferPoint', data);
131 const sig = signTransactionHash(hash, _wallet.privateKey);
132 return api.transferPoint(sig, from, _wallet.address, data);
133};
135const proxyType = (proxy: Proxy) => {
136 switch (proxy) {
137 case 'manage':
138 return 'setManagementProxy';
139 case 'spawn':
140 return 'setSpawnProxy';
141 case 'transfer':
142 return 'setTransferProxy';
143 default:
144 throw new Error(`Unknown proxyType ${proxy}`);
145 }
146};
148const setProxy = async (
149 api: RollerRPCAPI,
150 proxyAddressType: string,
151 sig: Signature,
152 from: From,
153 address: EthAddress,
154 data: AddressParams
155) => {
156 switch (proxyAddressType) {
157 case 'manage':
158 return await api.setManagementProxy(sig, from, address, data);
159 case 'spawn':
160 return await api.setSpawnProxy(sig, from, address, data);
161 case 'transfer':
162 return await api.setTransferProxy(sig, from, address, data);
163 default:
164 throw new Error(`Unknown proxyType ${proxyAddressType}`);
165 }
166};
168export const registerProxyAddress = async (
169 api: RollerRPCAPI,
170 _wallet: any,
171 _point: Ship,
172 proxy: string,
173 proxyAddressType: string,
174 nonce: number,
175 address: string
176) => {
177 const from = {
178 ship: _point,
179 proxy,
180 };
181 const proxyData = { address };
182 const proxyHash = await api.hashTransaction(
183 nonce,
184 from,
185 proxyType(proxyAddressType),
186 proxyData
187 );
189 return setProxy(
190 api,
191 proxyAddressType,
192 signTransactionHash(proxyHash, _wallet.privateKey),
193 from,
194 _wallet.address,
195 proxyData
196 );
197};
199export const reticketL2Point = async () => {};
201export const hasInvite = (point: number) => (invite: Invite) =>
202 invite.planet === point && invite.ticket.length > 0;