@onekeyfe/hd-core 0.2.50-alpha.1 → 0.2.51

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (89) hide show
  1. package/dist/api/CheckBootloaderRelease.d.ts.map +1 -1
  2. package/dist/api/FirmwareUpdateV2.d.ts +1 -0
  3. package/dist/api/FirmwareUpdateV2.d.ts.map +1 -1
  4. package/dist/api/device/DeviceUpdateBootloader.d.ts +3 -0
  5. package/dist/api/device/DeviceUpdateBootloader.d.ts.map +1 -1
  6. package/dist/api/firmware/getBinary.d.ts +7 -1
  7. package/dist/api/firmware/getBinary.d.ts.map +1 -1
  8. package/dist/api/firmware/updateBootloader.d.ts +2 -1
  9. package/dist/api/firmware/updateBootloader.d.ts.map +1 -1
  10. package/dist/api/index.d.ts +2 -0
  11. package/dist/api/index.d.ts.map +1 -1
  12. package/dist/api/kaspa/KaspaGetAddress.d.ts +17 -0
  13. package/dist/api/kaspa/KaspaGetAddress.d.ts.map +1 -0
  14. package/dist/api/kaspa/KaspaSignTransaction.d.ts +19 -0
  15. package/dist/api/kaspa/KaspaSignTransaction.d.ts.map +1 -0
  16. package/dist/api/kaspa/helpers/BufferWriter.d.ts +28 -0
  17. package/dist/api/kaspa/helpers/BufferWriter.d.ts.map +1 -0
  18. package/dist/api/kaspa/helpers/HashWriter.d.ts +22 -0
  19. package/dist/api/kaspa/helpers/HashWriter.d.ts.map +1 -0
  20. package/dist/api/kaspa/helpers/SignatureType.d.ts +8 -0
  21. package/dist/api/kaspa/helpers/SignatureType.d.ts.map +1 -0
  22. package/dist/api/kaspa/helpers/TransferSerialize.d.ts +9 -0
  23. package/dist/api/kaspa/helpers/TransferSerialize.d.ts.map +1 -0
  24. package/dist/api/stellar/StellarSignTransaction.d.ts +1 -1
  25. package/dist/data-manager/DataManager.d.ts +1 -0
  26. package/dist/data-manager/DataManager.d.ts.map +1 -1
  27. package/dist/index.d.ts +78 -34
  28. package/dist/index.d.ts.map +1 -1
  29. package/dist/index.js +826 -218
  30. package/dist/inject.d.ts +0 -1
  31. package/dist/inject.d.ts.map +1 -1
  32. package/dist/types/api/checkBootloaderRelease.d.ts +4 -2
  33. package/dist/types/api/checkBootloaderRelease.d.ts.map +1 -1
  34. package/dist/types/api/export.d.ts +2 -0
  35. package/dist/types/api/export.d.ts.map +1 -1
  36. package/dist/types/api/firmwareUpdate.d.ts +1 -0
  37. package/dist/types/api/firmwareUpdate.d.ts.map +1 -1
  38. package/dist/types/api/index.d.ts +4 -1
  39. package/dist/types/api/index.d.ts.map +1 -1
  40. package/dist/types/api/init.d.ts +1 -2
  41. package/dist/types/api/init.d.ts.map +1 -1
  42. package/dist/types/api/kaspaGetAddress.d.ts +16 -0
  43. package/dist/types/api/kaspaGetAddress.d.ts.map +1 -0
  44. package/dist/types/api/kaspaSignTransaction.d.ts +35 -0
  45. package/dist/types/api/kaspaSignTransaction.d.ts.map +1 -0
  46. package/dist/types/api/suiGetAddress.d.ts +2 -2
  47. package/dist/types/api/suiGetAddress.d.ts.map +1 -1
  48. package/dist/types/settings.d.ts +4 -0
  49. package/dist/types/settings.d.ts.map +1 -1
  50. package/dist/utils/deviceFeaturesUtils.d.ts.map +1 -1
  51. package/dist/utils/index.d.ts +1 -1
  52. package/dist/utils/index.d.ts.map +1 -1
  53. package/dist/utils/patch.d.ts +1 -1
  54. package/dist/utils/patch.d.ts.map +1 -1
  55. package/package.json +4 -4
  56. package/src/api/CheckBootloaderRelease.ts +24 -10
  57. package/src/api/FirmwareUpdateV2.ts +7 -1
  58. package/src/api/device/DeviceUpdateBootloader.ts +19 -8
  59. package/src/api/firmware/getBinary.ts +15 -3
  60. package/src/api/firmware/updateBootloader.ts +27 -1
  61. package/src/api/index.ts +3 -0
  62. package/src/api/kaspa/KaspaGetAddress.ts +77 -0
  63. package/src/api/kaspa/KaspaSignTransaction.ts +157 -0
  64. package/src/api/kaspa/helpers/BufferWriter.ts +177 -0
  65. package/src/api/kaspa/helpers/HashWriter.ts +72 -0
  66. package/src/api/kaspa/helpers/SignatureType.ts +7 -0
  67. package/src/api/kaspa/helpers/TransferSerialize.ts +143 -0
  68. package/src/core/index.ts +2 -2
  69. package/src/data/messages/messages.json +126 -0
  70. package/src/data-manager/DataManager.ts +13 -1
  71. package/src/index.ts +0 -28
  72. package/src/inject.ts +193 -203
  73. package/src/types/api/checkBootloaderRelease.ts +5 -2
  74. package/src/types/api/export.ts +8 -0
  75. package/src/types/api/firmwareUpdate.ts +1 -0
  76. package/src/types/api/index.ts +9 -1
  77. package/src/types/api/init.ts +1 -5
  78. package/src/types/api/kaspaGetAddress.ts +25 -0
  79. package/src/types/api/kaspaSignTransaction.ts +43 -0
  80. package/src/types/api/suiGetAddress.ts +2 -2
  81. package/src/types/settings.ts +4 -0
  82. package/src/utils/deviceFeaturesUtils.ts +7 -1
  83. package/src/utils/index.ts +4 -1
  84. package/dist/lowLevelInject.d.ts +0 -20
  85. package/dist/lowLevelInject.d.ts.map +0 -1
  86. package/dist/topLevelInject.d.ts +0 -7
  87. package/dist/topLevelInject.d.ts.map +0 -1
  88. package/src/lowLevelInject.ts +0 -52
  89. package/src/topLevelInject.ts +0 -54
