on 2d3487f help sign in
bridge/src/lib/utils/nonce.ts
157 lines 3.9 kB history raw
1import { Ownership } from '@urbit/roller-api';
3export const getProxyAndNonce = (
4 owner: Ownership,
5 address: string
6): { proxy?: string; nonce?: number } =>
7 owner.managementProxy?.address === address
8 ? { proxy: 'manage', nonce: owner.managementProxy.nonce }
9 : owner.owner?.address === address
10 ? { proxy: 'own', nonce: owner.owner.nonce }
11 : owner.spawnProxy?.address === address
12 ? { proxy: 'spawn', nonce: owner.spawn?.owner.nonce }
13 : owner.votingProxy?.address === address
14 ? { proxy: 'vote', nonce: owner.votingProxy?.owner.nonce }
15 : owner.transferProxy?.address === address
16 ? {
17 proxy: 'transfer',
18 nonce: owner.votingProxy?.nonce,
19 }
20 : { proxy: undefined, nonce: undefined };
22export const getManagementNonce = (
23 owner: Ownership,
24 address: string
25): { nonce?: number; proxy?: string } | undefined =>
26 owner.managementProxy?.address === address
27 ? { nonce: owner.managementProxy.nonce, proxy: 'manage' }
28 : undefined;
30export const getTransferNonce = (
31 owner: Ownership,
32 address: string
33): { nonce?: number; proxy?: string } | undefined =>
34 owner.transferProxy?.address === address
35 ? { nonce: owner.transferProxy.nonce, proxy: 'transfer' }
36 : undefined;
38export const getOwnerNonce = (
39 owner: Ownership,
40 address: string
41): { nonce?: number; proxy?: string } | undefined =>
42 owner.owner?.address === address
43 ? { nonce: owner.owner.nonce, proxy: 'own' }
44 : undefined;
46export const getSpawnNonce = (
47 owner: Ownership,
48 address: string
49): { nonce?: number; proxy?: string } | undefined =>
50 owner.spawnProxy?.address === address
51 ? { nonce: owner.spawnProxy.nonce, proxy: 'spawn' }
52 : undefined;
54export const getVotingNonce = (
55 owner: Ownership,
56 address: string
57): { nonce?: number; proxy?: string } | undefined =>
58 owner.votingProxy?.address === address
59 ? { nonce: owner.votingProxy.nonce, proxy: 'vote' }
60 : undefined;
62export const increaseProxyNonce = (
63 owner: Ownership,
64 proxy: string
65): Ownership | undefined =>
66 'manage' === proxy
67 ? {
68 ...owner,
69 managementProxy: {
70 ...owner.managementProxy,
71 nonce: owner.managementProxy?.nonce
72 ? owner.managementProxy?.nonce + 1
73 : 1,
74 },
75 }
76 : 'own' === proxy
77 ? {
78 ...owner,
79 owner: {
80 ...owner.owner,
81 nonce: owner.owner?.nonce ? owner.owner?.nonce + 1 : 1,
82 },
83 }
84 : 'spawn' === proxy
85 ? {
86 ...owner,
87 spawnProxy: {
88 ...owner.spawnProxy,
89 nonce: owner.spawnProxy?.nonce ? owner.spawnProxy?.nonce + 1 : 1,
90 },
91 }
92 : 'vote' === proxy
93 ? {
94 ...owner,
95 votingProxy: {
96 ...owner.votingProxy,
97 nonce: owner.votingProxy?.nonce ? owner.votingProxy?.nonce + 1 : 1,
98 },
99 }
100 : 'transfer' === proxy
101 ? {
102 ...owner,
103 transferProxy: {
104 ...owner.transferProxy,
105 nonce: owner.transferProxy?.nonce
106 ? owner.transferProxy?.nonce + 1
107 : 1,
108 },
109 }
110 : undefined;
112export const setProxyNonce = (
113 owner: Ownership,
114 proxy: string,
115 nonce: number
116): Ownership | undefined =>
117 'manage' === proxy
118 ? {
119 ...owner,
120 managementProxy: {
121 ...owner.managementProxy,
122 nonce,
123 },
124 }
125 : 'own' === proxy
126 ? {
127 ...owner,
128 owner: {
129 ...owner.owner,
130 nonce,
131 },
132 }
133 : 'spawn' === proxy
134 ? {
135 ...owner,
136 spawnProxy: {
137 ...owner.spawnProxy,
138 nonce,
139 },
140 }
141 : 'vote' === proxy
142 ? {
143 ...owner,
144 votingProxy: {
145 ...owner.voteProxy,
146 nonce,
147 },
148 }
149 : 'transfer' === proxy
150 ? {
151 ...owner,
152 transferProxy: {
153 ...owner.transferProxy,
154 nonce,
155 },
156 }
157 : undefined;