| 1 | import "dart:convert"; |
| 2 | |
| 3 | import "package:cw_core/hardware/hardware_wallet_service.dart"; |
| 4 | import "package:trezor_flutter/trezor_flutter.dart"; |
| 5 | |
| 6 | class MoneroTrezorService extends HardwareWalletService { |
| 7 | MoneroTrezorService(this.client); |
| 8 | |
| 9 | final TrezorClient client; |
| 10 | } |
| 11 | |
| 12 | class MoneroTrezorWatchCredentials { |
| 13 | const MoneroTrezorWatchCredentials(this.watchKey, this.address); |
| 14 | |
| 15 | final String watchKey; |
| 16 | final String address; |
| 17 | } |
| 18 | |
| 19 | class Trezor { |
| 20 | Trezor(this.service); |
| 21 | |
| 22 | final MoneroTrezorService service; |
| 23 | |
| 24 | String? _sessionPassphrase; |
| 25 | |
| 26 | Future<void> newPassphraseSession(String? passphrase) async { |
| 27 | if (passphrase == null) return; |
| 28 | |
| 29 | if (_sessionPassphrase == passphrase) return; |
| 30 | _sessionPassphrase = passphrase; |
| 31 | return service.client.createChannel(passphrase: _sessionPassphrase); |
| 32 | } |
| 33 | |
| 34 | Future<MoneroTrezorWatchCredentials> getWatchCredentials() async { |
| 35 | final credentials = await TrezorMonero(service.client).getWatchCredentials(); |
| 36 | |
| 37 | return MoneroTrezorWatchCredentials(credentials.$1, credentials.$2); |
| 38 | } |
| 39 | |
| 40 | Future<String> keyImageSync(String tdis) async { |
| 41 | final tdisMap = jsonDecode(tdis) as Map<String, dynamic>; |
| 42 | final tdisList = tdisMap["tdis"] as List<dynamic>; |
| 43 | |
| 44 | final txIds = <MoneroKeyImageTxData>[]; |
| 45 | for (final tdi in tdisList) { |
| 46 | txIds.add( |
| 47 | MoneroKeyImageTxData( |
| 48 | outKey: tdi["out_key"] as String, |
| 49 | txPubKey: tdi["tx_pub_key"] as String, |
| 50 | internalOutputIndex: tdi["internal_output_index"] as int, |
| 51 | subAddrMajor: tdi["sub_addr_major"] as int, |
| 52 | subAddrMinor: tdi["sub_addr_minor"] as int, |
| 53 | additionalTxPubKeys: |
| 54 | (tdi["additional_tx_pub_keys"] as List?)?.map((e) => e as String).toList() ?? [], |
| 55 | ), |
| 56 | ); |
| 57 | } |
| 58 | final keyImages = await TrezorMonero(service.client).syncKeyImages(txIds); |
| 59 | |
| 60 | return jsonEncode(keyImages.toMap()); |
| 61 | } |
| 62 | |
| 63 | Future<String> signTransaction(String json) => |
| 64 | TrezorMonero(service.client).signTransaction(jsonDecode(json) as Map<String, dynamic>); |
| 65 | } |