-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathcredentials.dart
More file actions
104 lines (85 loc) · 2.67 KB
/
credentials.dart
File metadata and controls
104 lines (85 loc) · 2.67 KB
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
library webthree.internal.js.creds;
import 'dart:js_interop';
import 'dart:js_util';
import 'dart:typed_data';
import 'package:webthree/webthree.dart';
import '../../../crypto.dart';
import 'javascript.dart';
class BinanceWalletCredentials extends CredentialsWithKnownAddress
implements CustomTransactionSender {
@override
final EthereumAddress address;
final BinanceChainWallet bsc;
BinanceWalletCredentials(String hexAddress, this.bsc)
: address = EthereumAddress.fromHex(hexAddress);
@override
Future<MsgSignature> signToSignature(Uint8List payload,
{int? chainId, bool isEIP1559 = false}) {
throw UnsupportedError('Signing raw payloads is not supported on MetaMask');
}
@override
Future<Uint8List> signPersonalMessage(Uint8List payload, {int? chainId}) {
final paramsValue = jsify([
address.hex,
_bytesToData(payload),
]);
final responsePromise = bsc.request(RequestArguments(
method: 'eth_sign',
params: paramsValue as JSAny?,
));
return (responsePromise as JSPromise<JSString>)
.toDart
.then((JSString jsResult) => _responseToBytes(jsResult.toDart));
}
@override
Future<String> sendTransaction(Transaction transaction) {
final param = _TransactionParameters(
from: (transaction.from ?? address).hex,
to: transaction.to?.hex,
gasPrice: _bigIntToQuantity(transaction.gasPrice?.getInWei),
gas: _intToQuantity(transaction.maxGas),
value: _bigIntToQuantity(transaction.value?.getInWei),
data: _bytesToData(transaction.data),
);
final paramsValue = jsify([param]);
final responsePromise = bsc.request(RequestArguments(
method: 'eth_sendTransaction',
params: paramsValue as JSAny?,
));
return (responsePromise as JSPromise<JSString>)
.toDart
.then((JSString jsResult) => jsResult.toDart);
}
}
String? _bigIntToQuantity(BigInt? int) {
return int != null ? '0x${int.toRadixString(16)}' : null;
}
String? _intToQuantity(int? int) {
return int != null ? '0x${int.toRadixString(16)}' : null;
}
Uint8List _responseToBytes(dynamic response) {
return hexToBytes(response as String);
}
String? _bytesToData(Uint8List? data) {
return data != null
? bytesToHex(data, include0x: true, padToEvenLength: true)
: null;
}
@JS()
@anonymous
class _TransactionParameters {
external String? get gasPrice;
external String? get gas;
external String? get to;
external String get from;
external String? get value;
external String? get data;
external factory _TransactionParameters({
required String from,
String? gas,
String? gasPrice,
String? to,
String? value,
String? data,
});
}