@onekeyfe/cardano-coin-selection-asmjs 1.1.10-alpha.6 → 1.1.10-alpha.8

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.
@@ -91,7 +91,7 @@ const convertCborTxToEncodeTx = async ({ txHex, utxos, addresses, changeAddress,
91
91
  var _a, _b, _c, _d;
92
92
  let body;
93
93
  let rawTxHex;
94
- console.log('CARDANO LOCAL_VERSION : 1');
94
+ // console.log('CARDANO LOCAL_VERSION : 333====>>>');
95
95
  try {
96
96
  const tx = CardanoWasm.Transaction.from_bytes(Buffer.from(txHex, 'hex'));
97
97
  body = tx.body();
@@ -108,22 +108,7 @@ const convertCborTxToEncodeTx = async ({ txHex, utxos, addresses, changeAddress,
108
108
  // Fee
109
109
  const fee = body.fee().to_str();
110
110
  const totalFeeInNative = new bignumber_js_1.default(fee).shiftedBy(-1 * 6).toFixed();
111
- // inputs txs
112
- const encodeInputs = [];
113
- const inputs = [];
114
- const inputsLen = body.inputs().len();
115
- for (let i = 0; i < inputsLen; i++) {
116
- const input = body.inputs().get(i);
117
- const txHash = Buffer.from(input.transaction_id().to_bytes(), 'utf8').toString('hex');
118
- const index = input.index();
119
- inputs.push({ tx_hash: txHash, tx_index: index });
120
- const utxo = utxos.find(utxo => utxo.tx_hash === txHash && +utxo.tx_index === +index);
121
- // Only push matched UTXOs, skip external inputs (e.g., from DeFi protocols)
122
- if (utxo) {
123
- encodeInputs.push(utxo);
124
- }
125
- }
126
- // Parse certificates (for staking transactions)
111
+ // Parse certificates first (needed to determine if this is a staking transaction)
127
112
  const certificates = [];
128
113
  let poolId;
129
114
  const certs = body.certs();
@@ -183,12 +168,38 @@ const convertCborTxToEncodeTx = async ({ txHex, utxos, addresses, changeAddress,
183
168
  const stakingInfo = isStakingTx
184
169
  ? { isStakingTx: true, certificates, poolId }
185
170
  : undefined;
171
+ // inputs txs
172
+ const encodeInputs = [];
173
+ const inputs = [];
174
+ const inputsLen = body.inputs().len();
175
+ for (let i = 0; i < inputsLen; i++) {
176
+ const input = body.inputs().get(i);
177
+ const txHash = Buffer.from(input.transaction_id().to_bytes(), 'utf8').toString('hex');
178
+ const index = input.index();
179
+ inputs.push({ tx_hash: txHash, tx_index: index });
180
+ const utxo = utxos.find(utxo => utxo.tx_hash === txHash && +utxo.tx_index === +index);
181
+ // Only push matched UTXOs, skip external inputs (e.g., from DeFi protocols)
182
+ if (utxo) {
183
+ encodeInputs.push(utxo);
184
+ }
185
+ }
186
186
  // outputs txs
187
187
  const outputs = [];
188
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) {
189
+ // Staking transactions (delegation, registration, deregistration) can have empty outputs
190
+ // Regular transactions must have at least one output
191
+ if (outputsLen === 0 && !isStakingTx) {
192
+ console.log('[convertCborTxToEncodeTx] Empty outputs detected, transaction data:', {
193
+ fee,
194
+ totalFeeInNative,
195
+ inputsCount: inputsLen,
196
+ inputs: inputs,
197
+ matchedUtxos: encodeInputs.map(u => ({
198
+ tx_hash: u.tx_hash,
199
+ tx_index: u.tx_index,
200
+ amount: u.amount,
201
+ })),
202
+ });
192
203
  throw new errors_1.CoinSelectionError(constants_1.ERROR.UTXO_BALANCE_INSUFFICIENT);
193
204
  }
194
205
  for (let i = 0; i < outputsLen; i++) {
@@ -230,7 +241,9 @@ const convertCborTxToEncodeTx = async ({ txHex, utxos, addresses, changeAddress,
230
241
  isChange,
231
242
  });
232
243
  }
233
- const totalSpent = bignumber_js_1.default.sum(...outputs.map(o => o.amount)).toFixed();
244
+ const totalSpent = outputs.length > 0
245
+ ? bignumber_js_1.default.sum(...outputs.map(o => o.amount)).toFixed()
246
+ : '0';
234
247
  const token = (_b = (_a = outputs
235
248
  .filter(o => !addresses.includes(o.address))
236
249
  .find(o => o.assets.length > 0)) === null || _a === void 0 ? void 0 : _a.assets) === null || _b === void 0 ? void 0 : _b[0].unit;
@@ -238,7 +251,7 @@ const convertCborTxToEncodeTx = async ({ txHex, utxos, addresses, changeAddress,
238
251
  const fromAddress = ((_c = encodeInputs[0]) === null || _c === void 0 ? void 0 : _c.address) || changeAddress.address || '';
239
252
  // For staking transactions, 'to' can be the pool id or empty
240
253
  const toAddress = isStakingTx
241
- ? poolId || ''
254
+ ? fromAddress || ''
242
255
  : ((_d = outputs[0]) === null || _d === void 0 ? void 0 : _d.address) || '';
243
256
  const encodedTx = {
244
257
  inputs: encodeInputs.map(input => (Object.assign(Object.assign({}, input), { txHash: input.tx_hash, outputIndex: input.tx_index }))),
@@ -23,6 +23,8 @@ export declare enum CardanoCertificateType {
23
23
  STAKE_DEREGISTRATION = 1,
24
24
  STAKE_DELEGATION = 2,
25
25
  STAKE_POOL_REGISTRATION = 3,
26
+ STAKE_REGISTRATION_CONWAY = 7,
27
+ STAKE_DEREGISTRATION_CONWAY = 8,
26
28
  VOTE_DELEGATION = 9
27
29
  }
28
30
  export declare enum CardanoDRepType {
@@ -53,6 +53,8 @@ var CardanoCertificateType;
53
53
  CardanoCertificateType[CardanoCertificateType["STAKE_DEREGISTRATION"] = 1] = "STAKE_DEREGISTRATION";
54
54
  CardanoCertificateType[CardanoCertificateType["STAKE_DELEGATION"] = 2] = "STAKE_DELEGATION";
55
55
  CardanoCertificateType[CardanoCertificateType["STAKE_POOL_REGISTRATION"] = 3] = "STAKE_POOL_REGISTRATION";
56
+ CardanoCertificateType[CardanoCertificateType["STAKE_REGISTRATION_CONWAY"] = 7] = "STAKE_REGISTRATION_CONWAY";
57
+ CardanoCertificateType[CardanoCertificateType["STAKE_DEREGISTRATION_CONWAY"] = 8] = "STAKE_DEREGISTRATION_CONWAY";
56
58
  CardanoCertificateType[CardanoCertificateType["VOTE_DELEGATION"] = 9] = "VOTE_DELEGATION";
57
59
  })(CardanoCertificateType || (exports.CardanoCertificateType = CardanoCertificateType = {}));
58
60
  var CardanoDRepType;
@@ -205,7 +207,7 @@ const outputsToOneKey = (outputs, changeAddress) => {
205
207
  * @param {Transaction} tx
206
208
  */
207
209
  const txToOneKey = async (rawTx, network, initKeys, xpub, changeAddress) => {
208
- var _a, _b, _c;
210
+ var _a, _b, _c, _d;
209
211
  const keys = generateKeys(initKeys, xpub);
210
212
  const tx = CardanoWasm.Transaction.from_bytes(Buffer.from(rawTx, 'hex'));
211
213
  let signingMode = CardanoTxSigningMode.ORDINARY_TRANSACTION;
@@ -220,8 +222,18 @@ const txToOneKey = async (rawTx, network, initKeys, xpub, changeAddress) => {
220
222
  const certificate = {};
221
223
  const certKind = cert.kind();
222
224
  if (certKind === 0) {
223
- const credential = (_a = cert.as_stake_registration()) === null || _a === void 0 ? void 0 : _a.stake_credential();
224
- certificate.type = CardanoCertificateType.STAKE_REGISTRATION;
225
+ const stakeRegistration = cert.as_stake_registration();
226
+ const credential = stakeRegistration === null || stakeRegistration === void 0 ? void 0 : stakeRegistration.stake_credential();
227
+ const deposit = (_a = stakeRegistration === null || stakeRegistration === void 0 ? void 0 : stakeRegistration.coin) === null || _a === void 0 ? void 0 : _a.call(stakeRegistration);
228
+ if (deposit) {
229
+ // Conway era - reg_cert with deposit
230
+ certificate.type = CardanoCertificateType.STAKE_REGISTRATION_CONWAY;
231
+ certificate.deposit = deposit.to_str();
232
+ }
233
+ else {
234
+ // Pre-Conway - stake_registration without deposit
235
+ certificate.type = CardanoCertificateType.STAKE_REGISTRATION;
236
+ }
225
237
  if ((credential === null || credential === void 0 ? void 0 : credential.kind()) === 0) {
226
238
  certificate.path = keys.stake.path;
227
239
  }
@@ -231,8 +243,18 @@ const txToOneKey = async (rawTx, network, initKeys, xpub, changeAddress) => {
231
243
  }
232
244
  }
233
245
  else if (certKind === 1) {
234
- const credential = cert.as_stake_deregistration().stake_credential();
235
- certificate.type = CardanoCertificateType.STAKE_DEREGISTRATION;
246
+ const stakeDeregistration = cert.as_stake_deregistration();
247
+ const credential = stakeDeregistration.stake_credential();
248
+ const deposit = (_b = stakeDeregistration.coin) === null || _b === void 0 ? void 0 : _b.call(stakeDeregistration);
249
+ if (deposit) {
250
+ // Conway era - unreg_cert with deposit
251
+ certificate.type = CardanoCertificateType.STAKE_DEREGISTRATION_CONWAY;
252
+ certificate.deposit = deposit.to_str();
253
+ }
254
+ else {
255
+ // Pre-Conway - stake_deregistration without deposit
256
+ certificate.type = CardanoCertificateType.STAKE_DEREGISTRATION;
257
+ }
236
258
  if (credential.kind() === 0) {
237
259
  certificate.path = keys.stake.path;
238
260
  }
@@ -359,11 +381,11 @@ const txToOneKey = async (rawTx, network, initKeys, xpub, changeAddress) => {
359
381
  };
360
382
  if (drepKind === 0) {
361
383
  // KEY_HASH
362
- certificate.dRep.keyHash = (_b = drep.to_key_hash()) === null || _b === void 0 ? void 0 : _b.to_hex();
384
+ certificate.dRep.keyHash = (_c = drep.to_key_hash()) === null || _c === void 0 ? void 0 : _c.to_hex();
363
385
  }
364
386
  else if (drepKind === 1) {
365
387
  // SCRIPT_HASH
366
- certificate.dRep.scriptHash = (_c = drep.to_script_hash()) === null || _c === void 0 ? void 0 : _c.to_hex();
388
+ certificate.dRep.scriptHash = (_d = drep.to_script_hash()) === null || _d === void 0 ? void 0 : _d.to_hex();
367
389
  }
368
390
  // ABSTAIN (2) and NO_CONFIDENCE (3) don't need additional fields
369
391
  }
@@ -61,7 +61,7 @@ const convertCborTxToEncodeTx = async ({ txHex, utxos, addresses, changeAddress,
61
61
  var _a, _b, _c, _d;
62
62
  let body;
63
63
  let rawTxHex;
64
- console.log('CARDANO LOCAL_VERSION : 1');
64
+ // console.log('CARDANO LOCAL_VERSION : 333====>>>');
65
65
  try {
66
66
  const tx = CardanoWasm.Transaction.from_bytes(Buffer.from(txHex, 'hex'));
67
67
  body = tx.body();
@@ -78,22 +78,7 @@ const convertCborTxToEncodeTx = async ({ txHex, utxos, addresses, changeAddress,
78
78
  // Fee
79
79
  const fee = body.fee().to_str();
80
80
  const totalFeeInNative = new BigNumber(fee).shiftedBy(-1 * 6).toFixed();
81
- // inputs txs
82
- const encodeInputs = [];
83
- const inputs = [];
84
- const inputsLen = body.inputs().len();
85
- for (let i = 0; i < inputsLen; i++) {
86
- const input = body.inputs().get(i);
87
- const txHash = Buffer.from(input.transaction_id().to_bytes(), 'utf8').toString('hex');
88
- const index = input.index();
89
- inputs.push({ tx_hash: txHash, tx_index: index });
90
- const utxo = utxos.find(utxo => utxo.tx_hash === txHash && +utxo.tx_index === +index);
91
- // Only push matched UTXOs, skip external inputs (e.g., from DeFi protocols)
92
- if (utxo) {
93
- encodeInputs.push(utxo);
94
- }
95
- }
96
- // Parse certificates (for staking transactions)
81
+ // Parse certificates first (needed to determine if this is a staking transaction)
97
82
  const certificates = [];
98
83
  let poolId;
99
84
  const certs = body.certs();
@@ -153,12 +138,38 @@ const convertCborTxToEncodeTx = async ({ txHex, utxos, addresses, changeAddress,
153
138
  const stakingInfo = isStakingTx
154
139
  ? { isStakingTx: true, certificates, poolId }
155
140
  : undefined;
141
+ // inputs txs
142
+ const encodeInputs = [];
143
+ const inputs = [];
144
+ const inputsLen = body.inputs().len();
145
+ for (let i = 0; i < inputsLen; i++) {
146
+ const input = body.inputs().get(i);
147
+ const txHash = Buffer.from(input.transaction_id().to_bytes(), 'utf8').toString('hex');
148
+ const index = input.index();
149
+ inputs.push({ tx_hash: txHash, tx_index: index });
150
+ const utxo = utxos.find(utxo => utxo.tx_hash === txHash && +utxo.tx_index === +index);
151
+ // Only push matched UTXOs, skip external inputs (e.g., from DeFi protocols)
152
+ if (utxo) {
153
+ encodeInputs.push(utxo);
154
+ }
155
+ }
156
156
  // outputs txs
157
157
  const outputs = [];
158
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) {
159
+ // Staking transactions (delegation, registration, deregistration) can have empty outputs
160
+ // Regular transactions must have at least one output
161
+ if (outputsLen === 0 && !isStakingTx) {
162
+ console.log('[convertCborTxToEncodeTx] Empty outputs detected, transaction data:', {
163
+ fee,
164
+ totalFeeInNative,
165
+ inputsCount: inputsLen,
166
+ inputs: inputs,
167
+ matchedUtxos: encodeInputs.map(u => ({
168
+ tx_hash: u.tx_hash,
169
+ tx_index: u.tx_index,
170
+ amount: u.amount,
171
+ })),
172
+ });
162
173
  throw new CoinSelectionError(ERROR.UTXO_BALANCE_INSUFFICIENT);
163
174
  }
164
175
  for (let i = 0; i < outputsLen; i++) {
@@ -200,7 +211,9 @@ const convertCborTxToEncodeTx = async ({ txHex, utxos, addresses, changeAddress,
200
211
  isChange,
201
212
  });
202
213
  }
203
- const totalSpent = BigNumber.sum(...outputs.map(o => o.amount)).toFixed();
214
+ const totalSpent = outputs.length > 0
215
+ ? BigNumber.sum(...outputs.map(o => o.amount)).toFixed()
216
+ : '0';
204
217
  const token = (_b = (_a = outputs
205
218
  .filter(o => !addresses.includes(o.address))
206
219
  .find(o => o.assets.length > 0)) === null || _a === void 0 ? void 0 : _a.assets) === null || _b === void 0 ? void 0 : _b[0].unit;
@@ -208,7 +221,7 @@ const convertCborTxToEncodeTx = async ({ txHex, utxos, addresses, changeAddress,
208
221
  const fromAddress = ((_c = encodeInputs[0]) === null || _c === void 0 ? void 0 : _c.address) || changeAddress.address || '';
209
222
  // For staking transactions, 'to' can be the pool id or empty
210
223
  const toAddress = isStakingTx
211
- ? poolId || ''
224
+ ? fromAddress || ''
212
225
  : ((_d = outputs[0]) === null || _d === void 0 ? void 0 : _d.address) || '';
213
226
  const encodedTx = {
214
227
  inputs: encodeInputs.map(input => (Object.assign(Object.assign({}, input), { txHash: input.tx_hash, outputIndex: input.tx_index }))),
@@ -23,6 +23,8 @@ export declare enum CardanoCertificateType {
23
23
  STAKE_DEREGISTRATION = 1,
24
24
  STAKE_DELEGATION = 2,
25
25
  STAKE_POOL_REGISTRATION = 3,
26
+ STAKE_REGISTRATION_CONWAY = 7,
27
+ STAKE_DEREGISTRATION_CONWAY = 8,
26
28
  VOTE_DELEGATION = 9
27
29
  }
28
30
  export declare enum CardanoDRepType {
@@ -27,6 +27,8 @@ export var CardanoCertificateType;
27
27
  CardanoCertificateType[CardanoCertificateType["STAKE_DEREGISTRATION"] = 1] = "STAKE_DEREGISTRATION";
28
28
  CardanoCertificateType[CardanoCertificateType["STAKE_DELEGATION"] = 2] = "STAKE_DELEGATION";
29
29
  CardanoCertificateType[CardanoCertificateType["STAKE_POOL_REGISTRATION"] = 3] = "STAKE_POOL_REGISTRATION";
30
+ CardanoCertificateType[CardanoCertificateType["STAKE_REGISTRATION_CONWAY"] = 7] = "STAKE_REGISTRATION_CONWAY";
31
+ CardanoCertificateType[CardanoCertificateType["STAKE_DEREGISTRATION_CONWAY"] = 8] = "STAKE_DEREGISTRATION_CONWAY";
30
32
  CardanoCertificateType[CardanoCertificateType["VOTE_DELEGATION"] = 9] = "VOTE_DELEGATION";
31
33
  })(CardanoCertificateType || (CardanoCertificateType = {}));
32
34
  export var CardanoDRepType;
@@ -179,7 +181,7 @@ const outputsToOneKey = (outputs, changeAddress) => {
179
181
  * @param {Transaction} tx
180
182
  */
181
183
  export const txToOneKey = async (rawTx, network, initKeys, xpub, changeAddress) => {
182
- var _a, _b, _c;
184
+ var _a, _b, _c, _d;
183
185
  const keys = generateKeys(initKeys, xpub);
184
186
  const tx = CardanoWasm.Transaction.from_bytes(Buffer.from(rawTx, 'hex'));
185
187
  let signingMode = CardanoTxSigningMode.ORDINARY_TRANSACTION;
@@ -194,8 +196,18 @@ export const txToOneKey = async (rawTx, network, initKeys, xpub, changeAddress)
194
196
  const certificate = {};
195
197
  const certKind = cert.kind();
196
198
  if (certKind === 0) {
197
- const credential = (_a = cert.as_stake_registration()) === null || _a === void 0 ? void 0 : _a.stake_credential();
198
- certificate.type = CardanoCertificateType.STAKE_REGISTRATION;
199
+ const stakeRegistration = cert.as_stake_registration();
200
+ const credential = stakeRegistration === null || stakeRegistration === void 0 ? void 0 : stakeRegistration.stake_credential();
201
+ const deposit = (_a = stakeRegistration === null || stakeRegistration === void 0 ? void 0 : stakeRegistration.coin) === null || _a === void 0 ? void 0 : _a.call(stakeRegistration);
202
+ if (deposit) {
203
+ // Conway era - reg_cert with deposit
204
+ certificate.type = CardanoCertificateType.STAKE_REGISTRATION_CONWAY;
205
+ certificate.deposit = deposit.to_str();
206
+ }
207
+ else {
208
+ // Pre-Conway - stake_registration without deposit
209
+ certificate.type = CardanoCertificateType.STAKE_REGISTRATION;
210
+ }
199
211
  if ((credential === null || credential === void 0 ? void 0 : credential.kind()) === 0) {
200
212
  certificate.path = keys.stake.path;
201
213
  }
@@ -205,8 +217,18 @@ export const txToOneKey = async (rawTx, network, initKeys, xpub, changeAddress)
205
217
  }
206
218
  }
207
219
  else if (certKind === 1) {
208
- const credential = cert.as_stake_deregistration().stake_credential();
209
- certificate.type = CardanoCertificateType.STAKE_DEREGISTRATION;
220
+ const stakeDeregistration = cert.as_stake_deregistration();
221
+ const credential = stakeDeregistration.stake_credential();
222
+ const deposit = (_b = stakeDeregistration.coin) === null || _b === void 0 ? void 0 : _b.call(stakeDeregistration);
223
+ if (deposit) {
224
+ // Conway era - unreg_cert with deposit
225
+ certificate.type = CardanoCertificateType.STAKE_DEREGISTRATION_CONWAY;
226
+ certificate.deposit = deposit.to_str();
227
+ }
228
+ else {
229
+ // Pre-Conway - stake_deregistration without deposit
230
+ certificate.type = CardanoCertificateType.STAKE_DEREGISTRATION;
231
+ }
210
232
  if (credential.kind() === 0) {
211
233
  certificate.path = keys.stake.path;
212
234
  }
@@ -333,11 +355,11 @@ export const txToOneKey = async (rawTx, network, initKeys, xpub, changeAddress)
333
355
  };
334
356
  if (drepKind === 0) {
335
357
  // KEY_HASH
336
- certificate.dRep.keyHash = (_b = drep.to_key_hash()) === null || _b === void 0 ? void 0 : _b.to_hex();
358
+ certificate.dRep.keyHash = (_c = drep.to_key_hash()) === null || _c === void 0 ? void 0 : _c.to_hex();
337
359
  }
338
360
  else if (drepKind === 1) {
339
361
  // SCRIPT_HASH
340
- certificate.dRep.scriptHash = (_c = drep.to_script_hash()) === null || _c === void 0 ? void 0 : _c.to_hex();
362
+ certificate.dRep.scriptHash = (_d = drep.to_script_hash()) === null || _d === void 0 ? void 0 : _d.to_hex();
341
363
  }
342
364
  // ABSTAIN (2) and NO_CONFIDENCE (3) don't need additional fields
343
365
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@onekeyfe/cardano-coin-selection-asmjs",
3
- "version": "1.1.10-alpha.6",
3
+ "version": "1.1.10-alpha.8",
4
4
  "description": "",
5
5
  "keywords": [
6
6
  "coin selection"
@@ -29,7 +29,8 @@
29
29
  "type-check": "yarn tsc -p tsconfig.types.json",
30
30
  "test": "yarn run-s 'test:*'",
31
31
  "test:unit": "jest -c ./jest.config.js",
32
- "test:badges": "make-coverage-badge --output-path ./docs/badge-coverage.svg"
32
+ "test:badges": "make-coverage-badge --output-path ./docs/badge-coverage.svg",
33
+ "debug:watcher": "node scripts/sync-to-monorepo.js"
33
34
  },
34
35
  "devDependencies": {
35
36
  "@emurgo/cardano-serialization-lib-nodejs": "13.2.0",
@@ -42,10 +43,13 @@
42
43
  "@typescript-eslint/eslint-plugin": "^8.8.0",
43
44
  "@typescript-eslint/parser": "8.8.0",
44
45
  "bignumber.js": "^9.0.1",
46
+ "chokidar": "^5.0.0",
47
+ "dotenv": "^17.2.3",
45
48
  "eslint": "^9.11.1",
46
49
  "eslint-config-prettier": "^9.1.0",
47
50
  "eslint-plugin-import": "^2.30.0",
48
51
  "eslint-plugin-prettier": "^5.2.1",
52
+ "fs-extra": "^11.3.2",
49
53
  "jest": "^29.7.0",
50
54
  "make-coverage-badge": "^1.2.0",
51
55
  "npm-run-all": "^4.1.5",