@onekeyfe/hd-core 1.1.32-alpha.1 → 1.1.32

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.
package/dist/index.js CHANGED
@@ -7552,7 +7552,6 @@ var nested$1 = {
7552
7552
  }
7553
7553
  },
7554
7554
  raw_message: {
7555
- rule: "required",
7556
7555
  type: "bytes",
7557
7556
  id: 2
7558
7557
  },
@@ -7583,6 +7582,42 @@ var nested$1 = {
7583
7582
  options: {
7584
7583
  "default": true
7585
7584
  }
7585
+ },
7586
+ output_count: {
7587
+ type: "uint32",
7588
+ id: 7
7589
+ },
7590
+ version: {
7591
+ type: "uint32",
7592
+ id: 8,
7593
+ options: {
7594
+ "default": 0
7595
+ }
7596
+ },
7597
+ lock_time: {
7598
+ type: "uint64",
7599
+ id: 9,
7600
+ options: {
7601
+ "default": 0
7602
+ }
7603
+ },
7604
+ subnetwork_id: {
7605
+ type: "bytes",
7606
+ id: 10
7607
+ },
7608
+ gas: {
7609
+ type: "uint64",
7610
+ id: 11,
7611
+ options: {
7612
+ "default": 0
7613
+ }
7614
+ },
7615
+ payload_length: {
7616
+ type: "uint32",
7617
+ id: 12,
7618
+ options: {
7619
+ "default": 0
7620
+ }
7586
7621
  }
7587
7622
  }
7588
7623
  },
@@ -7616,6 +7651,249 @@ var nested$1 = {
7616
7651
  }
7617
7652
  }
7618
7653
  },