@@ -0,0 +1,143 @@
1
+ /* eslint-disable @typescript-eslint/no-non-null-assertion */
2
+ import {
3
+ KaspaSignTransactionParams,
4
+ KaspaSignInputParams,
5
+ KaspaSignOutputParams,
6
+ } from '../../../types';
7
+ import { SignatureType } from './SignatureType';
8
+ import { HashWriter } from './HashWriter';
9
+
10
+ export function zeroHash() {
11
+ return Buffer.alloc(32);
12
+ }
13
+
14
+ export function zeroSubnetworkID() {
15
+ return Buffer.alloc(20);
16
+ }
17
+
18
+ function isSighashAnyoneCanPay(sighashType: number) {
19
+ // eslint-disable-next-line no-bitwise
20
+ return (sighashType & SignatureType.SIGHASH_ANYONECANPAY) === SignatureType.SIGHASH_ANYONECANPAY;
21
+ }
22
+
23
+ function isSighashSingle(sighashType: number) {
24
+ // eslint-disable-next-line no-bitwise
25
+ return (sighashType & 31) === SignatureType.SIGHASH_SINGLE;
26
+ }
27
+
28
+ function isSighashNone(sighashType: number) {
29
+ // eslint-disable-next-line no-bitwise
30
+ return (sighashType & 31) === SignatureType.SIGHASH_NONE;
31
+ }
32
+
33
+ function hashOutpoint(hashWriter: HashWriter, input: KaspaSignInputParams) {
34
+ hashWriter.writeHash(Buffer.from(input.prevTxId, 'hex'));
35
+ hashWriter.writeUInt32LE(input.outputIndex);
36
+ }
37
+
38
+ function getPreviousOutputsHash(
39
+ transaction: KaspaSignTransactionParams,
40
+ sighashType: SignatureType
41
+ ) {
42
+ if (isSighashAnyoneCanPay(sighashType)) {
43
+ return zeroHash();
44
+ }
45
+
46
+ const hashWriter = new HashWriter();
47
+ transaction.inputs.forEach(input => hashOutpoint(hashWriter, input));
48
+ return hashWriter.finalize();
49
+ }
50
+
51
+ function getSequencesHash(transaction: KaspaSignTransactionParams, sighashType: SignatureType) {
52
+ if (
53
+ isSighashSingle(sighashType) ||
54
+ isSighashAnyoneCanPay(sighashType) ||
55
+ isSighashNone(sighashType)
56
+ ) {
57
+ return zeroHash();
58
+ }
59
+
60
+ const hashWriter = new HashWriter();
61
+ transaction.inputs.forEach(input => hashWriter.writeUInt64LE(input.sequenceNumber));
62
+ return hashWriter.finalize();
63
+ }
64
+
65
+ function getSigOpCountsHash(transaction: KaspaSignTransactionParams, sighashType: SignatureType) {
66
+ if (isSighashAnyoneCanPay(sighashType)) {
67
+ return zeroHash();
68
+ }
69
+
70
+ const hashWriter = new HashWriter();
71
+ transaction.inputs.forEach(input => hashWriter.writeUInt8(input.sigOpCount!));
72
+ return hashWriter.finalize();
73
+ }
74
+
75
+ function hashTxOut(hashWriter: HashWriter, output: KaspaSignOutputParams) {
76
+ hashWriter.writeUInt64LE(output.satoshis);
77
+ hashWriter.writeUInt16LE(0); // TODO: USE REAL SCRIPT VERSION
78
+ hashWriter.writeVarBytes(Buffer.from(output.script, 'hex'));
79
+ }
80
+
81
+ function getOutputsHash(
82
+ transaction: KaspaSignTransactionParams,
83
+ inputNumber: number,
84
+ sighashType: SignatureType
85
+ ) {
86
+ if (isSighashNone(sighashType)) {
87
+ return zeroHash();
88
+ }
89
+
90
+ // SigHashSingle: If the relevant output exists - return its hash, otherwise return zero-hash
91
+ if (isSighashSingle(sighashType)) {
92
+ if (inputNumber >= transaction.outputs.length) {
93
+ return zeroHash();
94
+ }
95
+
96
+ const hashWriter = new HashWriter();
97
+ return hashWriter.finalize();
98
+ }
99
+
100
+ const hashWriter = new HashWriter();
101
+ transaction.outputs.forEach(output => hashTxOut(hashWriter, output));
102
+ return hashWriter.finalize();
103
+ }
104
+
105
+ /**
106
+ * Returns a buffer of length 32 bytes with the hash that needs to be signed
107
+ * for OP_CHECKSIG.
108
+ *
109
+ * @name serialize
110
+ * @param {Transaction} transaction the transaction to sign
111
+ * @param {number} sighashType the type of the hash
112
+ * @param {number} inputNumber the input index for the signature
113
+ * @param {Script} subscript the script that will be signed
114
+ * @param {satoshisBN} input's amount (for ForkId signatures)
115
+ *
116
+ */
117
+ export function serialize(transaction: KaspaSignTransactionParams, inputNumber: number) {
118
+ const hashWriter = new HashWriter();
119
+
120
+ hashWriter.writeUInt16LE(transaction.version);
121
+ hashWriter.writeHash(getPreviousOutputsHash(transaction, transaction.sigHashType!));
122
+ hashWriter.writeHash(getSequencesHash(transaction, transaction.sigHashType!));
123
+ hashWriter.writeHash(getSigOpCountsHash(transaction, transaction.sigHashType!));
124
+
125
+ const input = transaction.inputs[inputNumber];
126
+ hashOutpoint(hashWriter, input);
127
+ hashWriter.writeUInt16LE(0); // TODO: USE REAL SCRIPT VERSION
128
+ hashWriter.writeVarBytes(Buffer.from(input.output.script, 'hex'));
129
+ hashWriter.writeUInt64LE(input.output.satoshis);
130
+ hashWriter.writeUInt64LE(input.sequenceNumber);
131
+ hashWriter.writeUInt8(transaction.sigOpCount ?? 1); // sigOpCount
132
+ hashWriter.writeHash(getOutputsHash(transaction, inputNumber, transaction.sigHashType!));
133
+ hashWriter.writeUInt64LE(transaction.lockTime);
134
+ hashWriter.writeHash(zeroSubnetworkID()); // TODO: USE REAL SUBNETWORK ID
135
+ hashWriter.writeUInt64LE(0); // TODO: USE REAL GAS
136
+ hashWriter.writeHash(zeroHash()); // TODO: USE REAL PAYLOAD HASH
137
+ hashWriter.writeUInt8(transaction.sigHashType!);
138
+
139
+ return {
140
+ hash: hashWriter.finalize(),
141
+ raw: hashWriter.toBuffer(),
142
+ };
143
+ }
package/src/core/index.ts CHANGED
@@ -89,7 +89,6 @@ export const callAPI = async (message: CoreMessage) => {
89
89
  const response = await method.run();
90
90
  return createResponseMessage(method.responseID, true, response);
91
91
  } catch (error) {
92
- console.log('!method.useDevice error =====>>> ', error);
93
92
  return createResponseMessage(method.responseID, false, { error });
94
93
  }
