123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535 |
- import 'dart:convert';
- import 'package:flutter/material.dart';
- import 'package:polkawallet_sdk/api/types/addressIconData.dart';
- import 'package:polkawallet_sdk/ethers/apiEthers.dart';
- import 'package:polkawallet_sdk/polkawallet_sdk.dart';
- import 'package:polkawallet_sdk/storage/keyringEVM.dart';
- import 'package:polkawallet_sdk/storage/types/ethWalletData.dart';
- import 'package:polkawallet_sdk_example/pages/keyring.dart';
- class EthWithJSPage extends StatefulWidget {
- EthWithJSPage(this.sdk, this.keyring, this.showResult);
- final WalletSDK sdk;
- final KeyringEVM keyring;
- final Function(BuildContext, String, String) showResult;
- static const String route = '/keyring/eth/js';
- @override
- _EthWithJSPageState createState() => _EthWithJSPageState();
- }
- class _EthWithJSPageState extends State<EthWithJSPage> {
- final String _testJson = '''{
- "crypto": {"cipher": "aes-128-ctr", "cipherparams": {"iv": "3be45c336ce8cd771061e3b5ec9460ec"}, "ciphertext": "b4e181f629e480d4073edfe6f0ad7bc8c201ac3975eb0232150e4550f59af1c0", "kdf": "scrypt", "kdfparams": {"dklen": 32, "n": 8192, "r": 8, "p": 1, "salt": "4771ff99bf695bab6d292330678a800f8acb1831134678df45b6911fab94f38c"}, "mac": "ed5cec3eed91ab251650657cff87c693a2672413658ff7da4c2f38598f21984a"},
- "id": "3228f3c9-dfee-4e65-9569-5c8a7afc3921",
- "version": 3,
- "address": "0x4b248f45dfbea07de1bbd69180f6695b78250caf"
- }''';
- final String _testPass = 'a123456';
- final _testMsg = 'hello message for signing';
- EthWalletData? _testAcc;
- bool _submitting = false;
- Future<void> _generateMnemonic() async {
- setState(() {
- _submitting = true;
- });
- final AddressIconDataWithMnemonic seed =
- await widget.sdk.api.eth.keyring.generateMnemonic();
- widget.showResult(context, 'generateMnemonic', seed.mnemonic!);
- setState(() {
- _submitting = false;
- });
- }
- Future<void> _getAccountList() async {
- final List<EthWalletData> 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.eth.keyring
- .getDecryptedSeed(widget.keyring, _testPass);
- // await widget.sdk.evm.getDecryptedSeed(widget.keyring, 'a654321');
- widget.showResult(
- context,
- 'getDecryptedSeeds',
- 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.eth.keyring.importAccount(
- keyType: EVMKeyType.mnemonic,
- key:
- 'wing know chapter eight shed lens mandate lake twenty useless bless glory',
- name: 'testName01',
- password: _testPass,
- );
- print(json);
- final acc = await widget.sdk.api.eth.keyring.addAccount(
- widget.keyring,
- keyType: EVMKeyType.mnemonic,
- acc: json!,
- password: _testPass,
- );
- widget.showResult(
- context,
- 'importFromMnemonic',
- JsonEncoder.withIndent(' ').convert(acc.toJson()),
- );
- setState(() {
- _submitting = false;
- _testAcc = acc;
- });
- }
- Future<void> _importFromPrivateKey() async {
- setState(() {
- _submitting = true;
- });
- final json = await widget.sdk.api.eth.keyring.importAccount(
- keyType: EVMKeyType.privateKey,
- key: '0x2defc5ff7c700eb3a39a702e9b38534e8ea3419b93b1836dc6ccc891ce359290',
- name: 'testName02',
- password: _testPass,
- );
- print(json);
- final acc = await widget.sdk.api.eth.keyring.addAccount(
- widget.keyring,
- keyType: EVMKeyType.privateKey,
- acc: json!,
- password: _testPass,
- );
- widget.showResult(
- context,
- 'importFromPrivateKey',
- JsonEncoder.withIndent(' ').convert(acc.toJson()),
- );
- setState(() {
- _submitting = false;
- _testAcc = acc;
- });
- }
- Future<void> _importFromKeystore() async {
- setState(() {
- _submitting = true;
- });
- final json = await widget.sdk.api.eth.keyring.importAccount(
- keyType: EVMKeyType.keystore,
- key: _testJson,
- name: 'testName03',
- password: _testPass,
- );
- final acc = await widget.sdk.api.eth.keyring.addAccount(
- widget.keyring,
- keyType: EVMKeyType.keystore,
- 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.eth.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.eth.keyring
- .checkPassword(_testAcc?.address ?? '', _testPass);
- // await widget.sdk.evm.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.eth.keyring
- .changePassword(widget.keyring, _testPass, 'a654321');
- // .changePassword(widget.keyring, 'a654321', _testPass);
- widget.showResult(
- context,
- 'changePassword',
- res == null ? 'null' : JsonEncoder.withIndent(' ').convert(res.toJson()),
- );
- setState(() {
- _submitting = false;
- _testAcc = res;
- });
- }
- 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.eth.keyring.changeName(widget.keyring, 'newName');
- widget.showResult(
- context,
- 'changeName', JsonEncoder.withIndent(' ').convert(res.toJson()),
- );
- setState(() {
- _submitting = false;
- });
- }
- Future<void> _signMessage() async {
- if (_testAcc == null) {
- widget.showResult(
- context,
- 'signMessage',
- 'should import keyPair to init test account.',
- );
- return;
- }
- setState(() {
- _submitting = true;
- });
- final res = await widget.sdk.api.eth.keyring
- .signMessage(_testPass, _testMsg, _testAcc?.address ?? '');
- widget.showResult(
- context,
- 'signMessage',
- res == null ? 'null' : JsonEncoder.withIndent(' ').convert(res.toJson()),
- );
- setState(() {
- _submitting = false;
- });
- }
- Future<void> _signatureVerify() async {
- if (_testAcc == null) {
- widget.showResult(
- context,
- 'signMessage',
- 'should import keyPair to init test account.',
- );
- return;
- }
- setState(() {
- _submitting = true;
- });
- final res = await widget.sdk.api.eth.keyring.signatureVerify(_testMsg,
- '0xded97a9a203f794a042bb71107523d685258a27f9df00634d782ba0bdb70be3808bff9ac1ef9bbfff474ef69fc2b613b6ae7b1956a83e2286136f8814993e8491c');
- widget.showResult(
- context,
- 'signatureVerify', JsonEncoder.withIndent(' ').convert(res),
- );
- 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: [
- // RaisedButton(
- // child: Text('Polkadot: 0'),
- // color:
- // _ss58 == 0 ? Theme.of(context).primaryColor : null,
- // onPressed: () => _setSS58(0),
- // ),
- // RaisedButton(
- // child: Text('Kusama: 2'),
- // color:
- // _ss58 == 2 ? Theme.of(context).primaryColor : null,
- // onPressed: () => _setSS58(2),
- // ),
- // RaisedButton(
- // child: Text('Substrate: 42'),
- // color:
- // _ss58 == 42 ? Theme.of(context).primaryColor : null,
- // onPressed: () => _setSS58(42),
- // )
- // ],
- // )
- // ],
- // ),
- // ),
- // Divider(),
- ListTile(
- title: Text('getAccountList'),
- subtitle: Text('''
- sdk.api.eth.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.eth.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('importFromPrivateKey'),
- subtitle: Text('''
- sdk.api.eth.keyring.importAccount(
- keyType: KeyType.privateKey,
- key: 'Alice',
- name: 'testName02',
- password: 'a123456',
- )'''),
- trailing: SubmitButton(
- submitting: _submitting,
- call: _importFromPrivateKey,
- ),
- ),
- Divider(),
- ListTile(
- title: Text('importFromKeystore'),
- subtitle: Text('''
- sdk.api.eth.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.eth.keyring.getDecryptedSeed(
- '${_testAcc?.toString()}',
- 'a123456',
- )'''),
- trailing: SubmitButton(
- submitting: _submitting,
- call: _getDecryptedSeed,
- ),
- ),
- Divider(),
- ListTile(
- title: Text('deleteAccount'),
- subtitle: Text('''
- sdk.api.eth.keyring.deleteAccount'''),
- trailing: SubmitButton(
- submitting: _submitting,
- call: _deleteAccount,
- ),
- ),
- Divider(),
- ListTile(
- title: Text('checkPassword'),
- subtitle: Text('''
- sdk.api.eth.keyring.checkPassword(
- '${_testAcc?.toString()}',
- 'a123456',
- )'''),
- trailing: SubmitButton(
- submitting: _submitting,
- call: _checkPassword,
- ),
- ),
- Divider(),
- ListTile(
- title: Text('changePassword'),
- subtitle: Text('''
- sdk.api.eth.keyring.changePassword(
- '${_testAcc?.toString()}',
- 'a123456',
- 'a654321',
- )'''),
- trailing: SubmitButton(
- submitting: _submitting,
- call: _changePassword,
- ),
- ),
- Divider(),
- ListTile(
- title: Text('changeName'),
- subtitle: Text('''
- sdk.api.eth.keyring.changeName(
- '${_testAcc?.toString()}',
- 'newName',
- )'''),
- trailing: SubmitButton(
- submitting: _submitting,
- call: _changeName,
- ),
- ),
- Divider(),
- ListTile(
- title: Text('signMessage'),
- subtitle: Text('''
- sdk.api.eth.keyring.signMessage(
- '$_testPass',
- '$_testMsg',
- '${_testAcc?.toString()}'
- )'''),
- trailing: SubmitButton(
- submitting: _submitting,
- call: _signMessage,
- ),
- ),
- Divider(),
- ListTile(
- title: Text('signatureVerify'),
- subtitle: Text('''
- sdk.api.eth.keyring.signatureVerify(
- '$_testMsg',
- 'test signature',
- )'''),
- trailing: SubmitButton(
- submitting: _submitting,
- call: _signatureVerify,
- ),
- ),
- Divider(),
- ],
- ),
- ), // This trailing comma makes auto-formatting nicer for build methods.
- );
- }
- }
|