init method
connect to JsonRPC and get service status.
Implementation
Future<void> init() async {
// crypto
if (!LyraCrypto.isPrivateKeyValid(prvKey)) {
throw ('Not valid private key.');
}
accountId = LyraCrypto.prvToPub(prvKey);
nodeAddress ??= 'wss://$network.lyra.live/api/v1/socket';
// websocket
var ws = WebSocketChannel.connect(Uri.parse(nodeAddress!));
client = Peer(ws.cast<String>());
// The client won't subscribe to the input stream until you call `listen`.
// The returned Future won't complete until the connection is closed.
unawaited(client!.listen());
client!.registerMethod('Notify', (Parameters news) {
print('Got news from Lyra: ' + news.asList[1].toString());
});
client!.registerMethod('Sign', (Parameters req) {
print('Got signning callback.');
print('Signing ' + req.asList[1].toString());
try {
var signature = LyraCrypto.sign(req.asList[1].toString(), prvKey);
print('signature is: ' + signature);
return ['p1393', signature];
} catch (e) {
return ['error', e.toString()];
}
});
try {
var status = await client!.sendRequest('Status', ['2.2.0.0', network]);
print(status.toString());
} on RpcException catch (error) {
print('RPC error ${error.code}: ${error.message}');
}
}