95
94
  }
@@ -337,6 +336,7 @@ export const callAPI = async (message: CoreMessage) => {
337
336
  cleanup();
338
337
 
339
338
  removeDeviceListener(device);
339
+ // TODO: 方法执行后,检查队列内是否有等待调用的 API,依次调用
340
340
  }
341
341
  };
342
342
 
@@ -580,7 +580,7 @@ const cleanup = () => {
580
580
 
581
581
  const removeDeviceListener = (device: Device) => {
582
582
  device.removeAllListeners();
583
- DevicePool.emitter.removeAllListeners(DEVICE.CONNECT);
583
+ DevicePool.emitter.removeListener(DEVICE.CONNECT, onDeviceConnectHandler);
584
584
  // DevicePool.emitter.removeListener(DEVICE.DISCONNECT, onDeviceDisconnectHandler);
585
585
  };
586
586
 
@@ -4468,6 +4468,122 @@
4468
4468
  }
4469
4469
  }
4470
4470
  },
4471
+ "KaspaGetAddress": {
4472
+ "fields": {
4473
+ "address_n": {
4474
+ "rule": "repeated",
4475
+ "type": "uint32",
4476
+ "id": 1,
4477
+ "options": {
4478
+ "packed": false
4479
+ }
4480
+ },
4481
+ "show_display": {
4482
+ "type": "bool",
4483
+ "id": 2
4484
+ },
4485
+ "prefix": {
4486
+ "type": "string",
4487
+ "id": 3,
4488
+ "options": {
4489
+ "default": "kaspa"
4490
+ }
4491
+ },
4492
+ "scheme": {
4493
+ "type": "string",
4494
+ "id": 4,
4495
+ "options": {
4496
+ "default": "schnorr"
4497
+ }
4498
+ }
4499
+ }
4500
+ },
4501
+ "KaspaAddress": {
4502
+ "fields": {
4503
+ "address": {
4504
+ "rule": "required",
4505
+ "type": "string",
4506
+ "id": 1
4507
+ }
4508
+ }
4509
+ },
4510
+ "KaspaSignTx": {
4511
+ "fields": {
4512
+ "address_n": {
4513
+ "rule": "repeated",
4514
+ "type": "uint32",
4515
+ "id": 1,
4516
+ "options": {
4517
+ "packed": false
4518
+ }
4519
+ },
4520
+ "raw_message": {
4521
+ "rule": "required",
4522
+ "type": "bytes",
4523
+ "id": 2
4524
+ },
4525
+ "scheme": {
4526
+ "type": "string",
4527
+ "id": 3,
4528
+ "options": {
4529
+ "default": "schnorr"
4530
+ }
4531
+ },
4532
+ "prefix": {
4533
+ "type": "string",
4534
+ "id": 4,
4535
+ "options": {
4536
+ "default": "kaspa"
4537
+ }
4538
+ },
4539
+ "input_count": {
4540
+ "type": "uint32",
4541
+ "id": 5,
4542
+ "options": {
4543
+ "default": 1
4544
+ }
4545
+ }
4546
+ }
4547
+ },
4548
+ "KaspaTxInputRequest": {
4549
+ "fields": {
4550
+ "request_index": {
4551
+ "rule": "required",
4552
+ "type": "uint32",
4553
+ "id": 1
4554
+ },
4555
+ "signature": {
4556
+ "type": "bytes",
4557
+ "id": 2
4558
+ }
4559
+ }
4560
+ },
4561
+ "KaspaTxInputAck": {
4562
+ "fields": {
4563
+ "address_n": {
4564
+ "rule": "repeated",
4565
+ "type": "uint32",
4566
+ "id": 1,
4567
+ "options": {
4568
+ "packed": false
4569
+ }
4570
+ },
4571
+ "raw_message": {
4572
+ "rule": "required",
4573
+ "type": "bytes",
4574
+ "id": 2
4575
+ }
4576
+ }
4577
+ },
4578
+ "KaspaSignedTx": {
4579
+ "fields": {
4580
+ "signature": {
4581
+ "rule": "required",
4582
+ "type": "bytes",
4583
+ "id": 2
4584
+ }
4585
+ }
4586
+ },
4471
4587
  "BackupType": {
4472
4588
  "values": {
4473
4589
  "Bip39": 0,
@@ -4743,6 +4859,10 @@
4743
4859
  "build_id": {
4744
4860
  "type": "string",
4745
4861
  "id": 518
4862
+ },
4863
+ "battery_level": {
4864
+ "type": "uint32",
4865
+ "id": 520
4746
4866
  }
4747
4867
  },
4748
4868
  "nested": {
@@ -8936,6 +9056,12 @@
8936
9056
  "MessageType_FilecoinAddress": 11201,
8937
9057
  "MessageType_FilecoinSignTx": 11202,
8938
9058
  "MessageType_FilecoinSignedTx": 11203,
9059
+ "MessageType_KaspaGetAddress": 11300,
9060
+ "MessageType_KaspaAddress": 11301,
9061
+ "MessageType_KaspaSignTx": 11302,
9062
+ "MessageType_KaspaSignedTx": 11303,
9063
+ "MessageType_KaspaTxInputRequest": 11304,
9064
+ "MessageType_KaspaTxInputAck": 11305,
8939
9065
  "MessageType_DeviceBackToBoot": 903,
8940
9066
  "MessageType_DeviceInfoSettings": 10001,
8941
9067
  "MessageType_GetDeviceInfo": 10002,
@@ -123,7 +123,6 @@ export default class DataManager {
123
123
  static getBootloaderTargetVersion = (features: Features): IVersionArray | undefined => {
124
124
  const deviceType = getDeviceType(features);
125
125
 
126
- if (deviceType !== 'pro' && deviceType !== 'touch') return undefined;
127
126
  const firmwareUpdateField = getFirmwareUpdateField(features, 'firmware') as FirmwareField;
128
127
  const targetDeviceConfigList = this.deviceMap[deviceType]?.[firmwareUpdateField] ?? [];
129
128
  const targetDeviceConfig = targetDeviceConfigList.filter(item => !!item.bootloaderResource);
@@ -131,6 +130,19 @@ export default class DataManager {
131
130
  return targetDeviceConfig?.[0]?.bootloaderVersion ?? undefined;
132
131
  };
133
132
 
133
+ static getBootloaderRelatedFirmwareVersion = (features: Features): IVersionArray | undefined => {
134
+ const deviceType = getDeviceType(features);
135
+
136
+ if (deviceType !== 'classic') return undefined;
137
+ const firmwareUpdateField = getFirmwareUpdateField(features, 'firmware') as FirmwareField;
138
+ const targetDeviceConfigList = this.deviceMap[deviceType]?.[firmwareUpdateField] ?? [];
139
+ const targetDeviceConfig = targetDeviceConfigList.filter(
140
+ item => !!item.bootloaderRelatedFirmwareVersion
141
+ );
142
+
143
+ return targetDeviceConfig?.[0]?.bootloaderRelatedFirmwareVersion ?? undefined;
144
+ };
145
+
134
146
  static getFirmwareChangelog = (features: Features) => {
135
147
  const deviceType = getDeviceType(features);
136
148
  const deviceFirmwareVersion = getDeviceFirmwareVersion(features);
package/src/index.ts CHANGED
@@ -1,11 +1,6 @@
1
1
  import { inject, InjectApi } from './inject';
2
- import { lowLevelInject, LowLevelInjectApi, LowLevelCoreApi } from './lowLevelInject';
3
- import { topLevelInject } from './topLevelInject';
4
2
  import { CoreApi } from './types/api';
5
3
 
6
- export type { LowLevelCoreApi, LowLevelInjectApi } from './lowLevelInject';
7
- export type { TopLevelInjectApi } from './topLevelInject';
8
-
9
4
  export { default as Core, init as initCore } from './core';
10
5
 
11
6
  export * from './constants';
@@ -31,27 +26,4 @@ const HardwareSdk = ({
31
26
  cancel,
32
27
  });
33
28
 
34
- const HardwareSDKLowLevel = ({
35
- init,
36
- call,
37
- dispose,
38
- eventEmitter,
39
- addHardwareGlobalEventListener,
40
- uiResponse,
41
- cancel,
42
- }: LowLevelInjectApi): LowLevelCoreApi =>
43
- lowLevelInject({
44
- init,
45
- call,
46
- dispose,
47
- eventEmitter,
48
- addHardwareGlobalEventListener,
49
- uiResponse,
50
- cancel,
51
- });
52
-
53
- const HardwareTopLevelSdk = (): CoreApi => topLevelInject();
54
-
55
- export { HardwareTopLevelSdk, HardwareSDKLowLevel };
56
-
57
29
  export default HardwareSdk;