-
Notifications
You must be signed in to change notification settings - Fork 44
/
signIn.ts
184 lines (168 loc) · 6.07 KB
/
signIn.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
import type { SolanaSignInInput, SolanaSignInOutput } from '@solana/wallet-standard-features';
import { verifyMessageSignature } from './signMessage.js';
import { arraysEqual } from './util.js';
/**
* TODO: docs
*/
export function verifySignIn(input: SolanaSignInInput, output: SolanaSignInOutput): boolean {
const {
signedMessage,
signature,
account: { publicKey },
} = output;
const message = deriveSignInMessage(input, output);
return !!message && verifyMessageSignature({ message, signedMessage, signature, publicKey });
}
/**
* TODO: docs
*/
export function deriveSignInMessage(input: SolanaSignInInput, output: SolanaSignInOutput): Uint8Array | null {
const text = deriveSignInMessageText(input, output);
if (!text) return null;
return new TextEncoder().encode(text);
}
/**
* TODO: docs
*/
export function deriveSignInMessageText(input: SolanaSignInInput, output: SolanaSignInOutput): string | null {
const parsed = parseSignInMessage(output.signedMessage);
if (!parsed) return null;
if (input.domain && input.domain !== parsed.domain) return null;
if (input.address && input.address !== parsed.address) return null;
if (input.statement !== parsed.statement) return null;
if (input.uri !== parsed.uri) return null;
if (input.version !== parsed.version) return null;
if (input.chainId !== parsed.chainId) return null;
if (input.nonce !== parsed.nonce) return null;
if (input.issuedAt !== parsed.issuedAt) return null;
if (input.expirationTime !== parsed.expirationTime) return null;
if (input.notBefore !== parsed.notBefore) return null;
if (input.requestId !== parsed.requestId) return null;
if (input.resources) {
if (!parsed.resources) return null;
if (!arraysEqual(input.resources, parsed.resources)) return null;
} else if (parsed.resources) return null;
return createSignInMessageText(parsed);
}
/**
* TODO: docs
*/
export type SolanaSignInInputWithRequiredFields = SolanaSignInInput &
Required<Pick<SolanaSignInInput, 'domain' | 'address'>>;
/**
* TODO: docs
*/
export function parseSignInMessage(message: Uint8Array): SolanaSignInInputWithRequiredFields | null {
const text = new TextDecoder().decode(message);
return parseSignInMessageText(text);
}
// TODO: implement https://github.com/solana-labs/solana/blob/master/docs/src/proposals/off-chain-message-signing.md
const DOMAIN = '(?<domain>[^\\n]+?) wants you to sign in with your Solana account:\\n';
const ADDRESS = '(?<address>[^\\n]+)(?:\\n|$)';
const STATEMENT = '(?:\\n(?<statement>[\\S\\s]*?)(?:\\n|$))??';
const URI = '(?:\\nURI: (?<uri>[^\\n]+))?';
const VERSION = '(?:\\nVersion: (?<version>[^\\n]+))?';
const CHAIN_ID = '(?:\\nChain ID: (?<chainId>[^\\n]+))?';
const NONCE = '(?:\\nNonce: (?<nonce>[^\\n]+))?';
const ISSUED_AT = '(?:\\nIssued At: (?<issuedAt>[^\\n]+))?';
const EXPIRATION_TIME = '(?:\\nExpiration Time: (?<expirationTime>[^\\n]+))?';
const NOT_BEFORE = '(?:\\nNot Before: (?<notBefore>[^\\n]+))?';
const REQUEST_ID = '(?:\\nRequest ID: (?<requestId>[^\\n]+))?';
const RESOURCES = '(?:\\nResources:(?<resources>(?:\\n- [^\\n]+)*))?';
const FIELDS = `${URI}${VERSION}${CHAIN_ID}${NONCE}${ISSUED_AT}${EXPIRATION_TIME}${NOT_BEFORE}${REQUEST_ID}${RESOURCES}`;
const MESSAGE = new RegExp(`^${DOMAIN}${ADDRESS}${STATEMENT}${FIELDS}\\n*$`);
/**
* TODO: docs
*/
export function parseSignInMessageText(text: string): SolanaSignInInputWithRequiredFields | null {
const match = MESSAGE.exec(text);
if (!match) return null;
const groups = match.groups;
if (!groups) return null;
return {
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion
domain: groups.domain!,
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion
address: groups.address!,
statement: groups.statement,
uri: groups.uri,
version: groups.version,
nonce: groups.nonce,
chainId: groups.chainId,
issuedAt: groups.issuedAt,
expirationTime: groups.expirationTime,
notBefore: groups.notBefore,
requestId: groups.requestId,
resources: groups.resources?.split('\n- ').slice(1),
};
}
/**
* TODO: docs
*/
export function createSignInMessage(input: SolanaSignInInputWithRequiredFields): Uint8Array {
const text = createSignInMessageText(input);
return new TextEncoder().encode(text);
}
/**
* TODO: docs
*/
export function createSignInMessageText(input: SolanaSignInInputWithRequiredFields): string {
// ${domain} wants you to sign in with your Solana account:
// ${address}
//
// ${statement}
//
// URI: ${uri}
// Version: ${version}
// Chain ID: ${chain}
// Nonce: ${nonce}
// Issued At: ${issued-at}
// Expiration Time: ${expiration-time}
// Not Before: ${not-before}
// Request ID: ${request-id}
// Resources:
// - ${resources[0]}
// - ${resources[1]}
// ...
// - ${resources[n]}
let message = `${input.domain} wants you to sign in with your Solana account:\n`;
message += `${input.address}`;
if (input.statement) {
message += `\n\n${input.statement}`;
}
const fields: string[] = [];
if (input.uri) {
fields.push(`URI: ${input.uri}`);
}
if (input.version) {
fields.push(`Version: ${input.version}`);
}
if (input.chainId) {
fields.push(`Chain ID: ${input.chainId}`);
}
if (input.nonce) {
fields.push(`Nonce: ${input.nonce}`);
}
if (input.issuedAt) {
fields.push(`Issued At: ${input.issuedAt}`);
}
if (input.expirationTime) {
fields.push(`Expiration Time: ${input.expirationTime}`);
}
if (input.notBefore) {
fields.push(`Not Before: ${input.notBefore}`);
}
if (input.requestId) {
fields.push(`Request ID: ${input.requestId}`);
}
if (input.resources) {
fields.push(`Resources:`);
for (const resource of input.resources) {
fields.push(`- ${resource}`);
}
}
if (fields.length) {
message += `\n\n${fields.join('\n')}`;
}
return message;
}