stack_wallet/test/electrumx_test.dart

1712 lines
53 KiB
Dart
Raw Normal View History

2022-08-26 08:11:35 +00:00
import 'package:flutter_test/flutter_test.dart';
import 'package:mockito/annotations.dart';
import 'package:mockito/mockito.dart';
import 'package:stackwallet/electrumx_rpc/electrumx.dart';
import 'package:stackwallet/electrumx_rpc/rpc.dart';
2023-09-08 16:04:56 +00:00
import 'package:stackwallet/services/tor_service.dart';
2022-08-26 08:11:35 +00:00
import 'package:stackwallet/utilities/prefs.dart';
import 'electrumx_test.mocks.dart';
import 'sample_data/get_anonymity_set_sample_data.dart';
import 'sample_data/get_used_serials_sample_data.dart';
import 'sample_data/transaction_data_samples.dart';
2023-09-08 16:04:56 +00:00
@GenerateMocks([JsonRPC, Prefs, TorService])
2022-08-26 08:11:35 +00:00
void main() {
group("factory constructors and getters", () {
test("electrumxnode .from factory", () {
final nodeA = ElectrumXNode(
address: "some address",
port: 1,
name: "some name",
id: "some ID",
useSSL: true,
);
final nodeB = ElectrumXNode.from(nodeA);
expect(nodeB.toString(), nodeA.toString());
expect(nodeA == nodeB, false);
});
test("electrumx .from factory", () {
final node = ElectrumXNode(
address: "some address",
port: 1,
name: "some name",
id: "some ID",
useSSL: true,
);
final mockPrefs = MockPrefs();
2023-09-08 16:04:56 +00:00
when(mockPrefs.useTor).thenAnswer((realInvocation) => false);
final torService = MockTorService();
2022-08-26 08:11:35 +00:00
2023-09-08 16:04:56 +00:00
final client = ElectrumX.from(
node: node,
failovers: [],
prefs: mockPrefs,
torService: torService,
);
2022-08-26 08:11:35 +00:00
expect(client.useSSL, node.useSSL);
expect(client.host, node.address);
expect(client.port, node.port);
expect(client.rpcClient, null);
verifyNoMoreInteractions(mockPrefs);
});
});
test("Server error", () {
final mockClient = MockJsonRPC();
const command = "blockchain.transaction.get";
const jsonArgs = '["",true]';
when(
mockClient.request(
2023-05-29 16:30:41 +00:00
'{"jsonrpc": "2.0", "id": "some requestId",'
'"method": "$command","params": $jsonArgs}',
2023-07-27 21:18:02 +00:00
const Duration(seconds: 60),
2023-05-29 16:30:41 +00:00
),
2022-08-26 08:11:35 +00:00
).thenAnswer(
2023-05-29 16:30:41 +00:00
(_) async => JsonRPCResponse(data: {
2022-08-26 08:11:35 +00:00
"jsonrpc": "2.0",
"error": {
"code": 1,
"message": "None should be a transaction hash",
},
"id": "some requestId",
2023-05-29 16:30:41 +00:00
}),
2022-08-26 08:11:35 +00:00
);
final mockPrefs = MockPrefs();
2023-09-08 16:04:56 +00:00
when(mockPrefs.useTor).thenAnswer((realInvocation) => false);
final torService = MockTorService();
2022-08-26 08:11:35 +00:00
when(mockPrefs.wifiOnly).thenAnswer((_) => false);
final client = ElectrumX(
2023-09-08 16:04:56 +00:00
host: "some server",
port: 0,
useSSL: true,
client: mockClient,
failovers: [],
prefs: mockPrefs,
torService: torService,
);
2022-08-26 08:11:35 +00:00
expect(() => client.getTransaction(requestID: "some requestId", txHash: ''),
throwsA(isA<Exception>()));
2023-09-08 16:04:56 +00:00
2022-08-26 08:11:35 +00:00
verify(mockPrefs.wifiOnly).called(1);
verifyNoMoreInteractions(mockPrefs);
});
group("getBlockHeadTip", () {
test("getBlockHeadTip success", () async {
final mockClient = MockJsonRPC();
const command = "blockchain.headers.subscribe";
const jsonArgs = '[]';
when(
mockClient.request(
2023-05-29 16:30:41 +00:00
'{"jsonrpc": "2.0", "id": "some requestId",'
'"method": "$command","params": $jsonArgs}',
2023-07-27 21:18:02 +00:00
const Duration(seconds: 60),
2023-05-29 16:30:41 +00:00
),
2022-08-26 08:11:35 +00:00
).thenAnswer(
2023-05-29 16:30:41 +00:00
(_) async => JsonRPCResponse(data: {
2022-08-26 08:11:35 +00:00
"jsonrpc": "2.0",
"result": {"height": 520481, "hex": "some block hex string"},
"id": "some requestId"
2023-05-29 16:30:41 +00:00
}),
2022-08-26 08:11:35 +00:00
);
final mockPrefs = MockPrefs();
2023-09-08 16:04:56 +00:00
when(mockPrefs.useTor).thenAnswer((realInvocation) => false);
final torService = MockTorService();
2022-08-26 08:11:35 +00:00
when(mockPrefs.wifiOnly).thenAnswer((_) => false);
final client = ElectrumX(
host: "some server",
port: 0,
useSSL: true,
client: mockClient,
prefs: mockPrefs,
2023-09-08 16:04:56 +00:00
torService: torService,
2022-08-26 08:11:35 +00:00
failovers: []);
final result =
await (client.getBlockHeadTip(requestID: "some requestId"));
expect(result["height"], 520481);
2023-09-08 16:04:56 +00:00
2022-08-26 08:11:35 +00:00
verify(mockPrefs.wifiOnly).called(1);
2023-09-08 16:04:56 +00:00
verify(mockPrefs.useTor).called(1);
2022-08-26 08:11:35 +00:00
verifyNoMoreInteractions(mockPrefs);
});
test("getBlockHeadTip throws/fails", () {
final mockClient = MockJsonRPC();
const command = "blockchain.headers.subscribe";
const jsonArgs = '[]';
when(
mockClient.request(
2023-05-29 16:30:41 +00:00
'{"jsonrpc": "2.0", "id": "some requestId",'
'"method": "$command","params": $jsonArgs}',
2023-07-27 21:18:02 +00:00
const Duration(seconds: 60),
2023-05-29 16:30:41 +00:00
),
2022-08-26 08:11:35 +00:00
).thenThrow(Exception());
final mockPrefs = MockPrefs();
2023-09-08 16:04:56 +00:00
when(mockPrefs.useTor).thenAnswer((realInvocation) => false);
final torService = MockTorService();
2022-08-26 08:11:35 +00:00
when(mockPrefs.wifiOnly).thenAnswer((_) => false);
final client = ElectrumX(
host: "some server",
port: 0,
useSSL: true,
client: mockClient,
prefs: mockPrefs,
2023-09-08 16:04:56 +00:00
torService: torService,
2022-08-26 08:11:35 +00:00
failovers: []);
expect(() => client.getBlockHeadTip(requestID: "some requestId"),
throwsA(isA<Exception>()));
2023-09-08 16:04:56 +00:00
2022-08-26 08:11:35 +00:00
verify(mockPrefs.wifiOnly).called(1);
verifyNoMoreInteractions(mockPrefs);
});
});
group("ping", () {
test("ping success", () async {
final mockClient = MockJsonRPC();
const command = "server.ping";
const jsonArgs = '[]';
when(
mockClient.request(
2023-05-29 16:30:41 +00:00
'{"jsonrpc": "2.0", "id": "some requestId",'
'"method": "$command","params": $jsonArgs}',
2023-07-27 21:18:02 +00:00
const Duration(seconds: 2),
2023-05-29 16:30:41 +00:00
),
2022-08-26 08:11:35 +00:00
).thenAnswer(
2023-05-29 16:30:41 +00:00
(_) async => JsonRPCResponse(data: {
"jsonrpc": "2.0",
"result": null,
"id": "some requestId",
}),
2022-08-26 08:11:35 +00:00
);
final mockPrefs = MockPrefs();
2023-09-08 16:04:56 +00:00
when(mockPrefs.useTor).thenAnswer((realInvocation) => false);
final torService = MockTorService();
2022-08-26 08:11:35 +00:00
when(mockPrefs.wifiOnly).thenAnswer((_) => false);
final client = ElectrumX(
host: "some server",
port: 0,
useSSL: true,
client: mockClient,
prefs: mockPrefs,
2023-09-08 16:04:56 +00:00
torService: torService,
2022-08-26 08:11:35 +00:00
failovers: []);
final result = await client.ping(requestID: "some requestId");
expect(result, true);
2023-09-08 16:04:56 +00:00
2022-08-26 08:11:35 +00:00
verify(mockPrefs.wifiOnly).called(1);
2023-09-08 16:04:56 +00:00
verify(mockPrefs.useTor).called(1);
2022-08-26 08:11:35 +00:00
verifyNoMoreInteractions(mockPrefs);
});
test("ping throws/fails", () {
final mockClient = MockJsonRPC();
const command = "server.ping";
const jsonArgs = '[]';
when(
mockClient.request(
2023-05-29 16:30:41 +00:00
'{"jsonrpc": "2.0", "id": "some requestId",'
'"method": "$command","params": $jsonArgs}',
2023-07-27 21:18:02 +00:00
const Duration(seconds: 2),
2023-05-29 16:30:41 +00:00
),
2022-08-26 08:11:35 +00:00
).thenThrow(Exception());
final mockPrefs = MockPrefs();
2023-09-08 16:04:56 +00:00
when(mockPrefs.useTor).thenAnswer((realInvocation) => false);
final torService = MockTorService();
2022-08-26 08:11:35 +00:00
when(mockPrefs.wifiOnly).thenAnswer((_) => false);
final client = ElectrumX(
host: "some server",
port: 0,
useSSL: true,
client: mockClient,
prefs: mockPrefs,
2023-09-08 16:04:56 +00:00
torService: torService,
2022-08-26 08:11:35 +00:00
failovers: []);
expect(() => client.ping(requestID: "some requestId"),
throwsA(isA<Exception>()));
2023-09-08 16:04:56 +00:00
2022-08-26 08:11:35 +00:00
verify(mockPrefs.wifiOnly).called(1);
verifyNoMoreInteractions(mockPrefs);
});
});
group("getServerFeatures", () {
test("getServerFeatures success", () async {
final mockClient = MockJsonRPC();
const command = "server.features";
const jsonArgs = '[]';
when(
mockClient.request(
2023-05-29 16:30:41 +00:00
'{"jsonrpc": "2.0", "id": "some requestId",'
'"method": "$command","params": $jsonArgs}',
2023-07-27 21:18:02 +00:00
const Duration(seconds: 60),
2023-05-29 16:30:41 +00:00
),
2022-08-26 08:11:35 +00:00
).thenAnswer(
2023-05-29 16:30:41 +00:00
(_) async => JsonRPCResponse(data: {
2022-08-26 08:11:35 +00:00
"jsonrpc": "2.0",
"result": {
"genesis_hash":
"000000000933ea01ad0ee984209779baaec3ced90fa3f408719526f8d77f4943",
"hosts": {
"0.0.0.0": {"tcp_port": 51001, "ssl_port": 51002}
},
"protocol_max": "1.0",
"protocol_min": "1.0",
"pruning": null,
"server_version": "ElectrumX 1.0.17",
"hash_function": "sha256"
},
"id": "some requestId"
2023-05-29 16:30:41 +00:00
}),
2022-08-26 08:11:35 +00:00
);
final mockPrefs = MockPrefs();
2023-09-08 16:04:56 +00:00
when(mockPrefs.useTor).thenAnswer((realInvocation) => false);
final torService = MockTorService();
2022-08-26 08:11:35 +00:00
when(mockPrefs.wifiOnly).thenAnswer((_) => false);
final client = ElectrumX(
host: "some server",
port: 0,
useSSL: true,
client: mockClient,
prefs: mockPrefs,
2023-09-08 16:04:56 +00:00
torService: torService,
2022-08-26 08:11:35 +00:00
failovers: []);
final result =
await client.getServerFeatures(requestID: "some requestId");
expect(result, {
"genesis_hash":
"000000000933ea01ad0ee984209779baaec3ced90fa3f408719526f8d77f4943",
"hosts": {
"0.0.0.0": {"tcp_port": 51001, "ssl_port": 51002}
},
"protocol_max": "1.0",
"protocol_min": "1.0",
"pruning": null,
"server_version": "ElectrumX 1.0.17",
"hash_function": "sha256",
});
2023-09-08 16:04:56 +00:00
2022-08-26 08:11:35 +00:00
verify(mockPrefs.wifiOnly).called(1);
2023-09-08 16:04:56 +00:00
verify(mockPrefs.useTor).called(1);
2022-08-26 08:11:35 +00:00
verifyNoMoreInteractions(mockPrefs);
});
test("getServerFeatures throws/fails", () {
final mockClient = MockJsonRPC();
const command = "server.features";
const jsonArgs = '[]';
when(
mockClient.request(
2023-05-29 16:30:41 +00:00
'{"jsonrpc": "2.0", "id": "some requestId",'
'"method": "$command","params": $jsonArgs}',
2023-07-27 21:18:02 +00:00
const Duration(seconds: 60),
2023-05-29 16:30:41 +00:00
),
2022-08-26 08:11:35 +00:00
).thenThrow(Exception());
final mockPrefs = MockPrefs();
2023-09-08 16:04:56 +00:00
when(mockPrefs.useTor).thenAnswer((realInvocation) => false);
final torService = MockTorService();
2022-08-26 08:11:35 +00:00
when(mockPrefs.wifiOnly).thenAnswer((_) => false);
final client = ElectrumX(
host: "some server",
port: 0,
useSSL: true,
client: mockClient,
prefs: mockPrefs,
2023-09-08 16:04:56 +00:00
torService: torService,
2022-08-26 08:11:35 +00:00
failovers: []);
expect(() => client.getServerFeatures(requestID: "some requestId"),
throwsA(isA<Exception>()));
2023-09-08 16:04:56 +00:00
2022-08-26 08:11:35 +00:00
verify(mockPrefs.wifiOnly).called(1);
verifyNoMoreInteractions(mockPrefs);
});
});
group("broadcastTransaction", () {
test("broadcastTransaction success", () async {
final mockClient = MockJsonRPC();
const command = "blockchain.transaction.broadcast";
const jsonArgs = '["some raw transaction string"]';
when(
mockClient.request(
2023-05-29 16:30:41 +00:00
'{"jsonrpc": "2.0", "id": "some requestId",'
'"method": "$command","params": $jsonArgs}',
2023-07-27 21:18:02 +00:00
const Duration(seconds: 60),
2023-05-29 16:30:41 +00:00
),
2022-08-26 08:11:35 +00:00
).thenAnswer(
2023-05-29 16:30:41 +00:00
(_) async => JsonRPCResponse(data: {
2022-08-26 08:11:35 +00:00
"jsonrpc": "2.0",
"result": "the txid of the rawtx",
"id": "some requestId"
2023-05-29 16:30:41 +00:00
}),
2022-08-26 08:11:35 +00:00
);
final mockPrefs = MockPrefs();
2023-09-08 16:04:56 +00:00
when(mockPrefs.useTor).thenAnswer((realInvocation) => false);
final torService = MockTorService();
2022-08-26 08:11:35 +00:00
when(mockPrefs.wifiOnly).thenAnswer((_) => false);
final client = ElectrumX(
host: "some server",
port: 0,
useSSL: true,
client: mockClient,
prefs: mockPrefs,
2023-09-08 16:04:56 +00:00
torService: torService,
2022-08-26 08:11:35 +00:00
failovers: []);
final result = await client.broadcastTransaction(
rawTx: "some raw transaction string", requestID: "some requestId");
expect(result, "the txid of the rawtx");
2023-09-08 16:04:56 +00:00
2022-08-26 08:11:35 +00:00
verify(mockPrefs.wifiOnly).called(1);
2023-09-08 16:04:56 +00:00
verify(mockPrefs.useTor).called(1);
2022-08-26 08:11:35 +00:00
verifyNoMoreInteractions(mockPrefs);
});
test("broadcastTransaction throws/fails", () {
final mockClient = MockJsonRPC();
const command = "blockchain.transaction.broadcast";
const jsonArgs = '["some raw transaction string"]';
when(
mockClient.request(
2023-05-29 16:30:41 +00:00
'{"jsonrpc": "2.0", "id": "some requestId",'
'"method": "$command","params": $jsonArgs}',
2023-07-27 21:18:02 +00:00
const Duration(seconds: 60),
2023-05-29 16:30:41 +00:00
),
2022-08-26 08:11:35 +00:00
).thenThrow(Exception());
final mockPrefs = MockPrefs();
2023-09-08 16:04:56 +00:00
when(mockPrefs.useTor).thenAnswer((realInvocation) => false);
final torService = MockTorService();
2022-08-26 08:11:35 +00:00
when(mockPrefs.wifiOnly).thenAnswer((_) => false);
final client = ElectrumX(
host: "some server",
port: 0,
useSSL: true,
client: mockClient,
prefs: mockPrefs,
2023-09-08 16:04:56 +00:00
torService: torService,
2022-08-26 08:11:35 +00:00
failovers: []);
expect(
() => client.broadcastTransaction(
rawTx: "some raw transaction string",
requestID: "some requestId"),
throwsA(isA<Exception>()));
2023-09-08 16:04:56 +00:00
2022-08-26 08:11:35 +00:00
verify(mockPrefs.wifiOnly).called(1);
verifyNoMoreInteractions(mockPrefs);
});
});
group("getBalance", () {
test("getBalance success", () async {
final mockClient = MockJsonRPC();
const command = "blockchain.scripthash.get_balance";
const jsonArgs = '["dummy hash"]';
when(
mockClient.request(
2023-05-29 16:30:41 +00:00
'{"jsonrpc": "2.0", "id": "some requestId",'
'"method": "$command","params": $jsonArgs}',
2023-07-27 21:18:02 +00:00
const Duration(seconds: 60),
2023-05-29 16:30:41 +00:00
),
2022-08-26 08:11:35 +00:00
).thenAnswer(
2023-05-29 16:30:41 +00:00
(_) async => JsonRPCResponse(data: {
2022-08-26 08:11:35 +00:00
"jsonrpc": "2.0",
"result": {
"confirmed": 103873966,
"unconfirmed": 23684400,
},
"id": "some requestId"
2023-05-29 16:30:41 +00:00
}),
2022-08-26 08:11:35 +00:00
);
final mockPrefs = MockPrefs();
2023-09-08 16:04:56 +00:00
when(mockPrefs.useTor).thenAnswer((realInvocation) => false);
final torService = MockTorService();
2022-08-26 08:11:35 +00:00
when(mockPrefs.wifiOnly).thenAnswer((_) => false);
final client = ElectrumX(
host: "some server",
port: 0,
useSSL: true,
client: mockClient,
prefs: mockPrefs,
2023-09-08 16:04:56 +00:00
torService: torService,
2022-08-26 08:11:35 +00:00
failovers: []);
final result = await client.getBalance(
scripthash: "dummy hash", requestID: "some requestId");
expect(result, {"confirmed": 103873966, "unconfirmed": 23684400});
2023-09-08 16:04:56 +00:00
2022-08-26 08:11:35 +00:00
verify(mockPrefs.wifiOnly).called(1);
2023-09-08 16:04:56 +00:00
verify(mockPrefs.useTor).called(1);
2022-08-26 08:11:35 +00:00
verifyNoMoreInteractions(mockPrefs);
});
test("getBalance throws/fails", () {
final mockClient = MockJsonRPC();
const command = "blockchain.scripthash.get_balance";
const jsonArgs = '["dummy hash"]';
when(
mockClient.request(
2023-05-29 16:30:41 +00:00
'{"jsonrpc": "2.0", "id": "some requestId",'
'"method": "$command","params": $jsonArgs}',
2023-07-27 21:18:02 +00:00
const Duration(seconds: 60),
2023-05-29 16:30:41 +00:00
),
2022-08-26 08:11:35 +00:00
).thenThrow(Exception());
final mockPrefs = MockPrefs();
2023-09-08 16:04:56 +00:00
when(mockPrefs.useTor).thenAnswer((realInvocation) => false);
final torService = MockTorService();
2022-08-26 08:11:35 +00:00
when(mockPrefs.wifiOnly).thenAnswer((_) => false);
final client = ElectrumX(
host: "some server",
port: 0,
useSSL: true,
client: mockClient,
prefs: mockPrefs,
2023-09-08 16:04:56 +00:00
torService: torService,
2022-08-26 08:11:35 +00:00
failovers: []);
expect(
() => client.getBalance(
scripthash: "dummy hash", requestID: "some requestId"),
throwsA(isA<Exception>()));
2023-09-08 16:04:56 +00:00
2022-08-26 08:11:35 +00:00
verify(mockPrefs.wifiOnly).called(1);
verifyNoMoreInteractions(mockPrefs);
});
});
group("getHistory", () {
test("getHistory success", () async {
final mockClient = MockJsonRPC();
const command = "blockchain.scripthash.get_history";
const jsonArgs = '["dummy hash"]';
when(
mockClient.request(
2023-05-29 16:30:41 +00:00
'{"jsonrpc": "2.0", "id": "some requestId",'
'"method": "$command","params": $jsonArgs}',
2023-07-27 21:18:02 +00:00
const Duration(minutes: 5),
2023-05-29 16:30:41 +00:00
),
2022-08-26 08:11:35 +00:00
).thenAnswer(
2023-05-29 16:30:41 +00:00
(_) async => JsonRPCResponse(data: {
2022-08-26 08:11:35 +00:00
"jsonrpc": "2.0",
"result": [
{
"height": 200004,
"tx_hash":
"acc3758bd2a26f869fcc67d48ff30b96464d476bca82c1cd6656e7d506816412"
},
{
"height": 215008,
"tx_hash":
"f3e1bf48975b8d6060a9de8884296abb80be618dc00ae3cb2f6cee3085e09403"
}
],
"id": "some requestId"
2023-05-29 16:30:41 +00:00
}),
2022-08-26 08:11:35 +00:00
);
final mockPrefs = MockPrefs();
2023-09-08 16:04:56 +00:00
when(mockPrefs.useTor).thenAnswer((realInvocation) => false);
final torService = MockTorService();
2022-08-26 08:11:35 +00:00
when(mockPrefs.wifiOnly).thenAnswer((_) => false);
final client = ElectrumX(
host: "some server",
port: 0,
useSSL: true,
client: mockClient,
prefs: mockPrefs,
2023-09-08 16:04:56 +00:00
torService: torService,
2022-08-26 08:11:35 +00:00
failovers: []);
final result = await client.getHistory(
scripthash: "dummy hash", requestID: "some requestId");
expect(result, [
{
"height": 200004,
"tx_hash":
"acc3758bd2a26f869fcc67d48ff30b96464d476bca82c1cd6656e7d506816412"
},
{
"height": 215008,
"tx_hash":
"f3e1bf48975b8d6060a9de8884296abb80be618dc00ae3cb2f6cee3085e09403"
}
]);
2023-09-08 16:04:56 +00:00
2022-08-26 08:11:35 +00:00
verify(mockPrefs.wifiOnly).called(1);
2023-09-08 16:04:56 +00:00
verify(mockPrefs.useTor).called(1);
2022-08-26 08:11:35 +00:00
verifyNoMoreInteractions(mockPrefs);
});
test("getHistory throws/fails", () {
final mockClient = MockJsonRPC();
const command = "blockchain.scripthash.get_history";
const jsonArgs = '["dummy hash"]';
when(
mockClient.request(
2023-05-29 16:30:41 +00:00
'{"jsonrpc": "2.0", "id": "some requestId",'
'"method": "$command","params": $jsonArgs}',
2023-07-27 21:18:02 +00:00
const Duration(minutes: 5),
2023-05-29 16:30:41 +00:00
),
2022-08-26 08:11:35 +00:00
).thenThrow(Exception());
final mockPrefs = MockPrefs();
2023-09-08 16:04:56 +00:00
when(mockPrefs.useTor).thenAnswer((realInvocation) => false);
final torService = MockTorService();
2022-08-26 08:11:35 +00:00
when(mockPrefs.wifiOnly).thenAnswer((_) => false);
final client = ElectrumX(
host: "some server",
port: 0,
useSSL: true,
client: mockClient,
prefs: mockPrefs,
2023-09-08 16:04:56 +00:00
torService: torService,
2022-08-26 08:11:35 +00:00
failovers: []);
expect(
() => client.getHistory(
scripthash: "dummy hash", requestID: "some requestId"),
throwsA(isA<Exception>()));
2023-09-08 16:04:56 +00:00
2022-08-26 08:11:35 +00:00
verify(mockPrefs.wifiOnly).called(1);
verifyNoMoreInteractions(mockPrefs);
});
});
group("getUTXOs", () {
test("getUTXOs success", () async {
final mockClient = MockJsonRPC();
const command = "blockchain.scripthash.listunspent";
const jsonArgs = '["dummy hash"]';
when(
mockClient.request(
2023-05-29 16:30:41 +00:00
'{"jsonrpc": "2.0", "id": "some requestId",'
'"method": "$command","params": $jsonArgs}',
2023-07-27 21:18:02 +00:00
const Duration(seconds: 60),
2023-05-29 16:30:41 +00:00
),
2022-08-26 08:11:35 +00:00
).thenAnswer(
2023-05-29 16:30:41 +00:00
(_) async => JsonRPCResponse(data: {
2022-08-26 08:11:35 +00:00
"jsonrpc": "2.0",
"result": [
{
"tx_pos": 0,
"value": 45318048,
"tx_hash":
"9f2c45a12db0144909b5db269415f7319179105982ac70ed80d76ea79d923ebf",
"height": 437146
},
{
"tx_pos": 0,
"value": 919195,
"tx_hash":
"3d2290c93436a3e964cfc2f0950174d8847b1fbe3946432c4784e168da0f019f",
"height": 441696
}
],
"id": "some requestId"
2023-05-29 16:30:41 +00:00
}),
2022-08-26 08:11:35 +00:00
);
final mockPrefs = MockPrefs();
2023-09-08 16:04:56 +00:00
when(mockPrefs.useTor).thenAnswer((realInvocation) => false);
final torService = MockTorService();
2022-08-26 08:11:35 +00:00
when(mockPrefs.wifiOnly).thenAnswer((_) => false);
final client = ElectrumX(
host: "some server",
port: 0,
useSSL: true,
client: mockClient,
prefs: mockPrefs,
2023-09-08 16:04:56 +00:00
torService: torService,
2022-08-26 08:11:35 +00:00
failovers: []);
final result = await client.getUTXOs(
scripthash: "dummy hash", requestID: "some requestId");
expect(result, [
{
"tx_pos": 0,
"value": 45318048,
"tx_hash":
"9f2c45a12db0144909b5db269415f7319179105982ac70ed80d76ea79d923ebf",
"height": 437146
},
{
"tx_pos": 0,
"value": 919195,
"tx_hash":
"3d2290c93436a3e964cfc2f0950174d8847b1fbe3946432c4784e168da0f019f",
"height": 441696
}
]);
2023-09-08 16:04:56 +00:00
2022-08-26 08:11:35 +00:00
verify(mockPrefs.wifiOnly).called(1);
2023-09-08 16:04:56 +00:00
verify(mockPrefs.useTor).called(1);
2022-08-26 08:11:35 +00:00
verifyNoMoreInteractions(mockPrefs);
});
test("getUTXOs throws/fails", () {
final mockClient = MockJsonRPC();
const command = "blockchain.scripthash.listunspent";
const jsonArgs = '["dummy hash"]';
when(
mockClient.request(
2023-05-29 16:30:41 +00:00
'{"jsonrpc": "2.0", "id": "some requestId",'
'"method": "$command","params": $jsonArgs}',
2023-07-27 21:18:02 +00:00
const Duration(seconds: 60),
2023-05-29 16:30:41 +00:00
),
2022-08-26 08:11:35 +00:00
).thenThrow(Exception());
final mockPrefs = MockPrefs();
2023-09-08 16:04:56 +00:00
when(mockPrefs.useTor).thenAnswer((realInvocation) => false);
final torService = MockTorService();
2022-08-26 08:11:35 +00:00
when(mockPrefs.wifiOnly).thenAnswer((_) => false);
final client = ElectrumX(
host: "some server",
port: 0,
useSSL: true,
client: mockClient,
prefs: mockPrefs,
2023-09-08 16:04:56 +00:00
torService: torService,
2022-08-26 08:11:35 +00:00
failovers: []);
expect(
() => client.getUTXOs(
scripthash: "dummy hash", requestID: "some requestId"),
throwsA(isA<Exception>()));
2023-09-08 16:04:56 +00:00
2022-08-26 08:11:35 +00:00
verify(mockPrefs.wifiOnly).called(1);
verifyNoMoreInteractions(mockPrefs);
});
});
group("getTransaction", () {
test("getTransaction success", () async {
final mockClient = MockJsonRPC();
const command = "blockchain.transaction.get";
const jsonArgs = '["${SampleGetTransactionData.txHash0}",true]';
when(
mockClient.request(
2023-05-29 16:30:41 +00:00
'{"jsonrpc": "2.0", "id": "some requestId",'
'"method": "$command","params": $jsonArgs}',
2023-07-27 21:18:02 +00:00
const Duration(seconds: 60),
2023-05-29 16:30:41 +00:00
),
2022-08-26 08:11:35 +00:00
).thenAnswer(
2023-05-29 16:30:41 +00:00
(_) async => JsonRPCResponse(data: {
2022-08-26 08:11:35 +00:00
"jsonrpc": "2.0",
"result": SampleGetTransactionData.txData0,
"id": "some requestId"
2023-05-29 16:30:41 +00:00
}),
2022-08-26 08:11:35 +00:00
);
final mockPrefs = MockPrefs();
2023-09-08 16:04:56 +00:00
when(mockPrefs.useTor).thenAnswer((realInvocation) => false);
final torService = MockTorService();
2022-08-26 08:11:35 +00:00
when(mockPrefs.wifiOnly).thenAnswer((_) => false);
final client = ElectrumX(
host: "some server",
port: 0,
useSSL: true,
client: mockClient,
prefs: mockPrefs,
2023-09-08 16:04:56 +00:00
torService: torService,
2022-08-26 08:11:35 +00:00
failovers: []);
final result = await client.getTransaction(
txHash: SampleGetTransactionData.txHash0,
verbose: true,
requestID: "some requestId");
expect(result, SampleGetTransactionData.txData0);
2023-09-08 16:04:56 +00:00
2022-08-26 08:11:35 +00:00
verify(mockPrefs.wifiOnly).called(1);
2023-09-08 16:04:56 +00:00
verify(mockPrefs.useTor).called(1);
2022-08-26 08:11:35 +00:00
verifyNoMoreInteractions(mockPrefs);
});
test("getTransaction throws/fails", () {
final mockClient = MockJsonRPC();
const command = "blockchain.transaction.get";
const jsonArgs = '["${SampleGetTransactionData.txHash0}",true]';
when(
mockClient.request(
2023-05-29 16:30:41 +00:00
'{"jsonrpc": "2.0", "id": "some requestId",'
'"method": "$command","params": $jsonArgs}',
2023-07-27 21:18:02 +00:00
const Duration(seconds: 60),
2023-05-29 16:30:41 +00:00
),
2022-08-26 08:11:35 +00:00
).thenThrow(Exception());
final mockPrefs = MockPrefs();
2023-09-08 16:04:56 +00:00
when(mockPrefs.useTor).thenAnswer((realInvocation) => false);
final torService = MockTorService();
2022-08-26 08:11:35 +00:00
when(mockPrefs.wifiOnly).thenAnswer((_) => false);
final client = ElectrumX(
host: "some server",
port: 0,
useSSL: true,
client: mockClient,
prefs: mockPrefs,
2023-09-08 16:04:56 +00:00
torService: torService,
2022-08-26 08:11:35 +00:00
failovers: []);
expect(
() => client.getTransaction(
txHash: SampleGetTransactionData.txHash0,
requestID: "some requestId"),
throwsA(isA<Exception>()));
2023-09-08 16:04:56 +00:00
2022-08-26 08:11:35 +00:00
verify(mockPrefs.wifiOnly).called(1);
verifyNoMoreInteractions(mockPrefs);
});
});
group("getAnonymitySet", () {
test("getAnonymitySet success", () async {
final mockClient = MockJsonRPC();
const command = "lelantus.getanonymityset";
const jsonArgs = '["1",""]';
when(
mockClient.request(
2023-05-29 16:30:41 +00:00
'{"jsonrpc": "2.0", "id": "some requestId",'
'"method": "$command","params": $jsonArgs}',
2023-07-27 21:18:02 +00:00
const Duration(seconds: 60),
2023-05-29 16:30:41 +00:00
),
2022-08-26 08:11:35 +00:00
).thenAnswer(
2023-05-29 16:30:41 +00:00
(_) async => JsonRPCResponse(data: {
2022-08-26 08:11:35 +00:00
"jsonrpc": "2.0",
"result": GetAnonymitySetSampleData.data,
"id": "some requestId"
2023-05-29 16:30:41 +00:00
}),
2022-08-26 08:11:35 +00:00
);
final mockPrefs = MockPrefs();
2023-09-08 16:04:56 +00:00
when(mockPrefs.useTor).thenAnswer((realInvocation) => false);
final torService = MockTorService();
2022-08-26 08:11:35 +00:00
when(mockPrefs.wifiOnly).thenAnswer((_) => false);
final client = ElectrumX(
host: "some server",
port: 0,
useSSL: true,
client: mockClient,
prefs: mockPrefs,
2023-09-08 16:04:56 +00:00
torService: torService,
2022-08-26 08:11:35 +00:00
failovers: []);
final result = await client.getAnonymitySet(
groupId: "1", blockhash: "", requestID: "some requestId");
expect(result, GetAnonymitySetSampleData.data);
2023-09-08 16:04:56 +00:00
2022-08-26 08:11:35 +00:00
verify(mockPrefs.wifiOnly).called(1);
2023-09-08 16:04:56 +00:00
verify(mockPrefs.useTor).called(1);
2022-08-26 08:11:35 +00:00
verifyNoMoreInteractions(mockPrefs);
});
test("getAnonymitySet throws/fails", () {
final mockClient = MockJsonRPC();
const command = "lelantus.getanonymityset";
const jsonArgs = '["1",""]';
when(
mockClient.request(
2023-05-29 16:30:41 +00:00
'{"jsonrpc": "2.0", "id": "some requestId",'
'"method": "$command","params": $jsonArgs}',
2023-07-27 21:18:02 +00:00
const Duration(seconds: 60),
2023-05-29 16:30:41 +00:00
),
2022-08-26 08:11:35 +00:00
).thenThrow(Exception());
final mockPrefs = MockPrefs();
2023-09-08 16:04:56 +00:00
when(mockPrefs.useTor).thenAnswer((realInvocation) => false);
final torService = MockTorService();
2022-08-26 08:11:35 +00:00
when(mockPrefs.wifiOnly).thenAnswer((_) => false);
final client = ElectrumX(
host: "some server",
port: 0,
useSSL: true,
client: mockClient,
prefs: mockPrefs,
2023-09-08 16:04:56 +00:00
torService: torService,
2022-08-26 08:11:35 +00:00
failovers: []);
expect(
() =>
client.getAnonymitySet(groupId: "1", requestID: "some requestId"),
throwsA(isA<Exception>()));
2023-09-08 16:04:56 +00:00
2022-08-26 08:11:35 +00:00
verify(mockPrefs.wifiOnly).called(1);
verifyNoMoreInteractions(mockPrefs);
});
});
group("getMintData", () {
test("getMintData success", () async {
final mockClient = MockJsonRPC();
const command = "lelantus.getmintmetadata";
const jsonArgs = '["some mints"]';
when(
mockClient.request(
2023-05-29 16:30:41 +00:00
'{"jsonrpc": "2.0", "id": "some requestId",'
'"method": "$command","params": $jsonArgs}',
2023-07-27 21:18:02 +00:00
const Duration(seconds: 60),
2023-05-29 16:30:41 +00:00
),
2022-08-26 08:11:35 +00:00
).thenAnswer(
2023-05-29 16:30:41 +00:00
(_) async => JsonRPCResponse(data: {
2022-08-26 08:11:35 +00:00
"jsonrpc": "2.0",
"result": "mint meta data",
"id": "some requestId"
2023-05-29 16:30:41 +00:00
}),
2022-08-26 08:11:35 +00:00
);
final mockPrefs = MockPrefs();
2023-09-08 16:04:56 +00:00
when(mockPrefs.useTor).thenAnswer((realInvocation) => false);
final torService = MockTorService();
2022-08-26 08:11:35 +00:00
when(mockPrefs.wifiOnly).thenAnswer((_) => false);
final client = ElectrumX(
host: "some server",
port: 0,
useSSL: true,
client: mockClient,
prefs: mockPrefs,
2023-09-08 16:04:56 +00:00
torService: torService,
2022-08-26 08:11:35 +00:00
failovers: []);
final result = await client.getMintData(
mints: "some mints", requestID: "some requestId");
expect(result, "mint meta data");
2023-09-08 16:04:56 +00:00
2022-08-26 08:11:35 +00:00
verify(mockPrefs.wifiOnly).called(1);
2023-09-08 16:04:56 +00:00
verify(mockPrefs.useTor).called(1);
2022-08-26 08:11:35 +00:00
verifyNoMoreInteractions(mockPrefs);
});
test("getMintData throws/fails", () {
final mockClient = MockJsonRPC();
const command = "lelantus.getmintmetadata";
const jsonArgs = '["some mints"]';
when(
mockClient.request(
2023-05-29 16:30:41 +00:00
'{"jsonrpc": "2.0", "id": "some requestId",'
'"method": "$command","params": $jsonArgs}',
2023-07-27 21:18:02 +00:00
const Duration(seconds: 60),
2023-05-29 16:30:41 +00:00
),
2022-08-26 08:11:35 +00:00
).thenThrow(Exception());
final mockPrefs = MockPrefs();
2023-09-08 16:04:56 +00:00
when(mockPrefs.useTor).thenAnswer((realInvocation) => false);
final torService = MockTorService();
2022-08-26 08:11:35 +00:00
when(mockPrefs.wifiOnly).thenAnswer((_) => false);
final client = ElectrumX(
host: "some server",
port: 0,
useSSL: true,
client: mockClient,
prefs: mockPrefs,
2023-09-08 16:04:56 +00:00
torService: torService,
2022-08-26 08:11:35 +00:00
failovers: []);
expect(
() => client.getMintData(
mints: "some mints", requestID: "some requestId"),
throwsA(isA<Exception>()));
2023-09-08 16:04:56 +00:00
2022-08-26 08:11:35 +00:00
verify(mockPrefs.wifiOnly).called(1);
verifyNoMoreInteractions(mockPrefs);
});
});
group("getUsedCoinSerials", () {
test("getUsedCoinSerials success", () async {
final mockClient = MockJsonRPC();
const command = "lelantus.getusedcoinserials";
const jsonArgs = '["0"]';
when(
mockClient.request(
2023-05-29 16:30:41 +00:00
'{"jsonrpc": "2.0", "id": "some requestId",'
'"method": "$command","params": $jsonArgs}',
2023-07-27 21:18:02 +00:00
const Duration(minutes: 2),
2023-05-29 16:30:41 +00:00
),
2022-08-26 08:11:35 +00:00
).thenAnswer(
2023-05-29 16:30:41 +00:00
(_) async => JsonRPCResponse(data: {
2022-08-26 08:11:35 +00:00
"jsonrpc": "2.0",
"result": GetUsedSerialsSampleData.serials,
"id": "some requestId"
2023-05-29 16:30:41 +00:00
}),
2022-08-26 08:11:35 +00:00
);
final mockPrefs = MockPrefs();
2023-09-08 16:04:56 +00:00
when(mockPrefs.useTor).thenAnswer((realInvocation) => false);
final torService = MockTorService();
2022-08-26 08:11:35 +00:00
when(mockPrefs.wifiOnly).thenAnswer((_) => false);
final client = ElectrumX(
host: "some server",
port: 0,
useSSL: true,
client: mockClient,
prefs: mockPrefs,
2023-09-08 16:04:56 +00:00
torService: torService,
2022-08-26 08:11:35 +00:00
failovers: []);
final result = await client.getUsedCoinSerials(
requestID: "some requestId", startNumber: 0);
expect(result, GetUsedSerialsSampleData.serials);
2023-09-08 16:04:56 +00:00
2022-08-26 08:11:35 +00:00
verify(mockPrefs.wifiOnly).called(1);
2023-09-08 16:04:56 +00:00
verify(mockPrefs.useTor).called(1);
2022-08-26 08:11:35 +00:00
verifyNoMoreInteractions(mockPrefs);
});
test("getUsedCoinSerials throws/fails", () {
final mockClient = MockJsonRPC();
const command = "lelantus.getusedcoinserials";
const jsonArgs = '["0"]';
when(
mockClient.request(
2023-05-29 16:30:41 +00:00
'{"jsonrpc": "2.0", "id": "some requestId",'
'"method": "$command","params": $jsonArgs}',
2023-07-27 21:18:02 +00:00
const Duration(minutes: 2),
2023-05-29 16:30:41 +00:00
),
2022-08-26 08:11:35 +00:00
).thenThrow(Exception());
final mockPrefs = MockPrefs();
2023-09-08 16:04:56 +00:00
when(mockPrefs.useTor).thenAnswer((realInvocation) => false);
final torService = MockTorService();
2022-08-26 08:11:35 +00:00
when(mockPrefs.wifiOnly).thenAnswer((_) => false);
final client = ElectrumX(
host: "some server",
port: 0,
useSSL: true,
client: mockClient,
prefs: mockPrefs,
2023-09-08 16:04:56 +00:00
torService: torService,
2022-08-26 08:11:35 +00:00
failovers: []);
expect(
() => client.getUsedCoinSerials(
requestID: "some requestId", startNumber: 0),
throwsA(isA<Exception>()));
2023-09-08 16:04:56 +00:00
2022-08-26 08:11:35 +00:00
verify(mockPrefs.wifiOnly).called(1);
verifyNoMoreInteractions(mockPrefs);
});
});
group("getLatestCoinId", () {
test("getLatestCoinId success", () async {
final mockClient = MockJsonRPC();
const command = "lelantus.getlatestcoinid";
const jsonArgs = '[]';
when(
mockClient.request(
2023-05-29 16:30:41 +00:00
'{"jsonrpc": "2.0", "id": "some requestId",'
'"method": "$command","params": $jsonArgs}',
2023-07-27 21:18:02 +00:00
const Duration(seconds: 60),
2023-05-29 16:30:41 +00:00
),
2022-08-26 08:11:35 +00:00
).thenAnswer(
2023-05-29 16:30:41 +00:00
(_) async => JsonRPCResponse(data: {
"jsonrpc": "2.0",
"result": 1,
"id": "some requestId",
}),
2022-08-26 08:11:35 +00:00
);
final mockPrefs = MockPrefs();
2023-09-08 16:04:56 +00:00
when(mockPrefs.useTor).thenAnswer((realInvocation) => false);
final torService = MockTorService();
2022-08-26 08:11:35 +00:00
when(mockPrefs.wifiOnly).thenAnswer((_) => false);
final client = ElectrumX(
host: "some server",
port: 0,
useSSL: true,
client: mockClient,
prefs: mockPrefs,
2023-09-08 16:04:56 +00:00
torService: torService,
2022-08-26 08:11:35 +00:00
failovers: []);
final result = await client.getLatestCoinId(requestID: "some requestId");
expect(result, 1);
2023-09-08 16:04:56 +00:00
2022-08-26 08:11:35 +00:00
verify(mockPrefs.wifiOnly).called(1);
2023-09-08 16:04:56 +00:00
verify(mockPrefs.useTor).called(1);
2022-08-26 08:11:35 +00:00
verifyNoMoreInteractions(mockPrefs);
});
test("getLatestCoinId throws/fails", () {
final mockClient = MockJsonRPC();
const command = "lelantus.getlatestcoinid";
const jsonArgs = '[]';
when(
mockClient.request(
2023-05-29 16:30:41 +00:00
'{"jsonrpc": "2.0", "id": "some requestId",'
'"method": "$command","params": $jsonArgs}',
2023-07-27 21:18:02 +00:00
const Duration(seconds: 60),
2023-05-29 16:30:41 +00:00
),
2022-08-26 08:11:35 +00:00
).thenThrow(Exception());
final mockPrefs = MockPrefs();
2023-09-08 16:04:56 +00:00
when(mockPrefs.useTor).thenAnswer((realInvocation) => false);
final torService = MockTorService();
2022-08-26 08:11:35 +00:00
when(mockPrefs.wifiOnly).thenAnswer((_) => false);
final client = ElectrumX(
host: "some server",
port: 0,
useSSL: true,
client: mockClient,
prefs: mockPrefs,
2023-09-08 16:04:56 +00:00
torService: torService,
2022-08-26 08:11:35 +00:00
failovers: []);
2023-05-29 16:30:41 +00:00
expect(
() => client.getLatestCoinId(
requestID: "some requestId",
),
2022-08-26 08:11:35 +00:00
throwsA(isA<Exception>()));
2023-09-08 16:04:56 +00:00
2022-08-26 08:11:35 +00:00
verify(mockPrefs.wifiOnly).called(1);
verifyNoMoreInteractions(mockPrefs);
});
});
group("getCoinsForRecovery", () {
test("getCoinsForRecovery success", () async {
final mockClient = MockJsonRPC();
const command = "lelantus.getanonymityset";
const jsonArgs = '["1",""]';
when(
mockClient.request(
2023-05-29 16:30:41 +00:00
'{"jsonrpc": "2.0", "id": "some requestId",'
'"method": "$command","params": $jsonArgs}',
2023-07-27 21:18:02 +00:00
const Duration(seconds: 60),
2023-05-29 16:30:41 +00:00
),
2022-08-26 08:11:35 +00:00
).thenAnswer(
2023-05-29 16:30:41 +00:00
(_) async => JsonRPCResponse(data: {
2022-08-26 08:11:35 +00:00
"jsonrpc": "2.0",
"result": GetAnonymitySetSampleData.data,
"id": "some requestId"
2023-05-29 16:30:41 +00:00
}),
2022-08-26 08:11:35 +00:00
);
final mockPrefs = MockPrefs();
2023-09-08 16:04:56 +00:00
when(mockPrefs.useTor).thenAnswer((realInvocation) => false);
final torService = MockTorService();
2022-08-26 08:11:35 +00:00
when(mockPrefs.wifiOnly).thenAnswer((_) => false);
final client = ElectrumX(
host: "some server",
port: 0,
useSSL: true,
client: mockClient,
prefs: mockPrefs,
2023-09-08 16:04:56 +00:00
torService: torService,
2022-08-26 08:11:35 +00:00
failovers: []);
final result = await client.getAnonymitySet(
groupId: "1", blockhash: "", requestID: "some requestId");
expect(result, GetAnonymitySetSampleData.data);
2023-09-08 16:04:56 +00:00
2022-08-26 08:11:35 +00:00
verify(mockPrefs.wifiOnly).called(1);
2023-09-08 16:04:56 +00:00
verify(mockPrefs.useTor).called(1);
2022-08-26 08:11:35 +00:00
verifyNoMoreInteractions(mockPrefs);
});
test("getAnonymitySet throws/fails", () {
final mockClient = MockJsonRPC();
const command = "lelantus.getanonymityset";
const jsonArgs = '["1",""]';
when(
mockClient.request(
2023-05-29 16:30:41 +00:00
'{"jsonrpc": "2.0", "id": "some requestId",'
'"method": "$command","params": $jsonArgs}',
2023-07-27 21:18:02 +00:00
const Duration(seconds: 60),
2023-05-29 16:30:41 +00:00
),
2022-08-26 08:11:35 +00:00
).thenThrow(Exception());
final mockPrefs = MockPrefs();
2023-09-08 16:04:56 +00:00
when(mockPrefs.useTor).thenAnswer((realInvocation) => false);
final torService = MockTorService();
2022-08-26 08:11:35 +00:00
when(mockPrefs.wifiOnly).thenAnswer((_) => false);
final client = ElectrumX(
host: "some server",
port: 0,
useSSL: true,
client: mockClient,
prefs: mockPrefs,
2023-09-08 16:04:56 +00:00
torService: torService,
2022-08-26 08:11:35 +00:00
failovers: []);
expect(
2023-05-29 16:30:41 +00:00
() => client.getAnonymitySet(
groupId: "1",
requestID: "some requestId",
),
2022-08-26 08:11:35 +00:00
throwsA(isA<Exception>()));
2023-09-08 16:04:56 +00:00
2022-08-26 08:11:35 +00:00
verify(mockPrefs.wifiOnly).called(1);
verifyNoMoreInteractions(mockPrefs);
});
});
group("getMintData", () {
test("getMintData success", () async {
final mockClient = MockJsonRPC();
const command = "lelantus.getmintmetadata";
const jsonArgs = '["some mints"]';
when(
mockClient.request(
2023-05-29 16:30:41 +00:00
'{"jsonrpc": "2.0", "id": "some requestId",'
'"method": "$command","params": $jsonArgs}',
2023-07-27 21:18:02 +00:00
const Duration(seconds: 60),
2023-05-29 16:30:41 +00:00
),
2022-08-26 08:11:35 +00:00
).thenAnswer(
2023-05-29 16:30:41 +00:00
(_) async => JsonRPCResponse(data: {
2022-08-26 08:11:35 +00:00
"jsonrpc": "2.0",
"result": "mint meta data",
"id": "some requestId"
2023-05-29 16:30:41 +00:00
}),
2022-08-26 08:11:35 +00:00
);
final mockPrefs = MockPrefs();
2023-09-08 16:04:56 +00:00
when(mockPrefs.useTor).thenAnswer((realInvocation) => false);
final torService = MockTorService();
2022-08-26 08:11:35 +00:00
when(mockPrefs.wifiOnly).thenAnswer((_) => false);
final client = ElectrumX(
host: "some server",
port: 0,
useSSL: true,
client: mockClient,
prefs: mockPrefs,
2023-09-08 16:04:56 +00:00
torService: torService,
2022-08-26 08:11:35 +00:00
failovers: []);
final result = await client.getMintData(
mints: "some mints", requestID: "some requestId");
expect(result, "mint meta data");
2023-09-08 16:04:56 +00:00
2022-08-26 08:11:35 +00:00
verify(mockPrefs.wifiOnly).called(1);
2023-09-08 16:04:56 +00:00
verify(mockPrefs.useTor).called(1);
2022-08-26 08:11:35 +00:00
verifyNoMoreInteractions(mockPrefs);
});
test("getMintData throws/fails", () {
final mockClient = MockJsonRPC();
const command = "lelantus.getmintmetadata";
const jsonArgs = '["some mints"]';
when(
mockClient.request(
2023-05-29 16:30:41 +00:00
'{"jsonrpc": "2.0", "id": "some requestId",'
'"method": "$command","params": $jsonArgs}',
2023-07-27 21:18:02 +00:00
const Duration(seconds: 60),
2023-05-29 16:30:41 +00:00
),
2022-08-26 08:11:35 +00:00
).thenThrow(Exception());
final mockPrefs = MockPrefs();
2023-09-08 16:04:56 +00:00
when(mockPrefs.useTor).thenAnswer((realInvocation) => false);
final torService = MockTorService();
2022-08-26 08:11:35 +00:00
when(mockPrefs.wifiOnly).thenAnswer((_) => false);
final client = ElectrumX(
host: "some server",
port: 0,
useSSL: true,
client: mockClient,
prefs: mockPrefs,
2023-09-08 16:04:56 +00:00
torService: torService,
2022-08-26 08:11:35 +00:00
failovers: []);
expect(
() => client.getMintData(
2023-05-29 16:30:41 +00:00
mints: "some mints",
requestID: "some requestId",
),
2022-08-26 08:11:35 +00:00
throwsA(isA<Exception>()));
2023-09-08 16:04:56 +00:00
2022-08-26 08:11:35 +00:00
verify(mockPrefs.wifiOnly).called(1);
verifyNoMoreInteractions(mockPrefs);
});
});
group("getUsedCoinSerials", () {
test("getUsedCoinSerials success", () async {
final mockClient = MockJsonRPC();
const command = "lelantus.getusedcoinserials";
const jsonArgs = '["0"]';
when(
mockClient.request(
2023-05-29 16:30:41 +00:00
'{"jsonrpc": "2.0", "id": "some requestId",'
'"method": "$command","params": $jsonArgs}',
2023-07-27 21:18:02 +00:00
const Duration(minutes: 2),
2023-05-29 16:30:41 +00:00
),
2022-08-26 08:11:35 +00:00
).thenAnswer(
2023-05-29 16:30:41 +00:00
(_) async => JsonRPCResponse(data: {
2022-08-26 08:11:35 +00:00
"jsonrpc": "2.0",
"result": GetUsedSerialsSampleData.serials,
"id": "some requestId"
2023-05-29 16:30:41 +00:00
}),
2022-08-26 08:11:35 +00:00
);
final mockPrefs = MockPrefs();
2023-09-08 16:04:56 +00:00
when(mockPrefs.useTor).thenAnswer((realInvocation) => false);
final torService = MockTorService();
2022-08-26 08:11:35 +00:00
when(mockPrefs.wifiOnly).thenAnswer((_) => false);
final client = ElectrumX(
host: "some server",
port: 0,
useSSL: true,
client: mockClient,
prefs: mockPrefs,
2023-09-08 16:04:56 +00:00
torService: torService,
2022-08-26 08:11:35 +00:00
failovers: []);
final result = await client.getUsedCoinSerials(
requestID: "some requestId", startNumber: 0);
expect(result, GetUsedSerialsSampleData.serials);
2023-09-08 16:04:56 +00:00
2022-08-26 08:11:35 +00:00
verify(mockPrefs.wifiOnly).called(1);
2023-09-08 16:04:56 +00:00
verify(mockPrefs.useTor).called(1);
2022-08-26 08:11:35 +00:00
verifyNoMoreInteractions(mockPrefs);
});
test("getUsedCoinSerials throws/fails", () {
final mockClient = MockJsonRPC();
const command = "lelantus.getusedcoinserials";
const jsonArgs = '["0"]';
when(
mockClient.request(
2023-05-29 16:30:41 +00:00
'{"jsonrpc": "2.0", "id": "some requestId",'
'"method": "$command","params": $jsonArgs}',
2023-07-27 21:18:02 +00:00
const Duration(minutes: 2),
2023-05-29 16:30:41 +00:00
),
2022-08-26 08:11:35 +00:00
).thenThrow(Exception());
final mockPrefs = MockPrefs();
2023-09-08 16:04:56 +00:00
when(mockPrefs.useTor).thenAnswer((realInvocation) => false);
final torService = MockTorService();
2022-08-26 08:11:35 +00:00
when(mockPrefs.wifiOnly).thenAnswer((_) => false);
final client = ElectrumX(
host: "some server",
port: 0,
useSSL: true,
client: mockClient,
prefs: mockPrefs,
2023-09-08 16:04:56 +00:00
torService: torService,
2022-08-26 08:11:35 +00:00
failovers: []);
expect(
() => client.getUsedCoinSerials(
requestID: "some requestId", startNumber: 0),
throwsA(isA<Exception>()));
2023-09-08 16:04:56 +00:00
2022-08-26 08:11:35 +00:00
verify(mockPrefs.wifiOnly).called(1);
verifyNoMoreInteractions(mockPrefs);
});
});
group("getLatestCoinId", () {
test("getLatestCoinId success", () async {
final mockClient = MockJsonRPC();
const command = "lelantus.getlatestcoinid";
const jsonArgs = '[]';
when(
mockClient.request(
2023-05-29 16:30:41 +00:00
'{"jsonrpc": "2.0", "id": "some requestId",'
'"method": "$command","params": $jsonArgs}',
2023-07-27 21:18:02 +00:00
const Duration(seconds: 60),
2023-05-29 16:30:41 +00:00
),
2022-08-26 08:11:35 +00:00
).thenAnswer(
2023-05-29 16:30:41 +00:00
(_) async => JsonRPCResponse(data: {
"jsonrpc": "2.0",
"result": 1,
"id": "some requestId",
}),
2022-08-26 08:11:35 +00:00
);
final mockPrefs = MockPrefs();
2023-09-08 16:04:56 +00:00
when(mockPrefs.useTor).thenAnswer((realInvocation) => false);
final torService = MockTorService();
2022-08-26 08:11:35 +00:00
when(mockPrefs.wifiOnly).thenAnswer((_) => false);
final client = ElectrumX(
host: "some server",
port: 0,
useSSL: true,
client: mockClient,
prefs: mockPrefs,
2023-09-08 16:04:56 +00:00
torService: torService,
2022-08-26 08:11:35 +00:00
failovers: []);
final result = await client.getLatestCoinId(requestID: "some requestId");
expect(result, 1);
2023-09-08 16:04:56 +00:00
2022-08-26 08:11:35 +00:00
verify(mockPrefs.wifiOnly).called(1);
2023-09-08 16:04:56 +00:00
verify(mockPrefs.useTor).called(1);
2022-08-26 08:11:35 +00:00
verifyNoMoreInteractions(mockPrefs);
});
test("getLatestCoinId throws/fails", () {
final mockClient = MockJsonRPC();
const command = "lelantus.getlatestcoinid";
const jsonArgs = '[]';
when(
mockClient.request(
2023-05-29 16:30:41 +00:00
'{"jsonrpc": "2.0", "id": "some requestId",'
'"method": "$command","params": $jsonArgs}',
2023-07-27 21:18:02 +00:00
const Duration(seconds: 60),
2023-05-29 16:30:41 +00:00
),
2022-08-26 08:11:35 +00:00
).thenThrow(Exception());
final mockPrefs = MockPrefs();
2023-09-08 16:04:56 +00:00
when(mockPrefs.useTor).thenAnswer((realInvocation) => false);
final torService = MockTorService();
2022-08-26 08:11:35 +00:00
when(mockPrefs.wifiOnly).thenAnswer((_) => false);
final client = ElectrumX(
host: "some server",
port: 0,
useSSL: true,
client: mockClient,
prefs: mockPrefs,
2023-09-08 16:04:56 +00:00
torService: torService,
2022-08-26 08:11:35 +00:00
failovers: []);
expect(() => client.getLatestCoinId(requestID: "some requestId"),
throwsA(isA<Exception>()));
2023-09-08 16:04:56 +00:00
2022-08-26 08:11:35 +00:00
verify(mockPrefs.wifiOnly).called(1);
verifyNoMoreInteractions(mockPrefs);
});
});
group("getFeeRate", () {
test("getFeeRate success", () async {
final mockClient = MockJsonRPC();
const command = "blockchain.getfeerate";
const jsonArgs = '[]';
when(
mockClient.request(
2023-05-29 16:30:41 +00:00
'{"jsonrpc": "2.0", "id": "some requestId",'
'"method": "$command","params": $jsonArgs}',
2023-07-27 21:18:02 +00:00
const Duration(seconds: 60),
2023-05-29 16:30:41 +00:00
),
2022-08-26 08:11:35 +00:00
).thenAnswer(
2023-05-29 16:30:41 +00:00
(_) async => JsonRPCResponse(data: {
2022-08-26 08:11:35 +00:00
"jsonrpc": "2.0",
"result": {
"rate": 1000,
},
"id": "some requestId"
2023-05-29 16:30:41 +00:00
}),
2022-08-26 08:11:35 +00:00
);
final mockPrefs = MockPrefs();
2023-09-08 16:04:56 +00:00
when(mockPrefs.useTor).thenAnswer((realInvocation) => false);
final torService = MockTorService();
2022-08-26 08:11:35 +00:00
when(mockPrefs.wifiOnly).thenAnswer((_) => false);
final client = ElectrumX(
host: "some server",
port: 0,
useSSL: true,
client: mockClient,
prefs: mockPrefs,
2023-09-08 16:04:56 +00:00
torService: torService,
2022-08-26 08:11:35 +00:00
failovers: []);
final result = await client.getFeeRate(requestID: "some requestId");
expect(result, {"rate": 1000});
2023-09-08 16:04:56 +00:00
2022-08-26 08:11:35 +00:00
verify(mockPrefs.wifiOnly).called(1);
2023-09-08 16:04:56 +00:00
verify(mockPrefs.useTor).called(1);
2022-08-26 08:11:35 +00:00
verifyNoMoreInteractions(mockPrefs);
});
test("getFeeRate throws/fails", () {
final mockClient = MockJsonRPC();
const command = "blockchain.getfeerate";
const jsonArgs = '[]';
when(
mockClient.request(
2023-05-29 16:30:41 +00:00
'{"jsonrpc": "2.0", "id": "some requestId",'
'"method": "$command","params": $jsonArgs}',
2023-07-27 21:18:02 +00:00
const Duration(seconds: 60),
2023-05-29 16:30:41 +00:00
),
2022-08-26 08:11:35 +00:00
).thenThrow(Exception());
final mockPrefs = MockPrefs();
2023-09-08 16:04:56 +00:00
when(mockPrefs.useTor).thenAnswer((realInvocation) => false);
final torService = MockTorService();
2022-08-26 08:11:35 +00:00
when(mockPrefs.wifiOnly).thenAnswer((_) => false);
final client = ElectrumX(
host: "some server",
port: 0,
useSSL: true,
client: mockClient,
prefs: mockPrefs,
2023-09-08 16:04:56 +00:00
torService: torService,
2022-08-26 08:11:35 +00:00
failovers: []);
expect(() => client.getFeeRate(requestID: "some requestId"),
throwsA(isA<Exception>()));
2023-09-08 16:04:56 +00:00
2022-08-26 08:11:35 +00:00
verify(mockPrefs.wifiOnly).called(1);
verifyNoMoreInteractions(mockPrefs);
});
});
test("rpcClient is null throws with bad server info", () {
final mockPrefs = MockPrefs();
2023-09-08 16:04:56 +00:00
when(mockPrefs.useTor).thenAnswer((realInvocation) => false);
final torService = MockTorService();
2022-08-26 08:11:35 +00:00
when(mockPrefs.wifiOnly).thenAnswer((_) => false);
final client = ElectrumX(
client: null,
port: -10,
host: "_ :sa %",
useSSL: false,
prefs: mockPrefs,
2023-09-08 16:04:56 +00:00
torService: torService,
2022-08-26 08:11:35 +00:00
failovers: [],
);
expect(() => client.getFeeRate(), throwsA(isA<Exception>()));
2023-09-08 16:04:56 +00:00
2022-08-26 08:11:35 +00:00
verify(mockPrefs.wifiOnly).called(1);
verifyNoMoreInteractions(mockPrefs);
});
2023-09-08 16:45:07 +00:00
group("Tor tests", () {
// useTor is false, so no TorService calls should be made.
test("Tor not in use", () async {
final mockClient = MockJsonRPC();
const command = "blockchain.transaction.get";
const jsonArgs = '["${SampleGetTransactionData.txHash0}",true]';
when(mockClient.request(
'{"jsonrpc": "2.0", "id": "some requestId","method": "$command","params": $jsonArgs}',
const Duration(seconds: 60),
)).thenAnswer((_) async => JsonRPCResponse(data: {
"jsonrpc": "2.0",
"result": SampleGetTransactionData.txData0,
"id": "some requestId",
}));
final mockPrefs = MockPrefs();
when(mockPrefs.useTor).thenAnswer((_) => false);
when(mockPrefs.torKillswitch)
.thenAnswer((_) => false); // Or true, shouldn't matter.
when(mockPrefs.wifiOnly).thenAnswer((_) => false);
final mockTorService = MockTorService();
when(mockTorService.enabled).thenAnswer((_) => false);
final client = ElectrumX(
host: "some server",
port: 0,
useSSL: true,
client: mockClient,
failovers: [],
prefs: mockPrefs,
torService: mockTorService,
);
final result = await client.getTransaction(
txHash: SampleGetTransactionData.txHash0,
verbose: true,
requestID: "some requestId");
expect(result, SampleGetTransactionData.txData0);
verify(mockPrefs.wifiOnly).called(1);
verify(mockPrefs.useTor).called(1);
verifyNever(mockPrefs.torKillswitch);
verifyNoMoreInteractions(mockPrefs);
verifyNever(mockTorService.enabled);
verifyNoMoreInteractions(mockTorService);
});
// useTor is true, but TorService is not enabled and the killswitch is off, so a clearnet call should be made.
test("useTor but Tor unavailable but killswitch off", () async {
final mockClient = MockJsonRPC();
const command = "blockchain.transaction.get";
const jsonArgs = '["${SampleGetTransactionData.txHash0}",true]';
when(mockClient.request(
'{"jsonrpc": "2.0", "id": "some requestId","method": "$command","params": $jsonArgs}',
const Duration(seconds: 60),
)).thenAnswer((_) async => JsonRPCResponse(data: {
"jsonrpc": "2.0",
"result": SampleGetTransactionData.txData0,
"id": "some requestId",
}));
final mockPrefs = MockPrefs();
when(mockPrefs.useTor).thenAnswer((_) => true);
when(mockPrefs.torKillswitch).thenAnswer((_) => false);
when(mockPrefs.wifiOnly).thenAnswer((_) => false);
final mockTorService = MockTorService();
when(mockTorService.enabled).thenAnswer((_) => false);
final client = ElectrumX(
host: "some server",
port: 0,
useSSL: true,
client: mockClient,
prefs: mockPrefs,
torService: mockTorService,
failovers: []);
final result = await client.getTransaction(
txHash: SampleGetTransactionData.txHash0,
verbose: true,
requestID: "some requestId");
expect(result, SampleGetTransactionData.txData0);
verify(mockPrefs.wifiOnly).called(1);
verify(mockPrefs.useTor).called(1);
verify(mockPrefs.torKillswitch).called(1);
verifyNoMoreInteractions(mockPrefs);
verify(mockTorService.enabled).called(1);
verifyNoMoreInteractions(mockTorService);
});
2023-09-08 19:51:00 +00:00
// useTor is true, but TorService is not enabled and the killswitch is on, so no TorService calls should be made.
2023-09-08 16:45:07 +00:00
test("killswitch enabled", () async {
final mockClient = MockJsonRPC();
const command = "blockchain.transaction.get";
const jsonArgs = '["",true]';
when(
mockClient.request(
'{"jsonrpc": "2.0", "id": "some requestId",'
'"method": "$command","params": $jsonArgs}',
const Duration(seconds: 60),
),
).thenAnswer(
(_) async => JsonRPCResponse(data: {
"jsonrpc": "2.0",
"error": {
"code": 1,
"message": "None should be a transaction hash",
},
"id": "some requestId",
}),
);
final mockPrefs = MockPrefs();
when(mockPrefs.useTor).thenAnswer((_) => true);
when(mockPrefs.torKillswitch).thenAnswer((_) => true);
2023-09-08 16:45:07 +00:00
when(mockPrefs.wifiOnly).thenAnswer((_) => false);
2023-09-08 19:51:00 +00:00
final mockTorService = MockTorService();
when(mockTorService.enabled).thenAnswer((_) => false);
2023-09-08 16:45:07 +00:00
final client = ElectrumX(
host: "some server",
port: 0,
useSSL: true,
client: mockClient,
failovers: [],
prefs: mockPrefs,
2023-09-08 19:51:00 +00:00
torService: mockTorService,
2023-09-08 16:45:07 +00:00
);
try {
var result = await client.getTransaction(
requestID: "some requestId", txHash: '');
2023-09-08 16:45:07 +00:00
} catch (e) {
expect(e, isA<Exception>());
expect(
e.toString(),
equals(
"Exception: Tor preference and killswitch set but Tor is not enabled, not connecting to ElectrumX"));
2023-09-08 16:45:07 +00:00
}
verify(mockPrefs.wifiOnly).called(1);
verify(mockPrefs.useTor).called(1);
verify(mockPrefs.torKillswitch).called(1);
2023-09-08 16:45:07 +00:00
verifyNoMoreInteractions(mockPrefs);
2023-09-08 19:51:00 +00:00
verify(mockTorService.enabled).called(1);
verifyNoMoreInteractions(mockTorService);
2023-09-08 16:45:07 +00:00
});
2023-09-08 19:50:43 +00:00
// useTor is true but Tor is not enabled, but because the killswitch is off, TorService calls should be made.
2023-09-08 16:45:07 +00:00
test("killswitch disabled", () async {
final mockClient = MockJsonRPC();
const command = "blockchain.transaction.get";
const jsonArgs = '["${SampleGetTransactionData.txHash0}",true]';
2023-09-08 16:45:07 +00:00
when(
mockClient.request(
'{"jsonrpc": "2.0", "id": "some requestId",'
'"method": "$command","params": $jsonArgs}',
const Duration(seconds: 60),
),
).thenAnswer(
(_) async => JsonRPCResponse(data: {
"jsonrpc": "2.0",
"result": SampleGetTransactionData.txData0,
"id": "some requestId"
2023-09-08 16:45:07 +00:00
}),
);
final mockPrefs = MockPrefs();
when(mockPrefs.useTor).thenAnswer((_) => true);
2023-09-08 19:14:36 +00:00
when(mockPrefs.torKillswitch).thenAnswer((_) => false);
2023-09-08 16:45:07 +00:00
when(mockPrefs.wifiOnly).thenAnswer((_) => false);
2023-09-08 19:50:43 +00:00
final mockTorService = MockTorService();
when(mockTorService.enabled).thenAnswer((_) => false);
2023-09-08 16:45:07 +00:00
final client = ElectrumX(
host: "some server",
port: 0,
useSSL: true,
client: mockClient,
failovers: [],
prefs: mockPrefs,
2023-09-08 19:50:43 +00:00
torService: mockTorService,
2023-09-08 16:45:07 +00:00
);
2023-09-08 19:14:36 +00:00
final result = await client.getTransaction(
txHash: SampleGetTransactionData.txHash0,
verbose: true,
requestID: "some requestId");
2023-09-08 16:45:07 +00:00
2023-09-08 19:14:36 +00:00
expect(result, SampleGetTransactionData.txData0);
2023-09-08 16:45:07 +00:00
verify(mockPrefs.wifiOnly).called(1);
2023-09-08 16:45:07 +00:00
verify(mockPrefs.useTor).called(1);
verify(mockPrefs.torKillswitch).called(1);
2023-09-08 16:45:07 +00:00
verifyNoMoreInteractions(mockPrefs);
2023-09-08 19:50:43 +00:00
verify(mockTorService.enabled).called(1);
verifyNoMoreInteractions(mockTorService);
2023-09-08 16:45:07 +00:00
});
});
2022-08-26 08:11:35 +00:00
}