-
Notifications
You must be signed in to change notification settings - Fork 88
/
resolve.ts
231 lines (201 loc) · 7.46 KB
/
resolve.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
import type { IdentifierArray } from '@wallet-standard/core';
import { NativeModules, Platform } from 'react-native';
const LINKING_ERROR =
`The package 'solana-mobile-wallet-adapter-walletlib' doesn't seem to be linked. Make sure: \n\n` +
'- You rebuilt the app after installing the package\n' +
'- If you are using Lerna workspaces\n' +
' - You have added `@solana-mobile/mobile-wallet-adapter-walletlib` as an explicit dependency, and\n' +
' - You have added `@solana-mobile/mobile-wallet-adapter-walletlib` to the `nohoist` section of your package.json\n' +
'- You are not using Expo managed workflow\n';
const SolanaMobileWalletAdapterWalletLib =
Platform.OS === 'android' && NativeModules.SolanaMobileWalletAdapterWalletLib
? NativeModules.SolanaMobileWalletAdapterWalletLib
: new Proxy(
{},
{
get() {
throw new Error(
Platform.OS !== 'android'
? 'The package `solana-mobile-wallet-adapter-walletlib` is only compatible with React Native Android'
: LINKING_ERROR,
);
},
},
);
type AppIdentity = Readonly<{
identityUri?: string;
iconRelativeUri?: string;
identityName?: string;
}>;
export type SignInPayload = Readonly<{
domain?: string;
address?: string;
statement?: string;
uri?: string;
version?: string;
chainId?: string;
nonce?: string;
issuedAt?: string;
expirationTime?: string;
notBefore?: string;
requestId?: string;
resources?: readonly string[];
}>
export type Base64EncodedAddress = string;
type Base64EncodedSignedMessage = string;
/**
* Mobile Wallet Adapter Requests are remote requests coming from
* the dApp for authorization, signing, and sending services.
*/
export type MWARequest =
| SignMessagesRequest
| SignTransactionsRequest
| SignAndSendTransactionsRequest
| AuthorizeDappRequest
| ReauthorizeDappRequest
| DeauthorizeDappRequest;
export enum MWARequestType {
AuthorizeDappRequest = 'AUTHORIZE_DAPP',
ReauthorizeDappRequest = 'REAUTHORIZE_DAPP',
DeauthorizeDappRequest = 'DEAUTHORIZE_DAPP',
SignMessagesRequest = 'SIGN_MESSAGES',
SignTransactionsRequest = 'SIGN_TRANSACTIONS',
SignAndSendTransactionsRequest = 'SIGN_AND_SEND_TRANSACTIONS',
}
interface IMWARequest {
__type: MWARequestType;
requestId: string;
sessionId: string;
}
interface IVerifiableIdentityRequest {
chain: string;
authorizationScope: Uint8Array;
appIdentity?: AppIdentity;
}
export type AuthorizeDappRequest = Readonly<{
__type: MWARequestType.AuthorizeDappRequest;
chain: string;
appIdentity?: AppIdentity;
features?: IdentifierArray;
addresses?: [String];
signInPayload?: SignInPayload;
}> &
IMWARequest;
export type ReauthorizeDappRequest = Readonly<{
__type: MWARequestType.ReauthorizeDappRequest;
}> &
IMWARequest &
IVerifiableIdentityRequest;
export type DeauthorizeDappRequest = Readonly<{
__type: MWARequestType.DeauthorizeDappRequest;
}> &
IMWARequest &
IVerifiableIdentityRequest;
export type SignMessagesRequest = Readonly<{
__type: MWARequestType.SignMessagesRequest;
payloads: Uint8Array[];
}> &
IMWARequest &
IVerifiableIdentityRequest;
export type SignTransactionsRequest = Readonly<{
__type: MWARequestType.SignTransactionsRequest;
payloads: Uint8Array[];
}> &
IMWARequest &
IVerifiableIdentityRequest;
export type SignAndSendTransactionsRequest = Readonly<{
__type: MWARequestType.SignAndSendTransactionsRequest;
payloads: Uint8Array[];
minContextSlot?: number;
commitment?: string;
skipPreflight?: boolean;
maxRetries?: number;
waitForCommitmentToSendNextTransaction?: boolean;
}> &
IMWARequest &
IVerifiableIdentityRequest;
/**
* MWA Request Responses
*/
export type MWAResponse =
| AuthorizeDappResponse
| ReauthorizeDappResponse
| DeauthorizeDappResponse
| SignMessagesResponse
| SignTransactionsResponse
| SignAndSendTransactionsResponse;
/* Failure Responses */
export enum MWARequestFailReason {
UserDeclined = 'USER_DECLINED',
TooManyPayloads = 'TOO_MANY_PAYLOADS',
InvalidSignatures = 'INVALID_SIGNATURES',
AuthorizationNotValid = 'AUTHORIZATION_NOT_VALID',
}
export type UserDeclinedResponse = Readonly<{
failReason: MWARequestFailReason.UserDeclined;
}>;
export type TooManyPayloadsResponse = Readonly<{
failReason: MWARequestFailReason.TooManyPayloads;
}>;
export type AuthorizationNotValidResponse = Readonly<{
failReason: MWARequestFailReason.AuthorizationNotValid;
}>;
export type InvalidSignaturesResponse = Readonly<{
failReason: MWARequestFailReason.InvalidSignatures;
valid: boolean[];
}>;
/* Authorize Dapp */
export type AuthorizedAccount = Readonly<{
publicKey: Base64EncodedAddress;
accountLabel?: string;
icon?: string;
chains?: IdentifierArray;
features?: IdentifierArray;
}>;
export type SignInResult = Readonly<{
address: Base64EncodedAddress;
signed_message: Base64EncodedSignedMessage;
signature: Base64EncodedAddress;
signature_type?: string;
}>;
export type AuthorizeDappCompleteResponse = Readonly<{
accounts: Array<AuthorizedAccount>
walletUriBase?: string;
authorizationScope?: Uint8Array;
signInResult?: SignInResult;
}>;
export type AuthorizeDappResponse = AuthorizeDappCompleteResponse | UserDeclinedResponse;
/* Reauthorize Dapp */
export type ReauthorizeDappCompleteResponse = Readonly<{
authorizationScope?: Uint8Array;
}>;
export type ReauthorizeDappResponse = ReauthorizeDappCompleteResponse | AuthorizationNotValidResponse;
/* Deauthorize Dapp */
export type DeauthorizeDappCompleteResponse = Readonly<{}>;
export type DeauthorizeDappResponse = DeauthorizeDappCompleteResponse | AuthorizationNotValidResponse;
/* Sign Transactions/Messages */
export type SignPayloadsCompleteResponse = Readonly<{ signedPayloads: Uint8Array[] }>;
export type SignPayloadsFailResponse =
| UserDeclinedResponse
| TooManyPayloadsResponse
| AuthorizationNotValidResponse
| InvalidSignaturesResponse;
export type SignTransactionsResponse = SignPayloadsCompleteResponse | SignPayloadsFailResponse;
export type SignMessagesResponse = SignPayloadsCompleteResponse | SignPayloadsFailResponse;
/* Sign and Send Transaction */
export type SignAndSendTransactionsCompleteResponse = Readonly<{ signedTransactions: Uint8Array[] }>;
export type SignAndSendTransactionsResponse =
| SignAndSendTransactionsCompleteResponse
| UserDeclinedResponse
| TooManyPayloadsResponse
| AuthorizationNotValidResponse
| InvalidSignaturesResponse;
export function resolve(request: AuthorizeDappRequest, response: AuthorizeDappResponse): void;
export function resolve(request: ReauthorizeDappRequest, response: ReauthorizeDappResponse): void;
export function resolve(request: DeauthorizeDappRequest, response: DeauthorizeDappResponse): void;
export function resolve(request: SignMessagesRequest, response: SignMessagesResponse): void;
export function resolve(request: SignTransactionsRequest, response: SignTransactionsResponse): void;
export function resolve(request: SignAndSendTransactionsRequest, response: SignAndSendTransactionsResponse): void;
export function resolve(request: MWARequest, response: MWAResponse): void {
SolanaMobileWalletAdapterWalletLib.resolve(JSON.stringify(request), JSON.stringify(response));
}