-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpayloads.ts
214 lines (201 loc) · 5.48 KB
/
payloads.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
import { u8aToHex } from '@polkadot/util';
import {
SiwfResponsePayloadAddProvider,
SiwfResponsePayloadClaimHandle,
SiwfResponsePayloadItemActions,
SiwfResponsePayloadLogin,
} from '../types/payload.js';
import { ExampleUserKey } from './keys.js';
import {
serializeAddProviderPayloadHex,
serializeClaimHandlePayloadHex,
serializeItemActionsPayloadHex,
} from '../util.js';
function generateLoginMessage(account: string, issued: Date, expires: Date, uri: URL) {
return `${uri.hostname} wants you to sign in with your Frequency account:\n${account}\n\n\n\nURI: ${uri}\nNonce: N6rLwqyz34oUxJEXJ\nIssued At: ${issued.toISOString()}\nExpiration Time: ${expires.toISOString()}`;
}
// Setup now so that it is consistent for the entire test run
const now = Date.now();
const loginMessageUrl = (url: string) =>
generateLoginMessage(
ExampleUserKey.public,
new Date(now - 24 * 60 * 60 * 1000),
new Date(now + 24 * 60 * 60 * 1000),
new URL(url)
);
const loginMessageGood = () => loginMessageUrl('https://your-app.com/signin/callback');
const loginMessageExpired = () =>
generateLoginMessage(
ExampleUserKey.public,
new Date(now - 2 * 24 * 60 * 60 * 1000),
new Date(now - 24 * 60 * 60 * 1000),
new URL('https://your-app.com/signin/callback')
);
export const ExamplePayloadLoginUrl = (url: string): SiwfResponsePayloadLogin => ({
signature: {
algo: 'SR25519',
encoding: 'base16',
encodedValue: u8aToHex(ExampleUserKey.keyPair().sign(loginMessageUrl(url))),
},
type: 'login',
payload: {
message: loginMessageUrl(url),
},
});
export const ExamplePayloadLoginGood = (): SiwfResponsePayloadLogin => ({
signature: {
algo: 'SR25519',
encoding: 'base16',
encodedValue: u8aToHex(ExampleUserKey.keyPair().sign(loginMessageGood())),
},
type: 'login',
payload: {
message: loginMessageGood(),
},
});
export const ExamplePayloadLoginExpired = (): SiwfResponsePayloadLogin => ({
signature: {
algo: 'SR25519',
encoding: 'base16',
encodedValue: u8aToHex(ExampleUserKey.keyPair().sign(loginMessageExpired())),
},
type: 'login',
payload: {
message: loginMessageExpired(),
},
});
export const ExamplePayloadLoginStatic: SiwfResponsePayloadLogin = {
signature: {
algo: 'SR25519',
encoding: 'base16',
encodedValue:
'0xe261698297111834e68b4152bf1f89819e886b6528f6fff45715f7781d0f1e7dc4007ccfed1e85b8c603c0fea2f7abf22bfe6336869ad21f11a09a114452c680',
},
type: 'login',
payload: {
message:
'your-app.com wants you to sign in with your Frequency account:\n' +
'f6akufkq9Lex6rT8RCEDRuoZQRgo5pWiRzeo81nmKNGWGNJdJ\n' +
'\n' +
'\n' +
'\n' +
'URI: https://your-app.com/signin/callback\n' +
'Nonce: N6rLwqyz34oUxJEXJ\n' +
'Issued At: 2024-10-29T19:17:27.077Z\n' +
'Expiration Time: 2060-03-05T23:23:03.041Z',
},
};
// Signed by ExampleUserKey
export const ExamplePayloadCreateSponsoredAccount = (): SiwfResponsePayloadAddProvider => ({
signature: {
algo: 'SR25519',
encoding: 'base16',
encodedValue: u8aToHex(
ExampleUserKey.keyPair().sign(
serializeAddProviderPayloadHex({
authorizedMsaId: 1,
schemaIds: [5, 7, 8, 9, 10],
expiration: 24,
})
)
),
},
endpoint: {
pallet: 'msa',
extrinsic: 'createSponsoredAccountWithDelegation',
},
type: 'addProvider',
payload: {
authorizedMsaId: 1,
schemaIds: [5, 7, 8, 9, 10],
expiration: 24,
},
});
// Signed by ExampleUserKey
export const ExamplePayloadGrantDelegation = (): SiwfResponsePayloadAddProvider => ({
signature: {
algo: 'SR25519',
encoding: 'base16',
encodedValue: u8aToHex(
ExampleUserKey.keyPair().sign(
serializeAddProviderPayloadHex({
authorizedMsaId: 1,
schemaIds: [5, 7, 8, 9, 10],
expiration: 24,
})
)
),
},
endpoint: {
pallet: 'msa',
extrinsic: 'grantDelegation',
},
type: 'addProvider',
payload: {
authorizedMsaId: 1,
schemaIds: [5, 7, 8, 9, 10],
expiration: 24,
},
});
// Signed by ExampleUserKey
export const ExamplePayloadPublicGraphKey = (): SiwfResponsePayloadItemActions => ({
signature: {
algo: 'SR25519',
encoding: 'base16',
encodedValue: u8aToHex(
ExampleUserKey.keyPair().sign(
serializeItemActionsPayloadHex({
schemaId: 7,
targetHash: 0,
expiration: 20,
actions: [
{
type: 'addItem',
payloadHex: '0x40eea1e39d2f154584c4b1ca8f228bb49ae5a14786ed63c90025e755f16bd58d37',
},
],
})
)
),
},
endpoint: {
pallet: 'statefulStorage',
extrinsic: 'applyItemActionsWithSignatureV2',
},
type: 'itemActions',
payload: {
schemaId: 7,
targetHash: 0,
expiration: 20,
actions: [
{
type: 'addItem',
payloadHex: '0x40eea1e39d2f154584c4b1ca8f228bb49ae5a14786ed63c90025e755f16bd58d37',
},
],
},
});
// Signed by ExampleUserKey
export const ExamplePayloadClaimHandle = (): SiwfResponsePayloadClaimHandle => ({
signature: {
algo: 'SR25519',
encoding: 'base16',
encodedValue: u8aToHex(
ExampleUserKey.keyPair().sign(
serializeClaimHandlePayloadHex({
baseHandle: 'ExampleHandle',
expiration: 24,
})
)
),
},
endpoint: {
pallet: 'handles',
extrinsic: 'claimHandle',
},
type: 'claimHandle',
payload: {
baseHandle: 'ExampleHandle',
expiration: 24,
},
});