-
Notifications
You must be signed in to change notification settings - Fork 8
/
ur_decoder.dart
174 lines (162 loc) · 6.95 KB
/
ur_decoder.dart
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
import 'dart:ffi';
import 'package:ffi/ffi.dart';
import 'package:ur_registry_flutter/native_object.dart';
import 'package:ur_registry_flutter/registries/cardano/cardano_signature.dart';
import 'package:ur_registry_flutter/registries/crypto_account.dart';
import 'package:ur_registry_flutter/registries/crypto_hd_key.dart';
import 'package:ur_registry_flutter/registries/crypto_psbt.dart';
import 'package:ur_registry_flutter/registries/ethereum/eth_sign_request.dart';
import 'package:ur_registry_flutter/registries/ethereum/eth_signature.dart';
import 'package:ur_registry_flutter/registries/extend/crypto_multi_accounts.dart';
import 'package:ur_registry_flutter/registries/solana/sol_signature.dart';
import 'package:ur_registry_flutter/response.dart';
import 'registries/solana/sol_sign_request.dart';
const nativePrefix = "ur_decoder";
typedef NativeNew = Pointer<Response> Function();
typedef NativeReceive = Pointer<Response> Function(
Pointer<Void>, Pointer<Utf8>);
typedef NativeResult = Pointer<Response> Function(Pointer<Void>);
typedef NativeIsComplete = Pointer<Response> Function(Pointer<Void>);
typedef NativeResolve = Pointer<Response> Function(
Pointer<Void>, Pointer<Utf8>);
enum SupportedType {
cryptoHDKey,
cryptoAccount,
cryptoPSBT,
cryptoMultiAccounts,
// sol
solSignRequest,
solSignature,
// eth
ethSignRequest,
ethSignature,
// cardano
cardanoUTXO,
cardanoSignRequest,
cardanoSignature,
cardanoCertKey,
cardanoSignDataRequest,
cardanoSignDataSignature,
cardanoCatalystVotingRegistration,
cardanoCatalystVotingRegistrationSignature,
}
const _cryptoHDKey = 'crypto-hdkey';
const _cryptoAccount = 'crypto-account';
const _cryptoPSBT = 'crypto-psbt';
const _cryptoMultiAccounts = 'crypto-multi-accounts';
const _solSignRequest = 'sol-sign-request';
const _solSignature = 'sol-signature';
const _ethSignRequest = 'eth-sign-request';
const _ethSignature = 'eth-signature';
const _cardanoUTXO = 'cardano-utxo';
const _cardanoSignRequest = 'cardano-sign-request';
const _cardanoSignature = 'cardano-signature';
const _cardanoCertKey = 'cardano-cert-key';
const _cardanoSignDataRequest = 'cardano-sign-data-request';
const _cardanoSignDataSignature = 'cardano-sign-data-signature';
const _cardanoCatalystVotingRegistration = 'cardano-catalyst-voting-registration';
const _cardanoCatalystVotingRegistrationSignature = 'cardano-catalyst-voting-registration-signature';
class URDecoder extends NativeObject {
late NativeNew nativeNew =
lib.lookup<NativeFunction<NativeNew>>("${nativePrefix}_new").asFunction();
late NativeReceive nativeReceive = lib
.lookup<NativeFunction<NativeReceive>>("${nativePrefix}_receive")
.asFunction();
late NativeIsComplete nativeIsComplete = lib
.lookup<NativeFunction<NativeIsComplete>>("${nativePrefix}_is_complete")
.asFunction();
late NativeResult nativeResult = lib
.lookup<NativeFunction<NativeResult>>("${nativePrefix}_result")
.asFunction();
late NativeResolve nativeResolve = lib
.lookup<NativeFunction<NativeResolve>>("${nativePrefix}_resolve")
.asFunction();
URDecoder() : super() {
final response = nativeNew().ref;
nativeObject = response.getObject();
}
void receive(String ur) {
final response = nativeReceive(nativeObject, ur.toNativeUtf8()).ref;
response.throwIfPresent();
}
bool isComplete() {
final response = nativeIsComplete(nativeObject).ref;
return response.getBoolean();
}
String result() {
final response = nativeResult(nativeObject).ref;
return response.getString();
}
NativeObject resolve(SupportedType type) {
switch (type) {
case SupportedType.cryptoHDKey:
final response =
nativeResolve(nativeObject, _cryptoHDKey.toNativeUtf8()).ref;
return CryptoHDKey(response.getObject());
case SupportedType.cryptoAccount:
final response =
nativeResolve(nativeObject, _cryptoAccount.toNativeUtf8()).ref;
return CryptoAccount(response.getObject());
case SupportedType.cryptoPSBT:
final response = nativeResolve(nativeObject, _cryptoPSBT.toNativeUtf8()).ref;
return CryptoPSBT(response.getObject());
case SupportedType.cryptoMultiAccounts:
final response =
nativeResolve(nativeObject, _cryptoMultiAccounts.toNativeUtf8()).ref;
return CryptoMultiAccounts(response.getObject());
// sol
case SupportedType.solSignRequest:
final response =
nativeResolve(nativeObject, _solSignRequest.toNativeUtf8()).ref;
return SolSignRequest(response.getObject());
case SupportedType.solSignature:
final response =
nativeResolve(nativeObject, _solSignature.toNativeUtf8()).ref;
return SolSignature(response.getObject());
// eth
case SupportedType.ethSignRequest:
final response =
nativeResolve(nativeObject, _ethSignRequest.toNativeUtf8()).ref;
return EthSignRequest(response.getObject());
case SupportedType.ethSignature:
final response =
nativeResolve(nativeObject, _ethSignature.toNativeUtf8()).ref;
return EthSignature(response.getObject());
// cardano
// case SupportedType.cardanoUTXO:
// final response =
// nativeResolve(nativeObject, _cardanoUTXO.toNativeUtf8()).ref;
// return CryptoAccount(response.getObject());
// case SupportedType.cardanoSignRequest:
// final response =
// nativeResolve(nativeObject, _cardanoSignRequest.toNativeUtf8()).ref;
// return CryptoAccount(response.getObject());
case SupportedType.cardanoSignature:
final response =
nativeResolve(nativeObject, _cardanoSignature.toNativeUtf8()).ref;
return CardanoSignature(response.getObject());
// case SupportedType.cardanoCertKey:
// final response =
// nativeResolve(nativeObject, _cardanoCertKey.toNativeUtf8()).ref;
// return CryptoAccount(response.getObject());
// case SupportedType.cardanoSignDataRequest:
// final response =
// nativeResolve(nativeObject, _cardanoSignDataRequest.toNativeUtf8()).ref;
// return CryptoAccount(response.getObject());
// case SupportedType.cardanoSignDataSignature:
// final response =
// nativeResolve(nativeObject, _cardanoSignDataSignature.toNativeUtf8()).ref;
// return CryptoAccount(response.getObject());
// case SupportedType.cardanoCatalystVotingRegistration:
// final response =
// nativeResolve(nativeObject, _cardanoCatalystVotingRegistration.toNativeUtf8()).ref;
// return CryptoAccount(response.getObject());
// case SupportedType.cardanoCatalystVotingRegistrationSignature:
// final response =
// nativeResolve(nativeObject, _cardanoCatalystVotingRegistrationSignature.toNativeUtf8()).ref;
// return CryptoAccount(response.getObject());
default:
throw Exception("type $type is not supported");
}
}
}