@onekeyfe/cardano-coin-selection-asmjs 1.1.7 → 1.1.9

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.
@@ -33,6 +33,8 @@ const CardanoMessage = __importStar(require("@emurgo/cardano-message-signing-asm
33
33
  const bignumber_js_1 = __importDefault(require("bignumber.js"));
34
34
  const signTx_1 = require("./signTx");
35
35
  const error_1 = require("./error");
36
+ const errors_1 = require("../errors");
37
+ const constants_1 = require("../../constants");
36
38
  const types_1 = require("./types");
37
39
  const getBalance = async (balances) => {
38
40
  const value = await (0, exports.assetsToValue)(balances);
@@ -88,13 +90,20 @@ const getUtxos = async (address, utxos, amount) => {
88
90
  const convertCborTxToEncodeTx = async (txHex, utxos, addresses, changeAddress) => {
89
91
  var _a, _b, _c, _d;
90
92
  let body;
93
+ let rawTxHex;
91
94
  console.log('CARDANO LOCAL_VERSION : 1');
92
95
  try {
93
96
  const tx = CardanoWasm.Transaction.from_bytes(Buffer.from(txHex, 'hex'));
94
97
  body = tx.body();
98
+ rawTxHex = txHex;
95
99
  }
96
100
  catch (_e) {
101
+ // Input is TransactionBody only (e.g., from staking providers like stakefish)
102
+ // Build a complete Transaction with empty witness set
97
103
  body = CardanoWasm.TransactionBody.from_bytes(Buffer.from(txHex, 'hex'));
104
+ const emptyWitnessSet = CardanoWasm.TransactionWitnessSet.new();
105
+ const tx = CardanoWasm.Transaction.new(body, emptyWitnessSet);
106
+ rawTxHex = Buffer.from(tx.to_bytes(), 'hex').toString('hex');
98
107
  }
99
108
  // Fee
100
109
  const fee = body.fee().to_str();
@@ -177,6 +186,11 @@ const convertCborTxToEncodeTx = async (txHex, utxos, addresses, changeAddress) =
177
186
  // outputs txs
178
187
  const outputs = [];
179
188
  const outputsLen = body.outputs().len();
189
+ // All valid transactions must have at least one output
190
+ // Empty outputs means insufficient funds to cover the transaction
191
+ if (outputsLen === 0) {
192
+ throw new errors_1.CoinSelectionError(constants_1.ERROR.UTXO_BALANCE_INSUFFICIENT);
193
+ }
180
194
  for (let i = 0; i < outputsLen; i++) {
181
195
  const output = body.outputs().get(i);
182
196
  const address = output.address().to_bech32();
@@ -216,9 +230,7 @@ const convertCborTxToEncodeTx = async (txHex, utxos, addresses, changeAddress) =
216
230
  isChange,
217
231
  });
218
232
  }
219
- const totalSpent = outputs.length > 0
220
- ? bignumber_js_1.default.sum(...outputs.map(o => o.amount)).toFixed()
221
- : '0';
233
+ const totalSpent = bignumber_js_1.default.sum(...outputs.map(o => o.amount)).toFixed();
222
234
  const token = (_b = (_a = outputs
223
235
  .filter(o => !addresses.includes(o.address))
224
236
  .find(o => o.assets.length > 0)) === null || _a === void 0 ? void 0 : _a.assets) === null || _b === void 0 ? void 0 : _b[0].unit;
@@ -246,7 +258,7 @@ const convertCborTxToEncodeTx = async (txHex, utxos, addresses, changeAddress) =
246
258
  .transaction_hash()
247
259
  .to_hex(),
248
260
  size: 0,
249
- rawTxHex: txHex,
261
+ rawTxHex,
250
262
  },
251
263
  signOnly: true,
252
264
  staking: stakingInfo,
@@ -4,6 +4,8 @@ import * as CardanoMessage from '@emurgo/cardano-message-signing-asmjs/cardano_m
4
4
  import BigNumber from 'bignumber.js';
5
5
  import { getUtxos as getRawUtxos, requestAccountKey } from './signTx';
6
6
  import { DataSignError } from './error';
7
+ import { CoinSelectionError } from '../errors';
8
+ import { ERROR } from '../../constants';
7
9
  import { CardanoCertificateType, } from './types';
8
10
  const getBalance = async (balances) => {
9
11
  const value = await assetsToValue(balances);
@@ -58,13 +60,20 @@ const getUtxos = async (address, utxos, amount) => {
58
60
  const convertCborTxToEncodeTx = async (txHex, utxos, addresses, changeAddress) => {
59
61
  var _a, _b, _c, _d;
60
62
  let body;
63
+ let rawTxHex;
61
64
  console.log('CARDANO LOCAL_VERSION : 1');
62
65
  try {
63
66
  const tx = CardanoWasm.Transaction.from_bytes(Buffer.from(txHex, 'hex'));
64
67
  body = tx.body();
68
+ rawTxHex = txHex;
65
69
  }
66
70
  catch (_e) {
71
+ // Input is TransactionBody only (e.g., from staking providers like stakefish)
72
+ // Build a complete Transaction with empty witness set
67
73
  body = CardanoWasm.TransactionBody.from_bytes(Buffer.from(txHex, 'hex'));
74
+ const emptyWitnessSet = CardanoWasm.TransactionWitnessSet.new();
75
+ const tx = CardanoWasm.Transaction.new(body, emptyWitnessSet);
76
+ rawTxHex = Buffer.from(tx.to_bytes(), 'hex').toString('hex');
68
77
  }
69
78
  // Fee
70
79
  const fee = body.fee().to_str();
@@ -147,6 +156,11 @@ const convertCborTxToEncodeTx = async (txHex, utxos, addresses, changeAddress) =
147
156
  // outputs txs
148
157
  const outputs = [];
149
158
  const outputsLen = body.outputs().len();
159
+ // All valid transactions must have at least one output
160
+ // Empty outputs means insufficient funds to cover the transaction
161
+ if (outputsLen === 0) {
162
+ throw new CoinSelectionError(ERROR.UTXO_BALANCE_INSUFFICIENT);
163
+ }
150
164
  for (let i = 0; i < outputsLen; i++) {
151
165
  const output = body.outputs().get(i);
152
166
  const address = output.address().to_bech32();
@@ -186,9 +200,7 @@ const convertCborTxToEncodeTx = async (txHex, utxos, addresses, changeAddress) =
186
200
  isChange,
187
201
  });
188
202
  }
189
- const totalSpent = outputs.length > 0
190
- ? BigNumber.sum(...outputs.map(o => o.amount)).toFixed()
191
- : '0';
203
+ const totalSpent = BigNumber.sum(...outputs.map(o => o.amount)).toFixed();
192
204
  const token = (_b = (_a = outputs
193
205
  .filter(o => !addresses.includes(o.address))
194
206
  .find(o => o.assets.length > 0)) === null || _a === void 0 ? void 0 : _a.assets) === null || _b === void 0 ? void 0 : _b[0].unit;
@@ -216,7 +228,7 @@ const convertCborTxToEncodeTx = async (txHex, utxos, addresses, changeAddress) =
216
228
  .transaction_hash()
217
229
  .to_hex(),
218
230
  size: 0,
219
- rawTxHex: txHex,
231
+ rawTxHex,
220
232
  },
221
233
  signOnly: true,
222
234
  staking: stakingInfo,
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@onekeyfe/cardano-coin-selection-asmjs",
3
- "version": "1.1.7",
3
+ "version": "1.1.9",
4
4
  "description": "",
5
5
  "keywords": [
6
6
  "coin selection"