-
Notifications
You must be signed in to change notification settings - Fork 105
Expand file tree
/
Copy pathparse-url.ts
More file actions
218 lines (175 loc) · 8.05 KB
/
Copy pathparse-url.ts
File metadata and controls
218 lines (175 loc) · 8.05 KB
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
import { type Address } from "gill";
import assert from "node:assert";
import { parseSolanaPayURL, SolanaPayTransactionRequestURL, type SolanaPayTransferRequestURL } from "../parse-url.js";
describe("parseSolanaPayURL", () => {
const pubkey = "nick6zJc6HpW3kfBm4xS2dmbuVRyb5F3AnUvj5ymzR5" as Address;
describe("SolanaPayTransactionRequestURL", () => {
it("should parse with only a url", () => {
const url = `solana:${"https://gillsdk.com"}`;
const { label, message, link } = parseSolanaPayURL(url) as SolanaPayTransactionRequestURL;
// should add trailing slash
assert.equal(link, "https://gillsdk.com/");
assert.equal(label, undefined);
assert.equal(message, undefined);
});
it("should parse with Solana Pay query params", () => {
const data = {
link: "https://gillsdk.com/",
message: "Message",
label: "Label",
};
const url = `solana:${data.link}?label=${data.label}&message=${data.message}`;
const { label, message, link } = parseSolanaPayURL(url) as SolanaPayTransactionRequestURL;
assert.equal(link, data.link);
assert.equal(label, data.label);
assert.equal(message, data.message);
});
it("should parse with link with query params", () => {
const data = {
link: "https://gillsdk.com/?query=param",
};
const url = `solana:${encodeURIComponent(data.link)}`;
const { label, message, link } = parseSolanaPayURL(url) as SolanaPayTransactionRequestURL;
assert.equal(link, data.link);
assert.equal(label, undefined);
assert.equal(message, undefined);
});
it("should parse with link with query params and Solana Pay query params", () => {
const data = {
link: "https://gillsdk.com/?query=param",
message: "Message",
label: "Label",
};
const url = `solana:${encodeURIComponent(data.link)}?label=${data.label}&message=${data.message}`;
const { label, message, link } = parseSolanaPayURL(url) as SolanaPayTransactionRequestURL;
assert.equal(link, data.link);
assert.equal(label, data.label);
assert.equal(message, data.message);
});
});
describe("SolanaPayTransferRequestURL", () => {
it("should parse with only address", () => {
const url = "solana:nick6zJc6HpW3kfBm4xS2dmbuVRyb5F3AnUvj5ymzR5";
const { recipient, amount, splToken, reference, label, message, memo } = parseSolanaPayURL(
url,
) as SolanaPayTransferRequestURL;
assert.equal(recipient, pubkey);
assert.equal(amount, undefined);
assert.equal(splToken, undefined);
assert.equal(reference, undefined);
assert.equal(label, undefined);
assert.equal(message, undefined);
assert.equal(memo, undefined);
});
it("should parse successfully", () => {
const url =
"solana:nick6zJc6HpW3kfBm4xS2dmbuVRyb5F3AnUvj5ymzR5?amount=0.000000001&reference=82ZJ7nbGpixjeDCmEhUcmwXYfvurzAgGdtSMuHnUgyny&label=Michael&message=Thanks%20for%20all%20the%20fish&memo=OrderId5678";
const { recipient, amount, splToken, reference, label, message, memo } = parseSolanaPayURL(
url,
) as SolanaPayTransferRequestURL;
assert.equal(recipient, pubkey);
assert.equal(amount, 0.000000001);
assert.equal(splToken, undefined);
assert.equal(reference?.length, 1);
assert.equal(reference![0], "82ZJ7nbGpixjeDCmEhUcmwXYfvurzAgGdtSMuHnUgyny");
assert.equal(label, "Michael");
assert.equal(message, "Thanks for all the fish");
assert.equal(memo, "OrderId5678");
});
it("should parse with spl-token", () => {
const url =
"solana:nick6zJc6HpW3kfBm4xS2dmbuVRyb5F3AnUvj5ymzR5?amount=1.01&spl-token=82ZJ7nbGpixjeDCmEhUcmwXYfvurzAgGdtSMuHnUgyny&label=Michael&message=Thanks%20for%20all%20the%20fish&memo=OrderId5678";
const { recipient, amount, splToken, reference, label, message, memo } = parseSolanaPayURL(
url,
) as SolanaPayTransferRequestURL;
assert.equal(recipient, pubkey);
assert.equal(amount, 1.01);
assert.equal(splToken, "82ZJ7nbGpixjeDCmEhUcmwXYfvurzAgGdtSMuHnUgyny");
assert.equal(reference, undefined);
assert.equal(label, "Michael");
assert.equal(message, "Thanks for all the fish");
assert.equal(memo, "OrderId5678");
});
it("should parse multiple references", () => {
const url =
"solana:nick6zJc6HpW3kfBm4xS2dmbuVRyb5F3AnUvj5ymzR5?reference=82ZJ7nbGpixjeDCmEhUcmwXYfvurzAgGdtSMuHnUgyny&reference=mvines9iiHiQTysrwkJjGf2gb9Ex9jXJX8ns3qwf2kN";
const { recipient, amount, splToken, reference, label, message, memo } = parseSolanaPayURL(
url,
) as SolanaPayTransferRequestURL;
assert.equal(recipient, pubkey);
assert.equal(reference?.length, 2);
assert.equal(reference![0], "82ZJ7nbGpixjeDCmEhUcmwXYfvurzAgGdtSMuHnUgyny");
assert.equal(reference![1], "mvines9iiHiQTysrwkJjGf2gb9Ex9jXJX8ns3qwf2kN");
assert.equal(amount, undefined);
assert.equal(splToken, undefined);
assert.equal(label, undefined);
assert.equal(message, undefined);
assert.equal(memo, undefined);
});
it("should parse without an amount", () => {
const url =
"solana:nick6zJc6HpW3kfBm4xS2dmbuVRyb5F3AnUvj5ymzR5?reference=82ZJ7nbGpixjeDCmEhUcmwXYfvurzAgGdtSMuHnUgyny&label=Michael&message=Thanks%20for%20all%20the%20fish&memo=OrderId5678";
const { recipient, amount, splToken, reference, label, message, memo } = parseSolanaPayURL(
url,
) as SolanaPayTransferRequestURL;
assert.equal(recipient, pubkey);
expect(amount).toBeUndefined();
assert.equal(splToken, undefined);
assert.equal(reference?.length, 1);
assert.equal(reference![0], "82ZJ7nbGpixjeDCmEhUcmwXYfvurzAgGdtSMuHnUgyny");
assert.equal(label, "Michael");
assert.equal(message, "Thanks for all the fish");
assert.equal(memo, "OrderId5678");
});
it("should parse with only amount", () => {
const url = "solana:nick6zJc6HpW3kfBm4xS2dmbuVRyb5F3AnUvj5ymzR5?amount=0.000000001";
const { recipient, amount, splToken, reference, label, message, memo } = parseSolanaPayURL(
url,
) as SolanaPayTransferRequestURL;
assert.equal(recipient, pubkey);
assert.equal(amount, 0.000000001);
assert.equal(splToken, undefined);
assert.equal(reference, undefined);
assert.equal(label, undefined);
assert.equal(message, undefined);
assert.equal(memo, undefined);
});
it("should parse a zero amount", () => {
const url = "solana:nick6zJc6HpW3kfBm4xS2dmbuVRyb5F3AnUvj5ymzR5?amount=0";
const { recipient, amount } = parseSolanaPayURL(url) as SolanaPayTransferRequestURL;
assert.equal(recipient, pubkey);
assert.equal(amount, 0);
});
});
describe("errors", () => {
it("throws an error on invalid length", () => {
const url = "X".repeat(2049);
expect(() => parseSolanaPayURL(url)).toThrow("length invalid");
});
it("throws an error on invalid protocol", () => {
const url = "eth:0xffff";
expect(() => parseSolanaPayURL(url)).toThrow("protocol invalid");
});
it("throws an error on invalid recipient", () => {
const url = "solana:0xffff";
expect(() => parseSolanaPayURL(url)).toThrow("recipient invalid");
});
it.each([
// various invalid numbers
["1milliondollars"],
[-0.1],
[-100],
])("throws an error on invalid amount: %p", (amount) => {
const url = `solana:nick6zJc6HpW3kfBm4xS2dmbuVRyb5F3AnUvj5ymzR5?amount=${amount}`;
expect(() => parseSolanaPayURL(url)).toThrow("amount invalid");
});
it("throws an error on invalid token", () => {
const url = "solana:nick6zJc6HpW3kfBm4xS2dmbuVRyb5F3AnUvj5ymzR5?amount=1&spl-token=0xffff";
expect(() => parseSolanaPayURL(url)).toThrow("spl-token invalid");
});
it("throws an error on invalid reference", () => {
const url = "solana:nick6zJc6HpW3kfBm4xS2dmbuVRyb5F3AnUvj5ymzR5?amount=1&reference=0xffff";
expect(() => parseSolanaPayURL(url)).toThrow("reference invalid");
});
});
});