123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535 |
- import 'dart:convert';
- import 'package:flutter/cupertino.dart';
- import 'package:flutter/material.dart';
- import 'package:polkawallet_sdk/api/apiKeyring.dart';
- import 'package:polkawallet_sdk/api/types/addressIconData.dart';
- import 'package:polkawallet_sdk/polkawallet_sdk.dart';
- import 'package:polkawallet_sdk/storage/keyring.dart';
- import 'package:polkawallet_sdk/storage/types/keyPairData.dart';
- class KeyringPage extends StatefulWidget {
- KeyringPage(this.sdk, this.keyring, this.showResult);
- final WalletSDK sdk;
- final Keyring keyring;
- final Function(BuildContext, String, String) showResult;
- static const String route = '/keyring';
- @override
- _KeyringPageState createState() => _KeyringPageState();
- }
- class _KeyringPageState extends State<KeyringPage> {
- final String _testJson = '''{
- "pubKey":"0xa2d1d33cc490d34ccc6938f8b30430428da815a85bf5927adc85d9e27cbbfc1a",
- "address":"14gV68QsGAEUGkcuV5JA1hx2ZFTuKJthMFfnkDyLMZyn8nnb",
- "encoded":"G3BHvs9tVTSf1Qe02bcOGpj7vjLdgqyS+/s0/J3EfRMAgAAAAQAAAAgAAADpWTEOs5/06DmEZaeuoExpf9+y1xcUhIzmEr6dUxyl67VQRX2KNGVmTqq05/sEIUDPVeOqqLbjBEPaNRoC0lZTQlKM5u38lX4PzKivGHM9ZJkvtQxf7RAndN/vgfIX4X76gX60bqrUY8Qr2ZswtuPTeGVKQOD7y0GtoPOcR2RzFg6rs44NuugTR0UwA8HWTDkh0c/KOnUc1FJDb4rV",
- "encoding":{"content":["pkcs8","sr25519"],"type":["scrypt","xsalsa20-poly1305"],"version":"3"},
- "meta": {
- "name": "testName-3",
- "whenCreated": 1598270113026,
- "whenEdited": 1598270113026
- }}''';
- final String _testPass = 'a123456';
- KeyPairData? _testAcc;
- int _ss58 = 0;
- bool _submitting = false;
- void _setSS58(int ss58) {
- final res = widget.keyring.setSS58(ss58);
- if (res != null) {
- setState(() {
- _ss58 = res;
- });
- }
- }
- Future<void> _generateMnemonic() async {
- setState(() {
- _submitting = true;
- });
- final AddressIconDataWithMnemonic seed =
- await widget.sdk.api.keyring.generateMnemonic(_ss58);
- widget.showResult(context, 'generateMnemonic', seed.mnemonic!);
- setState(() {
- _submitting = false;
- });
- }
- Future<void> _getAccountList() async {
- final List<KeyPairData> ls = widget.keyring.keyPairs;
- widget.showResult(
- context,
- 'getAccountList',
- JsonEncoder.withIndent(' ')
- .convert(ls.map((e) => '${e.name}: ${e.address}').toList()),
- );
- }
- Future<void> _getDecryptedSeed() async {
- if (_testAcc == null) {
- widget.showResult(
- context,
- 'getDecryptedSeeds',
- 'should import keyPair to init test account.',
- );
- return;
- }
- setState(() {
- _submitting = true;
- });
- final seed = await widget.sdk.api.keyring
- .getDecryptedSeed(widget.keyring, _testPass);
- // await widget.sdk.api.keyring.getDecryptedSeed(_testAcc, 'a654321');
- widget.showResult(
- context,
- 'getAccountList',
- seed == null
- ? 'null'
- : JsonEncoder.withIndent(' ').convert({
- 'address': _testAcc?.address,
- 'type': seed.type,
- 'seed': seed.seed,
- 'error': seed.error,
- }),
- );
- setState(() {
- _submitting = false;
- });
- }
- Future<void> _importFromMnemonic() async {
- setState(() {
- _submitting = true;
- });
- final json = await widget.sdk.api.keyring.importAccount(
- widget.keyring,
- keyType: KeyType.mnemonic,
- key:
- 'wing know chapter eight shed lens mandate lake twenty useless bless glory',
- name: 'testName01',
- password: _testPass,
- );
- final acc = await widget.sdk.api.keyring.addAccount(
- widget.keyring,
- keyType: KeyType.mnemonic,
- acc: json!,
- password: _testPass,
- );
- widget.showResult(
- context,
- 'importFromMnemonic',
- JsonEncoder.withIndent(' ').convert(acc.toJson()),
- );
- setState(() {
- _submitting = false;
- _testAcc = acc;
- });
- }
- Future<void> _importFromRawSeed() async {
- setState(() {
- _submitting = true;
- });
- final json = await widget.sdk.api.keyring.importAccount(
- widget.keyring,
- keyType: KeyType.rawSeed,
- key: 'Alice',
- name: 'testName02',
- password: _testPass,
- );
- final acc = await widget.sdk.api.keyring.addAccount(
- widget.keyring,
- keyType: KeyType.mnemonic,
- acc: json!,
- password: _testPass,
- );
- widget.showResult(
- context,
- 'importFromRawSeed',
- JsonEncoder.withIndent(' ').convert(acc.toJson()),
- );
- setState(() {
- _submitting = false;
- _testAcc = acc;
- });
- }
- Future<void> _importFromKeystore() async {
- setState(() {
- _submitting = true;
- });
- final json = await widget.sdk.api.keyring.importAccount(
- widget.keyring,
- keyType: KeyType.keystore,
- key: _testJson,
- name: 'testName03',
- password: _testPass,
- );
- final acc = await widget.sdk.api.keyring.addAccount(
- widget.keyring,
- keyType: KeyType.mnemonic,
- acc: json!,
- password: _testPass,
- );
- widget.showResult(
- context,
- 'importFromKeystore',
- JsonEncoder.withIndent(' ').convert(acc.toJson()),
- );
- setState(() {
- _submitting = false;
- _testAcc = acc;
- });
- }
- Future<void> _deleteAccount() async {
- if (_testAcc == null) {
- widget.showResult(
- context,
- 'deleteAccount',
- 'should import keyPair to init test account.',
- );
- return;
- }
- setState(() {
- _submitting = true;
- });
- await widget.sdk.api.keyring.deleteAccount(widget.keyring, _testAcc!);
- widget.showResult(
- context,
- 'deleteAccount',
- 'ok',
- );
- setState(() {
- _submitting = false;
- });
- }
- Future<void> _checkPassword() async {
- if (_testAcc == null) {
- widget.showResult(
- context,
- 'checkPassword',
- 'should import keyPair to init test account.',
- );
- return;
- }
- setState(() {
- _submitting = true;
- });
- final bool passed =
- await widget.sdk.api.keyring.checkPassword(_testAcc!, _testPass);
- // await widget.sdk.api.keyring.checkPassword(_testAcc, 'a654321');
- widget.showResult(
- context,
- 'checkPassword',
- passed.toString(),
- );
- setState(() {
- _submitting = false;
- });
- }
- Future<void> _changePassword() async {
- if (_testAcc == null) {
- widget.showResult(
- context,
- 'changePassword',
- 'should import keyPair to init test account.',
- );
- return;
- }
- setState(() {
- _submitting = true;
- });
- final res = await widget.sdk.api.keyring
- // .changePassword(widget.keyring, _testAcc, _testPass, 'a654321');
- .changePassword(widget.keyring, 'a654321', _testPass);
- widget.showResult(
- context,
- 'changePassword',
- res == null ? 'null' : JsonEncoder.withIndent(' ').convert(res.toJson()),
- );
- setState(() {
- _submitting = false;
- });
- }
- Future<void> _changeName() async {
- if (_testAcc == null) {
- widget.showResult(
- context,
- 'changeName',
- 'should import keyPair to init test account.',
- );
- return;
- }
- setState(() {
- _submitting = true;
- });
- final res =
- await widget.sdk.api.keyring.changeName(widget.keyring, 'newName');
- widget.showResult(
- context,
- 'changeName', JsonEncoder.withIndent(' ').convert(res.toJson()),
- );
- setState(() {
- _submitting = false;
- });
- }
- Future<void> _checkDerivePath() async {
- setState(() {
- _submitting = true;
- });
- final err = await widget.sdk.api.keyring
- .checkDerivePath('Alice', '///', CryptoType.sr25519);
- widget.showResult(
- context,
- 'checkDerivePath',
- 'error: $err',
- );
- setState(() {
- _submitting = false;
- });
- }
- @override
- void initState() {
- super.initState();
- WidgetsBinding.instance.addPostFrameCallback((_) {
- if (widget.keyring.keyPairs.length > 0) {
- setState(() {
- _testAcc = widget.keyring.keyPairs[0];
- });
- }
- });
- }
- @override
- Widget build(BuildContext context) {
- return Scaffold(
- appBar: AppBar(
- title: Text('keyring API'),
- ),
- body: SafeArea(
- child: ListView(
- children: [
- Padding(
- padding: EdgeInsets.all(16),
- child: Column(
- crossAxisAlignment: CrossAxisAlignment.start,
- children: [
- Text('address ss58Format: ${widget.keyring.ss58}'),
- Row(
- mainAxisAlignment: MainAxisAlignment.spaceBetween,
- children: [
- CupertinoButton(
- child: Text('Polkadot: 0'),
- color:
- _ss58 == 0 ? Theme.of(context).primaryColor : null,
- onPressed: () => _setSS58(0),
- ),
- CupertinoButton(
- child: Text('Kusama: 2'),
- color:
- _ss58 == 2 ? Theme.of(context).primaryColor : null,
- onPressed: () => _setSS58(2),
- ),
- CupertinoButton(
- child: Text('Substrate: 42'),
- color:
- _ss58 == 42 ? Theme.of(context).primaryColor : null,
- onPressed: () => _setSS58(42),
- )
- ],
- )
- ],
- ),
- ),
- Divider(),
- ListTile(
- title: Text('getAccountList'),
- subtitle: Text('''
- sdk.api.keyring.accountList'''),
- trailing: SubmitButton(
- submitting: _submitting,
- call: _getAccountList,
- ),
- ),
- Divider(),
- ListTile(
- title: Text('generateMnemonic'),
- subtitle: Text('sdk.api.keyring.generateMnemonic()'),
- trailing: SubmitButton(
- submitting: _submitting,
- call: _generateMnemonic,
- ),
- ),
- Divider(),
- ListTile(
- title: Text('importFromMnemonic'),
- subtitle: Text('''
- sdk.api.keyring.importAccount(
- keyType: KeyType.mnemonic,
- key: 'wing know chapter eight shed lens mandate lake twenty useless bless glory',
- name: 'testName01',
- password: 'a123456',
- )'''),
- trailing: SubmitButton(
- submitting: _submitting,
- call: _importFromMnemonic,
- ),
- ),
- Divider(),
- ListTile(
- title: Text('importFromRawSeed'),
- subtitle: Text('''
- sdk.api.keyring.importAccount(
- keyType: KeyType.rawSeed,
- key: 'Alice',
- name: 'testName02',
- password: 'a123456',
- )'''),
- trailing: SubmitButton(
- submitting: _submitting,
- call: _importFromRawSeed,
- ),
- ),
- Divider(),
- ListTile(
- title: Text('importFromKeystore'),
- subtitle: Text('''
- sdk.api.keyring.importAccount(
- keyType: KeyType.keystore,
- key: '{xxx...xxx}',
- name: 'testName03',
- password: 'a123456',
- )'''),
- trailing: SubmitButton(
- submitting: _submitting,
- call: _importFromKeystore,
- ),
- ),
- Divider(),
- ListTile(
- title: Text('getDecryptedSeed'),
- subtitle: Text('''
- sdk.api.keyring.getDecryptedSeed(
- '${_testAcc?.toString()}',
- 'a123456',
- )'''),
- trailing: SubmitButton(
- submitting: _submitting,
- call: _getDecryptedSeed,
- ),
- ),
- Divider(),
- ListTile(
- title: Text('deleteAccount'),
- subtitle: Text('''
- sdk.api.keyring.deleteAccount'''),
- trailing: SubmitButton(
- submitting: _submitting,
- call: _deleteAccount,
- ),
- ),
- Divider(),
- ListTile(
- title: Text('checkPassword'),
- subtitle: Text('''
- sdk.api.keyring.checkPassword(
- '${_testAcc?.toString()}',
- 'a123456',
- )'''),
- trailing: SubmitButton(
- submitting: _submitting,
- call: _checkPassword,
- ),
- ),
- Divider(),
- ListTile(
- title: Text('changePassword'),
- subtitle: Text('''
- sdk.api.keyring.changePassword(
- '${_testAcc?.toString()}',
- 'a123456',
- 'a654321',
- )'''),
- trailing: SubmitButton(
- submitting: _submitting,
- call: _changePassword,
- ),
- ),
- Divider(),
- ListTile(
- title: Text('changeName'),
- subtitle: Text('''
- sdk.api.keyring.changeName(
- '${_testAcc?.toString()}',
- 'newName',
- )'''),
- trailing: SubmitButton(
- submitting: _submitting,
- call: _changeName,
- ),
- ),
- Divider(),
- ListTile(
- title: Text('checkDerivePath'),
- subtitle: Text('''
- sdk.api.keyring.checkDerivePath(
- 'Alice',
- '///',
- CryptoType.sr25519,
- )'''),
- trailing: SubmitButton(
- submitting: _submitting,
- call: _checkDerivePath,
- ),
- ),
- Divider(),
- ],
- ),
- ), // This trailing comma makes auto-formatting nicer for build methods.
- );
- }
- }
- class SubmitButton extends StatelessWidget {
- SubmitButton(
- {required this.call, required this.submitting, this.needConnect = false});
- final bool submitting;
- final bool needConnect;
- final Function call;
- @override
- Widget build(BuildContext context) {
- return needConnect
- ? Column(
- children: [
- Icon(
- Icons.play_circle_outline,
- color: Theme.of(context).disabledColor,
- ),
- Text('Connection\nRequired', style: TextStyle(fontSize: 10))
- ],
- )
- : IconButton(
- color: submitting
- ? Theme.of(context).disabledColor
- : Theme.of(context).primaryColor,
- icon: submitting
- ? Icon(Icons.refresh)
- : Icon(Icons.play_circle_outline),
- onPressed: () => call(),
- );
- }
- }
|