-
Notifications
You must be signed in to change notification settings - Fork 34
/
sep0007_test.dart
289 lines (248 loc) · 11.1 KB
/
sep0007_test.dart
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
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
@Timeout(const Duration(seconds: 400))
import 'dart:convert';
import 'package:flutter_test/flutter_test.dart';
import 'package:http/testing.dart';
import 'package:stellar_flutter_sdk/stellar_flutter_sdk.dart';
import 'package:http/http.dart' as http;
void main() {
StellarSDK sdk = StellarSDK.TESTNET;
final String accountId =
"GDGUF4SCNINRDCRUIVOMDYGIMXOWVP3ZLMTL2OGQIWMFDDSECZSFQMQV";
final String secretSeed =
"SBA2XQ5SRUW5H3FUQARMC6QYEPUYNSVCMM4PGESGVB2UIFHLM73TPXXF";
final String originDomainParam = "&origin_domain=place.domain.com";
final String callbackParam = "&callback=url:https://examplepost.com";
final KeyPair signerKeyPair = KeyPair.fromSecretSeed(secretSeed);
final URIScheme uriScheme = URIScheme();
String requestToml() {
return '''# Sample stellar.toml
FEDERATION_SERVER="https://api.domain.com/federation"
AUTH_SERVER="https://api.domain.com/auth"
TRANSFER_SERVER="https://api.domain.com"
URI_REQUEST_SIGNING_KEY="GDGUF4SCNINRDCRUIVOMDYGIMXOWVP3ZLMTL2OGQIWMFDDSECZSFQMQV"''';
}
String requestTomlSignatureMissing() {
return '''# Sample stellar.toml
FEDERATION_SERVER="https://api.domain.com/federation"
AUTH_SERVER="https://api.domain.com/auth"
TRANSFER_SERVER="https://api.domain.com"''';
}
String requestTomlSignatureMissmatch() {
return '''# Sample stellar.toml
FEDERATION_SERVER="https://api.domain.com/federation"
AUTH_SERVER="https://api.domain.com/auth"
TRANSFER_SERVER="https://api.domain.com"
URI_REQUEST_SIGNING_KEY="GCCHBLJOZUFBVAUZP55N7ZU6ZB5VGEDHSXT23QC6UIVDQNGI6QDQTOOR"''';
}
setUp(() async {
await sdk.accounts.account(accountId).then((response) {
assert(true);
}).catchError((error) async {
assert(error is ErrorResponse && error.code == 404);
await FriendBot.fundTestAccount(accountId);
});
});
test('test generate sign transaction url', () async {
AccountResponse sourceAccount = await sdk.accounts.account(accountId);
SetOptionsOperationBuilder setOp = SetOptionsOperationBuilder();
setOp.setSourceAccount(accountId);
setOp.setHomeDomain("www.soneso.com");
Transaction transaction =
TransactionBuilder(sourceAccount).addOperation(setOp.build()).build();
String url =
uriScheme.generateSignTransactionURI(transaction.toEnvelopeXdrBase64());
assert(url.startsWith(
"web+stellar:tx?xdr=AAAAAgAAAADNQvJCahsRijRFXMHgyGXdar95Wya9O"));
});
test('test generate pay operation url', () async {
String url = uriScheme.generatePayOperationURI(accountId,
amount: "123.21",
assetCode: "ANA",
assetIssuer:
"GC4HC3AXQDNAMURMHVGMLFGLQELEQBCE4GI7IOKEAWAKBXY7SXXWBTLV");
assert(
"web+stellar:pay?destination=GDGUF4SCNINRDCRUIVOMDYGIMXOWVP3ZLMTL2OGQIWMFDDSECZSFQMQV&amount=123.21&asset_code=ANA&asset_issuer=GC4HC3AXQDNAMURMHVGMLFGLQELEQBCE4GI7IOKEAWAKBXY7SXXWBTLV" ==
url);
});
test('check missing signature from URI scheme', () async {
AccountResponse sourceAccount = await sdk.accounts.account(accountId);
SetOptionsOperationBuilder setOp = SetOptionsOperationBuilder();
setOp.setSourceAccount(accountId);
setOp.setHomeDomain("www.soneso.com");
Transaction transaction =
TransactionBuilder(sourceAccount).addOperation(setOp.build()).build();
String url = uriScheme
.generateSignTransactionURI(transaction.toEnvelopeXdrBase64()) +
originDomainParam;
await uriScheme.checkUIRSchemeIsValid(url).then((response) {
assert(false);
}).catchError((error) async {
assert(error is URISchemeError &&
error.type == URISchemeError.missingSignature);
});
});
test('check missing domain from URI scheme', () async {
AccountResponse sourceAccount = await sdk.accounts.account(accountId);
SetOptionsOperationBuilder setOp = SetOptionsOperationBuilder();
setOp.setSourceAccount(accountId);
setOp.setHomeDomain("www.soneso.com");
Transaction transaction =
TransactionBuilder(sourceAccount).addOperation(setOp.build()).build();
String url =
uriScheme.generateSignTransactionURI(transaction.toEnvelopeXdrBase64());
await uriScheme.checkUIRSchemeIsValid(url).then((response) {
assert(false);
}).catchError((error) async {
assert(error is URISchemeError &&
error.type == URISchemeError.missingOriginDomain);
});
});
test('generate signed Tx Test Url', () async {
AccountResponse sourceAccount = await sdk.accounts.account(accountId);
SetOptionsOperationBuilder setOp = SetOptionsOperationBuilder();
setOp.setSourceAccount(accountId);
setOp.setHomeDomain("www.soneso.com");
Transaction transaction =
TransactionBuilder(sourceAccount).addOperation(setOp.build()).build();
String url = uriScheme
.generateSignTransactionURI(transaction.toEnvelopeXdrBase64()) +
originDomainParam;
url = uriScheme.signURI(url, signerKeyPair);
assert(uriScheme.getParameterValue(URIScheme.signatureParameterName, url) !=
null);
});
test('validate Test Url', () async {
AccountResponse sourceAccount = await sdk.accounts.account(accountId);
SetOptionsOperationBuilder setOp = SetOptionsOperationBuilder();
setOp.setSourceAccount(accountId);
setOp.setHomeDomain("www.soneso.com");
Transaction transaction =
TransactionBuilder(sourceAccount).addOperation(setOp.build()).build();
String url = uriScheme
.generateSignTransactionURI(transaction.toEnvelopeXdrBase64()) +
originDomainParam;
url = uriScheme.signURI(url, signerKeyPair);
assert(uriScheme.getParameterValue(URIScheme.signatureParameterName, url) !=
null);
uriScheme.httpClient = MockClient((request) async {
if (request.url.toString().startsWith(
"https://place.domain.com/.well-known/stellar.toml") &&
request.method == "GET") {
return http.Response(requestToml(), 200);
}
final mapJson = {'error': "Bad request"};
return http.Response(json.encode(mapJson), 400);
});
bool isValid = await uriScheme.checkUIRSchemeIsValid(url);
assert(isValid);
uriScheme.httpClient = http.Client();
});
test('sign and submit transaction', () async {
AccountResponse sourceAccount = await sdk.accounts.account(accountId);
SetOptionsOperationBuilder setOp = SetOptionsOperationBuilder();
setOp.setSourceAccount(accountId);
setOp.setHomeDomain("www.soneso.com");
Transaction transaction =
TransactionBuilder(sourceAccount).addOperation(setOp.build()).build();
String url = uriScheme
.generateSignTransactionURI(transaction.toEnvelopeXdrBase64()) +
originDomainParam;
url = uriScheme.signURI(url, signerKeyPair);
assert(uriScheme.getParameterValue(URIScheme.signatureParameterName, url) !=
null);
SubmitUriSchemeTransactionResponse response = await uriScheme
.signAndSubmitTransaction(url, signerKeyPair, network: Network.TESTNET);
assert(response.submitTransactionResponse != null);
assert(response.submitTransactionResponse!.success);
});
test('sign and submit transaction to callback', () async {
AccountResponse sourceAccount = await sdk.accounts.account(accountId);
SetOptionsOperationBuilder setOp = SetOptionsOperationBuilder();
setOp.setSourceAccount(accountId);
setOp.setHomeDomain("www.soneso.com");
Transaction transaction =
TransactionBuilder(sourceAccount).addOperation(setOp.build()).build();
String url = uriScheme
.generateSignTransactionURI(transaction.toEnvelopeXdrBase64()) +
originDomainParam +
callbackParam;
url = uriScheme.signURI(url, signerKeyPair);
assert(uriScheme.getParameterValue(URIScheme.signatureParameterName, url) !=
null);
uriScheme.httpClient = MockClient((request) async {
if (request.url.toString().startsWith("https://examplepost.com") &&
request.method == "POST" &&
request.body.startsWith(URIScheme.xdrParameterName)) {
return http.Response("", 200);
}
final mapJson = {'error': "Bad request"};
return http.Response(json.encode(mapJson), 400);
});
SubmitUriSchemeTransactionResponse response = await uriScheme
.signAndSubmitTransaction(url, signerKeyPair, network: Network.TESTNET);
assert(response.submitTransactionResponse == null);
assert(response.response != null);
assert(response.response!.statusCode == 200);
uriScheme.httpClient = http.Client();
});
test('check Toml Signature Missing', () async {
AccountResponse sourceAccount = await sdk.accounts.account(accountId);
SetOptionsOperationBuilder setOp = SetOptionsOperationBuilder();
setOp.setSourceAccount(accountId);
setOp.setHomeDomain("www.soneso.com");
Transaction transaction =
TransactionBuilder(sourceAccount).addOperation(setOp.build()).build();
String url = uriScheme
.generateSignTransactionURI(transaction.toEnvelopeXdrBase64()) +
originDomainParam;
url = uriScheme.signURI(url, signerKeyPair);
assert(uriScheme.getParameterValue(URIScheme.signatureParameterName, url) !=
null);
uriScheme.httpClient = MockClient((request) async {
if (request.url.toString().startsWith(
"https://place.domain.com/.well-known/stellar.toml") &&
request.method == "GET") {
return http.Response(requestTomlSignatureMissing(), 200);
}
final mapJson = {'error': "Bad request"};
return http.Response(json.encode(mapJson), 400);
});
await uriScheme.checkUIRSchemeIsValid(url).then((response) {
assert(false);
}).catchError((error) async {
assert(error is URISchemeError &&
error.type == URISchemeError.tomlSignatureMissing);
});
uriScheme.httpClient = http.Client();
});
test('check Toml Signature Missmatch', () async {
AccountResponse sourceAccount = await sdk.accounts.account(accountId);
SetOptionsOperationBuilder setOp = SetOptionsOperationBuilder();
setOp.setSourceAccount(accountId);
setOp.setHomeDomain("www.soneso.com");
Transaction transaction =
TransactionBuilder(sourceAccount).addOperation(setOp.build()).build();
String url = uriScheme
.generateSignTransactionURI(transaction.toEnvelopeXdrBase64()) +
originDomainParam;
url = uriScheme.signURI(url, signerKeyPair);
assert(uriScheme.getParameterValue(URIScheme.signatureParameterName, url) !=
null);
uriScheme.httpClient = MockClient((request) async {
if (request.url.toString().startsWith(
"https://place.domain.com/.well-known/stellar.toml") &&
request.method == "GET") {
return http.Response(requestTomlSignatureMissmatch(), 200);
}
final mapJson = {'error': "Bad request"};
return http.Response(json.encode(mapJson), 400);
});
await uriScheme.checkUIRSchemeIsValid(url).then((response) {
assert(false);
}).catchError((error) async {
assert(error is URISchemeError &&
error.type == URISchemeError.invalidSignature);
});
uriScheme.httpClient = http.Client();
});
}