7654
+ KaspaOutpoint: {
7655
+ fields: {
7656
+ tx_id: {
7657
+ rule: "required",
7658
+ type: "bytes",
7659
+ id: 1
7660
+ },
7661
+ index: {
7662
+ rule: "required",
7663
+ type: "uint32",
7664
+ id: 2
7665
+ }
7666
+ }
7667
+ },
7668
+ KaspaInputScriptType: {
7669
+ values: {
7670
+ KASPA_SPEND_P2PK_SCHNORR: 0,
7671
+ KASPA_SPEND_P2PK_ECDSA: 1
7672
+ }
7673
+ },
7674
+ KaspaOutputScriptType: {
7675
+ values: {
7676
+ KASPA_PAYTOADDRESS: 0,
7677
+ KASPA_PAYTOCHANGE: 1
7678
+ }
7679
+ },
7680
+ KaspaRequestType: {
7681
+ values: {
7682
+ KASPA_TX_INPUT: 0,
7683
+ KASPA_TX_OUTPUT: 1,
7684
+ KASPA_TX_PAYLOAD: 2,
7685
+ KASPA_TX_FINISHED: 3,
7686
+ KASPA_TX_PREV_META: 4
7687
+ }
7688
+ },
7689
+ KaspaTxRequestSignature: {
7690
+ fields: {
7691
+ signature_index: {
7692
+ rule: "required",
7693
+ type: "uint32",
7694
+ id: 1
7695
+ },
7696
+ signature: {
7697
+ rule: "required",
7698
+ type: "bytes",
7699
+ id: 2
7700
+ }
7701
+ }
7702
+ },
7703
+ KaspaTxRequest: {
7704
+ fields: {
7705
+ request_type: {
7706
+ rule: "required",
7707
+ type: "KaspaRequestType",
7708
+ id: 1
7709
+ },
7710
+ request_index: {
7711
+ type: "uint32",
7712
+ id: 2
7713
+ },
7714
+ signature: {
7715
+ type: "KaspaTxRequestSignature",
7716
+ id: 3
7717
+ },
7718
+ request_payload_length: {
7719
+ type: "uint32",
7720
+ id: 4
7721
+ },
7722
+ prev_tx_id: {
7723
+ type: "bytes",
7724
+ id: 5
7725
+ }
7726
+ }
7727
+ },
7728
+ KaspaTxAckInput: {
7729
+ fields: {
7730
+ address_n: {
7731
+ rule: "repeated",
7732
+ type: "uint32",
7733
+ id: 1,
7734
+ options: {
7735
+ packed: false
7736
+ }
7737
+ },
7738
+ previous_outpoint: {
7739
+ rule: "required",
7740
+ type: "KaspaOutpoint",
7741
+ id: 2
7742
+ },
7743
+ amount: {
7744
+ rule: "required",
7745
+ type: "uint64",
7746
+ id: 3
7747
+ },
7748
+ sequence: {
7749
+ rule: "required",
7750
+ type: "uint64",
7751
+ id: 4
7752
+ },
7753
+ sig_op_count: {
7754
+ rule: "required",
7755
+ type: "uint32",
7756
+ id: 5
7757
+ },
7758
+ script_type: {
7759
+ type: "KaspaInputScriptType",
7760
+ id: 6,
7761
+ options: {
7762
+ "default": "KASPA_SPEND_P2PK_SCHNORR"
7763
+ }
7764
+ },
7765
+ use_tweak: {
7766
+ type: "bool",
7767
+ id: 9,
7768
+ options: {
7769
+ "default": true
7770
+ }
7771
+ }
7772
+ }
7773
+ },
7774
+ KaspaTxAckOutput: {
7775
+ fields: {
7776
+ script_type: {
7777
+ type: "KaspaOutputScriptType",
7778
+ id: 1,
7779
+ options: {
7780
+ "default": "KASPA_PAYTOADDRESS"
7781
+ }
7782
+ },
7783
+ amount: {
7784
+ rule: "required",
7785
+ type: "uint64",
7786
+ id: 2
7787
+ },
7788
+ address_n: {
7789
+ rule: "repeated",
7790
+ type: "uint32",
7791
+ id: 3,
7792
+ options: {
7793
+ packed: false
7794
+ }
7795
+ },
7796
+ address: {
7797
+ type: "string",
7798
+ id: 4
7799
+ },
7800
+ scheme: {
7801
+ type: "string",
7802
+ id: 7,
7803
+ options: {
7804
+ "default": "schnorr"
7805
+ }
7806
+ },
7807
+ use_tweak: {
7808
+ type: "bool",
7809
+ id: 8,
7810
+ options: {
7811
+ "default": true
7812
+ }
7813
+ }
7814
+ }
7815
+ },
7816
+ KaspaTxAckPayloadChunk: {
7817
+ fields: {
7818
+ payload_chunk: {
7819
+ rule: "required",
7820
+ type: "bytes",
7821
+ id: 1
7822
+ }
7823
+ }
7824
+ },
7825
+ KaspaTxAckPrevMeta: {
7826
+ fields: {
7827
+ version: {
7828
+ rule: "required",
7829
+ type: "uint32",
7830
+ id: 1
7831
+ },
7832
+ input_count: {
7833
+ rule: "required",
7834
+ type: "uint32",
7835
+ id: 2
7836
+ },
7837
+ output_count: {
7838
+ rule: "required",
7839
+ type: "uint32",
7840
+ id: 3
7841
+ },
7842
+ lock_time: {
7843
+ rule: "required",
7844
+ type: "uint64",
7845
+ id: 4
7846
+ },
7847
+ subnetwork_id: {
7848
+ rule: "required",
7849
+ type: "bytes",
7850
+ id: 5
7851
+ },
7852
+ gas: {
7853
+ rule: "required",
7854
+ type: "uint64",
7855
+ id: 6
7856
+ },
7857
+ payload_length: {
7858
+ rule: "required",
7859
+ type: "uint32",
7860
+ id: 7
7861
+ }
7862
+ }
7863
+ },
7864
+ KaspaTxAckPrevInput: {
7865
+ fields: {
7866
+ previous_outpoint: {
7867
+ rule: "required",
7868
+ type: "KaspaOutpoint",
7869
+ id: 1
7870
+ },
7871
+ sequence: {
7872
+ rule: "required",
7873
+ type: "uint64",
7874
+ id: 2
7875
+ }
7876
+ }
7877
+ },
7878
+ KaspaTxAckPrevOutput: {
7879
+ fields: {
7880
+ amount: {
7881
+ rule: "required",
7882
+ type: "uint64",
7883
+ id: 1
7884
+ },
7885
+ script_version: {
7886
+ rule: "required",
7887
+ type: "uint32",
7888
+ id: 2
7889
+ },
7890
+ script_public_key: {
7891
+ rule: "required",
7892
+ type: "bytes",
7893
+ id: 3
7894
+ }
7895
+ }
7896
+ },
7619
7897
  KaspaSignedTx: {
7620
7898
  fields: {
7621
7899
  signature: {
@@ -14063,6 +14341,13 @@ var nested$1 = {
14063
14341
  MessageType_KaspaSignedTx: 11303,
14064
14342
  MessageType_KaspaTxInputRequest: 11304,
14065
14343
  MessageType_KaspaTxInputAck: 11305,
14344
+ MessageType_KaspaTxRequest: 11306,
14345
+ MessageType_KaspaTxAckInput: 11307,
14346
+ MessageType_KaspaTxAckOutput: 11308,
14347
+ MessageType_KaspaTxAckPayloadChunk: 11309,
14348
+ MessageType_KaspaTxAckPrevMeta: 11310,
14349
+ MessageType_KaspaTxAckPrevInput: 11311,
14350
+ MessageType_KaspaTxAckPrevOutput: 11312,
14066
14351
  MessageType_NexaGetAddress: 11400,
14067
14352
  MessageType_NexaAddress: 11401,
14068
14353
  MessageType_NexaSignTx: 11402,
@@ -38315,6 +38600,9 @@ function getSigOpCountsHash(transaction, sighashType) {
38315
38600
  function hashTxOut(hashWriter, output) {
38316
38601
  hashWriter.writeUInt64LE(output.satoshis);
38317
38602
  hashWriter.writeUInt16LE(0);
38603
+ if (output.script === undefined) {
38604
+ throw hdShared.ERRORS.TypedError(hdShared.HardwareErrorCode.CallMethodInvalidParameter, 'Kaspa legacy sighash requires output.script');
38605
+ }
38318
38606
  hashWriter.writeVarBytes(Buffer.from(output.script, 'hex'));
38319
38607
  }
38320
38608
  function getOutputsHash(transaction, inputNumber, sighashType) {
@@ -38342,6 +38630,9 @@ function serialize(transaction, inputNumber) {
38342
38630
  const input = transaction.inputs[inputNumber];
38343
38631
  hashOutpoint(hashWriter, input);
38344
38632
  hashWriter.writeUInt16LE(0);
38633
+ if (input.output.script === undefined) {
38634
+ throw hdShared.ERRORS.TypedError(hdShared.HardwareErrorCode.CallMethodInvalidParameter, 'Kaspa legacy sighash requires input.output.script');
38635
+ }
38345
38636
  hashWriter.writeVarBytes(Buffer.from(input.output.script, 'hex'));
38346
38637
  hashWriter.writeUInt64LE(input.output.satoshis);
38347
38638
  hashWriter.writeUInt64LE(input.sequenceNumber);
@@ -38358,25 +38649,31 @@ function serialize(transaction, inputNumber) {
38358
38649
  };
38359
38650
  }
38360
38651
 
38652
+ const P2PK_SCRIPT = /^(20[0-9a-f]{64}ac|21[0-9a-f]{66}ab)$/i;
38653
+ const isStreamableScript = (script) => !script || P2PK_SCRIPT.test(script);
38361
38654
  class KaspaSignTransaction extends BaseMethod {
38362
38655
  constructor() {
38363
38656
  super(...arguments);
38364
38657
  this.hasBundle = false;
38658
+ this.supportsLegacy = false;
38659
+ this.supportsStreaming = false;
38365
38660
  }
38366
38661
  init() {
38367
- var _a, _b, _c, _d, _e;
38662
+ var _a, _b, _c, _d, _e, _f, _g, _h, _j;
38368
38663
  this.checkDeviceId = true;
38369
38664
  this.allowDeviceMode = [...this.allowDeviceMode, UI_REQUEST.NOT_INITIALIZE];
38370
38665
  this.allowUsePreInitialize = true;
38371
38666
  const payload = this.payload;
38372
38667
  validateParams(payload, [
38373
38668
  { name: 'version', type: 'number' },
38374
- { name: 'sigHashType', type: 'number', required: true },
38669
+ { name: 'sigHashType', type: 'number' },
38375
38670
  { name: 'inputs', type: 'array', required: true },
38376
38671
  { name: 'outputs', type: 'array', required: true },
38377
38672
  { name: 'lockTime', required: true },
38378
38673
  { name: 'sigOpCount', type: 'number' },
38379
38674
  { name: 'subNetworkID', type: 'string' },
38675
+ { name: 'payload', type: 'hexString' },
38676
+ { name: 'refTxs', type: 'array', allowEmpty: true },
38380
38677
  { name: 'useTweak', type: 'boolean' },
38381
38678
  ]);
38382
38679
  const inputs = payload.inputs.map(input => {
@@ -38387,20 +38684,47 @@ class KaspaSignTransaction extends BaseMethod {
38387
38684
  { name: 'outputIndex', type: 'number', required: true },
38388
38685
  { name: 'sequenceNumber', required: true },
38389
38686
  ]);
38687
+ validateParams(input.output, [{ name: 'satoshis', required: true }]);
38390
38688
  const addressN = validatePath(input.path, 3);
38391
38689
  return Object.assign(Object.assign({}, input), { path: addressN, sigOpCount: (_a = input.sigOpCount) !== null && _a !== void 0 ? _a : 1 });
38392
38690
  });
38691
+ (_a = payload.refTxs) === null || _a === void 0 ? void 0 : _a.forEach(refTx => {
38692
+ validateParams(refTx, [
38693
+ { name: 'txId', type: 'string', required: true },
38694
+ { name: 'version', type: 'number', required: true },
38695
+ { name: 'inputs', type: 'array', required: true, allowEmpty: true },
38696
+ { name: 'outputs', type: 'array', required: true, allowEmpty: true },
38697
+ { name: 'payload', type: 'hexString' },
38698
+ ]);
38699
+ });
38393
38700
  const outputs = payload.outputs.map(output => {
38394
38701
  var _a;
38395
38702
  validateParams(output, [
38396
38703
  { name: 'satoshis', required: true },
38397
- { name: 'script', type: 'string', required: true },
38704
+ { name: 'address', type: 'string' },
38705
+ { name: 'script', type: 'string' },
38398
38706
  { name: 'scriptVersion', type: 'number' },
38399
38707
  ]);
38400
- return Object.assign(Object.assign({}, output), { scriptVersion: (_a = output.scriptVersion) !== null && _a !== void 0 ? _a : 0 });
38708
+ return Object.assign(Object.assign({}, output), { addressN: output.addressN ? validatePath(output.addressN, 3) : undefined, scriptVersion: (_a = output.scriptVersion) !== null && _a !== void 0 ? _a : 0 });
38401
38709
  });
38402
38710
  this.params = Object.assign(Object.assign({}, payload), { inputs,
38403
- outputs, scheme: (_a = payload.scheme) !== null && _a !== void 0 ? _a : 'schnorr', prefix: (_b = payload.prefix) !== null && _b !== void 0 ? _b : 'kaspa', sigHashType: (_c = payload.sigHashType) !== null && _c !== void 0 ? _c : SignatureType.SIGHASH_ALL | SignatureType.SIGHASH_FORKID, sigOpCount: (_d = payload.sigOpCount) !== null && _d !== void 0 ? _d : 1, subNetworkID: (_e = payload.subNetworkID) !== null && _e !== void 0 ? _e : utils.bytesToHex(zeroSubnetworkID()), useTweak: payload.useTweak });
38711
+ outputs, scheme: (_b = payload.scheme) !== null && _b !== void 0 ? _b : 'schnorr', prefix: (_c = payload.prefix) !== null && _c !== void 0 ? _c : 'kaspa', sigHashType: (_d = payload.sigHashType) !== null && _d !== void 0 ? _d : SignatureType.SIGHASH_ALL | SignatureType.SIGHASH_FORKID, sigOpCount: (_e = payload.sigOpCount) !== null && _e !== void 0 ? _e : 1, subNetworkID: (_f = payload.subNetworkID) !== null && _f !== void 0 ? _f : utils.bytesToHex(zeroSubnetworkID()), gas: (_g = payload.gas) !== null && _g !== void 0 ? _g : 0, useTweak: payload.useTweak });
38712
+ this.supportsLegacy =
38713
+ this.params.outputs.every(output => !!output.script) &&
38714
+ this.params.inputs.every(input => !!input.output.script) &&
38715
+ !this.params.payload &&
38716
+ Number((_h = this.params.gas) !== null && _h !== void 0 ? _h : 0) === 0 &&
38717
+ /^0*$/.test((_j = this.params.subNetworkID) !== null && _j !== void 0 ? _j : '');
38718
+ const isDefaultSigHashType = this.params.sigHashType === SignatureType.SIGHASH_ALL ||
38719
+ this.params.sigHashType === (SignatureType.SIGHASH_ALL | SignatureType.SIGHASH_FORKID);
38720
+ this.supportsStreaming =
38721
+ isDefaultSigHashType &&
38722
+ this.params.outputs.every(output => !!output.address || !!output.addressN) &&
38723
+ this.params.inputs.every(input => isStreamableScript(input.output.script)) &&
38724
+ this.params.outputs.every(output => isStreamableScript(output.script));
38725
+ if (!this.supportsLegacy && !this.supportsStreaming) {
38726
+ throw hdShared.ERRORS.TypedError(hdShared.HardwareErrorCode.CallMethodInvalidParameter, 'KaspaSignTransaction: outputs require either address/addressN (streaming protocol) or script (legacy protocol)');
38727
+ }
38404
38728
  }
38405
38729
  getVersionRange() {
38406
38730
  return {
@@ -38449,23 +38773,188 @@ class KaspaSignTransaction extends BaseMethod {
38449
38773
  return signature;
38450
38774
  });
38451
38775
  }
38776
+ ackPrevRequest(typedCall, request, requestIndex) {
38777
+ var _a, _b, _c, _d, _e, _f, _g, _h, _j;
38778
+ return __awaiter(this, void 0, void 0, function* () {
38779
+ const prevTxId = ((_a = request.prev_tx_id) !== null && _a !== void 0 ? _a : '').toLowerCase();
38780
+ const refTx = ((_b = this.params.refTxs) !== null && _b !== void 0 ? _b : []).find(tx => tx.txId.toLowerCase() === prevTxId);
38781
+ if (!refTx) {
38782
+ throw hdShared.ERRORS.TypedError(hdShared.HardwareErrorCode.CallMethodInvalidParameter, `KaspaSignTransaction: device requested previous transaction ${prevTxId || '(unknown)'}; provide it via refTxs`);
38783
+ }
38784
+ const requestType = request.request_type;
38785
+ if (requestType === 'KASPA_TX_PREV_META') {
38786
+ return typedCall('KaspaTxAckPrevMeta', 'KaspaTxRequest', {
38787
+ version: refTx.version,
38788
+ input_count: refTx.inputs.length,
38789
+ output_count: refTx.outputs.length,
38790
+ lock_time: ((_c = refTx.lockTime) !== null && _c !== void 0 ? _c : 0),
38791
+ subnetwork_id: (_d = refTx.subNetworkID) !== null && _d !== void 0 ? _d : utils.bytesToHex(zeroSubnetworkID()),
38792
+ gas: ((_e = refTx.gas) !== null && _e !== void 0 ? _e : 0),
38793
+ payload_length: ((_f = refTx.payload) !== null && _f !== void 0 ? _f : '').length / 2,
38794
+ });
38795
+ }
38796
+ if (requestType === 'KASPA_TX_INPUT') {
38797
+ const input = refTx.inputs[requestIndex];
38798
+ if (!input) {
38799
+ throw hdShared.ERRORS.TypedError(hdShared.HardwareErrorCode.RuntimeError, `KaspaSignTransaction: device requested input ${requestIndex} of previous tx out of range`);
38800
+ }
38801
+ return typedCall('KaspaTxAckPrevInput', 'KaspaTxRequest', {
38802
+ previous_outpoint: {
38803
+ tx_id: input.prevTxId,
38804
+ index: input.outputIndex,
38805
+ },
38806
+ sequence: input.sequenceNumber,
38807
+ });
38808
+ }
38809
+ if (requestType === 'KASPA_TX_OUTPUT') {
38810
+ const output = refTx.outputs[requestIndex];
38811
+ if (!output) {
38812
+ throw hdShared.ERRORS.TypedError(hdShared.HardwareErrorCode.RuntimeError, `KaspaSignTransaction: device requested output ${requestIndex} of previous tx out of range`);
38813
+ }
38814
+ return typedCall('KaspaTxAckPrevOutput', 'KaspaTxRequest', {
38815
+ amount: output.satoshis,
38816
+ script_version: (_g = output.scriptVersion) !== null && _g !== void 0 ? _g : 0,
38817
+ script_public_key: output.script,
38818
+ });
38819
+ }
38820
+ if (requestType === 'KASPA_TX_PAYLOAD') {
38821
+ const payloadHex = (_h = refTx.payload) !== null && _h !== void 0 ? _h : '';
38822
+ const payloadLength = payloadHex.length / 2;
38823
+ const length = (_j = request.request_payload_length) !== null && _j !== void 0 ? _j : payloadLength - requestIndex;
38824
+ return typedCall('KaspaTxAckPayloadChunk', 'KaspaTxRequest', {
38825
+ payload_chunk: payloadHex.substring(requestIndex * 2, (requestIndex + length) * 2),
38826
+ });
38827
+ }
38828
+ throw hdShared.ERRORS.TypedError(hdShared.HardwareErrorCode.RuntimeError, `KaspaSignTransaction: unknown previous-tx request type ${requestType !== null && requestType !== void 0 ? requestType : 'undefined'}`);
38829
+ });
38830
+ }
38831
+ signTxStream(typedCall, firstResponse) {
38832
+ var _a, _b, _c, _d, _e;
38833
+ return __awaiter(this, void 0, void 0, function* () {
38834
+ const { params } = this;
38835
+ const signatures = [];
38836
+ const inputScriptType = params.scheme === 'ecdsa' ? 'KASPA_SPEND_P2PK_ECDSA' : 'KASPA_SPEND_P2PK_SCHNORR';
38837
+ const saveSignature = (signature) => {
38838
+ if (signature && typeof signature.signature_index === 'number' && signature.signature) {
38839
+ signatures[signature.signature_index] = {
38840
+ index: signature.signature_index,
38841
+ signature: signature.signature,
38842
+ };
38843
+ }
38844
+ };
38845
+ const payloadHex = (_a = params.payload) !== null && _a !== void 0 ? _a : '';
38846
+ const payloadLength = payloadHex.length / 2;
38847
+ let response = firstResponse;
38848
+ while (true) {
38849
+ const request = response.message;
38850
+ saveSignature(request.signature);
38851
+ const requestType = request.request_type;
38852
+ if (requestType === 'KASPA_TX_FINISHED') {
38853
+ break;
38854
+ }
38855
+ const requestIndex = (_b = request.request_index) !== null && _b !== void 0 ? _b : 0;
38856
+ if (request.prev_tx_id || requestType === 'KASPA_TX_PREV_META') {
38857
+ response = yield this.ackPrevRequest(typedCall, request, requestIndex);
38858
+ }
38859
+ else if (requestType === 'KASPA_TX_INPUT') {
38860
+ const input = params.inputs[requestIndex];
38861
+ if (!input) {
38862
+ throw hdShared.ERRORS.TypedError(hdShared.HardwareErrorCode.RuntimeError, `KaspaSignTransaction: device requested input ${requestIndex} out of range`);
38863
+ }
38864
+ response = yield typedCall('KaspaTxAckInput', 'KaspaTxRequest', {
38865
+ address_n: input.path,
38866
+ previous_outpoint: {
38867
+ tx_id: input.prevTxId,
38868
+ index: input.outputIndex,
38869
+ },
38870
+ amount: input.output.satoshis,
38871
+ sequence: input.sequenceNumber,
38872
+ sig_op_count: (_c = input.sigOpCount) !== null && _c !== void 0 ? _c : 1,
38873
+ script_type: inputScriptType,
38874
+ use_tweak: params.useTweak,
38875
+ });
38876
+ }
38877
+ else if (requestType === 'KASPA_TX_OUTPUT') {
38878
+ const output = params.outputs[requestIndex];
38879
+ if (!output) {
38880
+ throw hdShared.ERRORS.TypedError(hdShared.HardwareErrorCode.RuntimeError, `KaspaSignTransaction: device requested output ${requestIndex} out of range`);
38881
+ }
38882
+ const isChange = !!output.addressN;
38883
+ response = yield typedCall('KaspaTxAckOutput', 'KaspaTxRequest', {
38884
+ script_type: isChange ? 'KASPA_PAYTOCHANGE' : 'KASPA_PAYTOADDRESS',
38885
+ amount: output.satoshis,
38886
+ address_n: (_d = output.addressN) !== null && _d !== void 0 ? _d : [],
38887
+ address: output.address,
38888
+ scheme: params.scheme,
38889
+ use_tweak: params.useTweak,
38890
+ });
38891
+ }
38892
+ else if (requestType === 'KASPA_TX_PAYLOAD') {
38893
+ const offset = requestIndex;
38894
+ const length = (_e = request.request_payload_length) !== null && _e !== void 0 ? _e : payloadLength - offset;
38895
+ const chunk = payloadHex.substring(offset * 2, (offset + length) * 2);
38896
+ response = yield typedCall('KaspaTxAckPayloadChunk', 'KaspaTxRequest', {
38897
+ payload_chunk: chunk,
38898
+ });
38899
+ }
38900
+ else {
38901
+ throw hdShared.ERRORS.TypedError(hdShared.HardwareErrorCode.RuntimeError, `KaspaSignTransaction: unknown request type ${requestType !== null && requestType !== void 0 ? requestType : 'undefined'}`);
38902
+ }
38903
+ }
38904
+ const collected = signatures.filter(Boolean);
38905
+ if (collected.length !== params.inputs.length) {
38906
+ throw hdShared.ERRORS.TypedError(hdShared.HardwareErrorCode.RuntimeError, `KaspaSignTransaction: expected ${params.inputs.length} signatures, received ${collected.length}`);
38907
+ }
38908
+ return collected;
38909
+ });
38910
+ }
38452
38911
  run() {
38912
+ var _a, _b, _c;
38453
38913
  return __awaiter(this, void 0, void 0, function* () {
38454
38914
  this.checkFeatureVersionLimit(() => this.params.useTweak === false, () => this.getUseTweakVersionRange(), {
38455
38915
  strictCheckDeviceSupport: true,
38456
38916
  });
38457
- const { raw: rawMessage } = serialize(this.params, 0);
38458
- const input = this.params.inputs[0];
38459
38917
  const { device, params } = this;
38460
- const response = yield device.commands.typedCall('KaspaSignTx', ['KaspaTxInputRequest', 'KaspaSignedTx'], {
38461
- address_n: input.path,
38462
- raw_message: utils.bytesToHex(rawMessage),
38463
- scheme: params.scheme,
38464
- prefix: params.prefix,
38465
- input_count: params.inputs.length,
38466
- use_tweak: params.useTweak,
38467
- });
38468
- return this.processTxRequest(device.commands.typedCall.bind(device.commands), response, 0, []);
38918
+ const payloadHex = (_a = params.payload) !== null && _a !== void 0 ? _a : '';
38919
+ const hasRefTxs = ((_c = (_b = params.refTxs) === null || _b === void 0 ? void 0 : _b.length) !== null && _c !== void 0 ? _c : 0) > 0;
38920
+ const streamingReady = this.supportsStreaming && (hasRefTxs || !this.supportsLegacy);
38921
+ const streamingFields = streamingReady
38922
+ ? {
38923
+ output_count: params.outputs.length,
38924
+ version: params.version,
38925
+ lock_time: params.lockTime,
38926
+ subnetwork_id: params.subNetworkID,
38927
+ gas: params.gas,
38928
+ payload_length: payloadHex.length / 2,
38929
+ }
38930
+ : {};
38931
+ const legacyFields = this.supportsLegacy
38932
+ ? { raw_message: utils.bytesToHex(serialize(params, 0).raw) }
38933
+ : {};
38934
+ let response;
38935
+ try {
38936
+ response = yield device.commands.typedCall('KaspaSignTx', ['KaspaTxRequest', 'KaspaTxInputRequest', 'KaspaSignedTx'], Object.assign(Object.assign({ address_n: params.inputs[0].path, scheme: params.scheme, prefix: params.prefix, input_count: params.inputs.length, use_tweak: params.useTweak }, streamingFields), legacyFields));
38937
+ }
38938
+ catch (error) {
38939
+ if (!this.supportsLegacy &&
38940
+ error instanceof hdShared.HardwareError &&
38941
+ error.errorCode === hdShared.HardwareErrorCode.RuntimeError &&
38942
+ String(error.message).includes('Failure_DataError')) {
38943
+ throw hdShared.ERRORS.TypedError(hdShared.HardwareErrorCode.CallMethodNeedUpgradeFirmware, 'KaspaSignTransaction: this transaction requires firmware with Kaspa streaming support');
38944
+ }
38945
+ throw error;
38946
+ }
38947
+ const typedCall = device.commands.typedCall.bind(device.commands);
38948
+ if (response.type === 'KaspaTxRequest') {
38949
+ if (!this.supportsStreaming) {
38950
+ throw hdShared.ERRORS.TypedError(hdShared.HardwareErrorCode.CallMethodInvalidParameter, 'KaspaSignTransaction: device firmware uses the streaming protocol; every output requires address or addressN');
38951
+ }
38952
+ return this.signTxStream(typedCall, response);
38953
+ }
38954
+ if (!this.supportsLegacy) {
38955
+ throw hdShared.ERRORS.TypedError(hdShared.HardwareErrorCode.RuntimeError, 'KaspaSignTransaction: device chose the legacy protocol but the transaction is not legacy-signable');
38956
+ }
38957
+ return this.processTxRequest(typedCall, response, 0, []);
38469
38958
  });
38470
38959
  }
38471
38960
  }
@@ -11,14 +11,36 @@ export type KaspaSignInputParams = {
11
11
  sequenceNumber: number | string;
12
12
  output: {
13
13
  satoshis: number | string;
14
- script: string;
14
+ script?: string;
15
15
  };
16
16
  sigOpCount?: number;
17
17
  };
18
18
  export type KaspaSignOutputParams = {
19
19
  satoshis: number | string;
20
+ address?: string;
21
+ addressN?: string | number[];
22
+ script?: string;
23
+ scriptVersion?: number;
24
+ };
25
+ export type KaspaRefTransactionInput = {
26
+ prevTxId: string;
27
+ outputIndex: number;
28
+ sequenceNumber: number | string;
29
+ };
30
+ export type KaspaRefTransactionOutput = {
31
+ satoshis: number | string;
32
+ scriptVersion?: number;
20
33
  script: string;
21
- scriptVersion: number;
34
+ };
35
+ export type KaspaRefTransaction = {
36
+ txId: string;
37
+ version: number;
38
+ inputs: KaspaRefTransactionInput[];
39
+ outputs: KaspaRefTransactionOutput[];
40
+ lockTime?: number | string;
41
+ subNetworkID?: string;
42
+ gas?: number | string;
43
+ payload?: string;
22
44
  };
23
45
  export type KaspaSignTransactionParams = {
24
46
  version: number;
@@ -28,6 +50,9 @@ export type KaspaSignTransactionParams = {
28
50
  sigHashType?: SignatureType;
29
51
  sigOpCount?: number;
30
52
  subNetworkID?: string;
53
+ payload?: string;
54
+ gas?: number | string;
55
+ refTxs?: KaspaRefTransaction[];
31
56
  scheme?: string;
32
57
  prefix?: string;
33
58
  useTweak?: boolean;
@@ -1 +1 @@
1
- {"version":3,"file":"kaspaSignTransaction.d.ts","sourceRoot":"","sources":["../../../src/types/api/kaspaSignTransaction.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,aAAa,EAAE,MAAM,uCAAuC,CAAC;AAC3E,OAAO,KAAK,EAAE,YAAY,EAAE,QAAQ,EAAE,MAAM,WAAW,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