@onekeyfe/cardano-coin-selection-asmjs 1.1.6 → 1.1.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.
@@ -33,6 +33,9 @@ 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");
38
+ const types_1 = require("./types");
36
39
  const getBalance = async (balances) => {
37
40
  const value = await (0, exports.assetsToValue)(balances);
38
41
  return Buffer.from(value.to_bytes(), 'hex').toString('hex');
@@ -85,13 +88,14 @@ const getUtxos = async (address, utxos, amount) => {
85
88
  return converted === null || converted === void 0 ? void 0 : converted.map(utxo => Buffer.from(utxo.to_bytes(), 'hex').toString('hex'));
86
89
  };
87
90
  const convertCborTxToEncodeTx = async (txHex, utxos, addresses, changeAddress) => {
88
- var _a, _b;
91
+ var _a, _b, _c, _d;
89
92
  let body;
93
+ console.log('CARDANO LOCAL_VERSION : 1');
90
94
  try {
91
95
  const tx = CardanoWasm.Transaction.from_bytes(Buffer.from(txHex, 'hex'));
92
96
  body = tx.body();
93
97
  }
94
- catch (_c) {
98
+ catch (_e) {
95
99
  body = CardanoWasm.TransactionBody.from_bytes(Buffer.from(txHex, 'hex'));
96
100
  }
97
101
  // Fee
@@ -107,11 +111,79 @@ const convertCborTxToEncodeTx = async (txHex, utxos, addresses, changeAddress) =
107
111
  const index = input.index();
108
112
  inputs.push({ tx_hash: txHash, tx_index: index });
109
113
  const utxo = utxos.find(utxo => utxo.tx_hash === txHash && +utxo.tx_index === +index);
110
- encodeInputs.push(utxo);
114
+ // Only push matched UTXOs, skip external inputs (e.g., from DeFi protocols)
115
+ if (utxo) {
116
+ encodeInputs.push(utxo);
117
+ }
118
+ }
119
+ // Parse certificates (for staking transactions)
120
+ const certificates = [];
121
+ let poolId;
122
+ const certs = body.certs();
123
+ if (certs) {
124
+ const certsLen = certs.len();
125
+ for (let i = 0; i < certsLen; i++) {
126
+ const cert = certs.get(i);
127
+ const certKind = cert.kind();
128
+ // Stake Registration (kind = 0)
129
+ if (certKind === CardanoWasm.CertificateKind.StakeRegistration) {
130
+ const stakeReg = cert.as_stake_registration();
131
+ if (stakeReg) {
132
+ const stakeCredential = stakeReg.stake_credential();
133
+ const keyHash = stakeCredential.to_keyhash();
134
+ certificates.push({
135
+ type: types_1.CardanoCertificateType.STAKE_REGISTRATION,
136
+ stakeCredential: keyHash
137
+ ? Buffer.from(keyHash.to_bytes(), 'hex').toString('hex')
138
+ : undefined,
139
+ });
140
+ }
141
+ }
142
+ // Stake Deregistration (kind = 1)
143
+ if (certKind === CardanoWasm.CertificateKind.StakeDeregistration) {
144
+ const stakeDeReg = cert.as_stake_deregistration();
145
+ if (stakeDeReg) {
146
+ const stakeCredential = stakeDeReg.stake_credential();
147
+ const keyHash = stakeCredential.to_keyhash();
148
+ certificates.push({
149
+ type: types_1.CardanoCertificateType.STAKE_DEREGISTRATION,
150
+ stakeCredential: keyHash
151
+ ? Buffer.from(keyHash.to_bytes(), 'hex').toString('hex')
152
+ : undefined,
153
+ });
154
+ }
155
+ }
156
+ // Stake Delegation (kind = 2)
157
+ if (certKind === CardanoWasm.CertificateKind.StakeDelegation) {
158
+ const stakeDelegation = cert.as_stake_delegation();
159
+ if (stakeDelegation) {
160
+ const stakeCredential = stakeDelegation.stake_credential();
161
+ const keyHash = stakeCredential.to_keyhash();
162
+ const poolKeyHash = stakeDelegation.pool_keyhash();
163
+ poolId = Buffer.from(poolKeyHash.to_bytes(), 'hex').toString('hex');
164
+ certificates.push({
165
+ type: types_1.CardanoCertificateType.STAKE_DELEGATION,
166
+ stakeCredential: keyHash
167
+ ? Buffer.from(keyHash.to_bytes(), 'hex').toString('hex')
168
+ : undefined,
169
+ poolKeyHash: poolId,
170
+ });
171
+ }
172
+ }
173
+ }
111
174
  }
175
+ const isStakingTx = certificates.length > 0;
176
+ const stakingInfo = isStakingTx
177
+ ? { isStakingTx: true, certificates, poolId }
178
+ : undefined;
112
179
  // outputs txs
113
180
  const outputs = [];
114
181
  const outputsLen = body.outputs().len();
182
+ // All valid transactions must have at least one output
183
+ // Empty outputs means insufficient funds to cover the transaction
184
+ if (outputsLen === 0) {
185
+ throw new errors_1.CoinSelectionError(constants_1.ERROR.UTXO_BALANCE_INSUFFICIENT);
186
+ }
115
187
  for (let i = 0; i < outputsLen; i++) {
116
188
  const output = body.outputs().get(i);
117
189
  const address = output.address().to_bech32();
@@ -155,6 +227,12 @@ const convertCborTxToEncodeTx = async (txHex, utxos, addresses, changeAddress) =
155
227
  const token = (_b = (_a = outputs
156
228
  .filter(o => !addresses.includes(o.address))
157
229
  .find(o => o.assets.length > 0)) === null || _a === void 0 ? void 0 : _a.assets) === null || _b === void 0 ? void 0 : _b[0].unit;
230
+ // Determine 'from' address: prefer matched input, fallback to changeAddress
231
+ const fromAddress = ((_c = encodeInputs[0]) === null || _c === void 0 ? void 0 : _c.address) || changeAddress.address || '';
232
+ // For staking transactions, 'to' can be the pool id or empty
233
+ const toAddress = isStakingTx
234
+ ? poolId || ''
235
+ : ((_d = outputs[0]) === null || _d === void 0 ? void 0 : _d.address) || '';
158
236
  const encodedTx = {
159
237
  inputs: encodeInputs.map(input => (Object.assign(Object.assign({}, input), { txHash: input.tx_hash, outputIndex: input.tx_index }))),
160
238
  outputs,
@@ -162,8 +240,8 @@ const convertCborTxToEncodeTx = async (txHex, utxos, addresses, changeAddress) =
162
240
  totalSpent,
163
241
  totalFeeInNative,
164
242
  transferInfo: {
165
- from: encodeInputs[0].address,
166
- to: outputs[0].address,
243
+ from: fromAddress,
244
+ to: toAddress,
167
245
  amount: totalSpent,
168
246
  token,
169
247
  },
@@ -176,6 +254,7 @@ const convertCborTxToEncodeTx = async (txHex, utxos, addresses, changeAddress) =
176
254
  rawTxHex: txHex,
177
255
  },
178
256
  signOnly: true,
257
+ staking: stakingInfo,
179
258
  };
180
259
  console.log('Cardano DApp EncodedTx: ', encodedTx);
181
260
  return encodedTx;
@@ -44,6 +44,7 @@ type ITxInfo = {
44
44
  body: string;
45
45
  hash: string;
46
46
  size: number;
47
+ rawTxHex?: string;
47
48
  };
48
49
  export type IEncodedTxADA = {
49
50
  inputs: IEncodeInput[];
@@ -54,6 +55,7 @@ export type IEncodedTxADA = {
54
55
  transferInfo: ITransferInfo;
55
56
  tx: ITxInfo;
56
57
  signOnly?: boolean;
58
+ staking?: IStakingInfo;
57
59
  };
58
60
  declare enum CardanoAddressType {
59
61
  BASE = 0,
@@ -76,4 +78,20 @@ export type IChangeAddress = {
76
78
  stakingPath: string;
77
79
  };
78
80
  };
81
+ export declare enum CardanoCertificateType {
82
+ STAKE_REGISTRATION = 0,
83
+ STAKE_DEREGISTRATION = 1,
84
+ STAKE_DELEGATION = 2,
85
+ STAKE_POOL_REGISTRATION = 3
86
+ }
87
+ export type IStakingInfo = {
88
+ isStakingTx: boolean;
89
+ certificates: ICardanoCertificate[];
90
+ poolId?: string;
91
+ };
92
+ export type ICardanoCertificate = {
93
+ type: CardanoCertificateType;
94
+ stakeCredential?: string;
95
+ poolKeyHash?: string;
96
+ };
79
97
  export {};
@@ -1,5 +1,6 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.CardanoCertificateType = void 0;
3
4
  var CardanoAddressType;
4
5
  (function (CardanoAddressType) {
5
6
  CardanoAddressType[CardanoAddressType["BASE"] = 0] = "BASE";
@@ -14,3 +15,11 @@ var CardanoAddressType;
14
15
  CardanoAddressType[CardanoAddressType["REWARD"] = 14] = "REWARD";
15
16
  CardanoAddressType[CardanoAddressType["REWARD_SCRIPT"] = 15] = "REWARD_SCRIPT";
16
17
  })(CardanoAddressType || (CardanoAddressType = {}));
18
+ // Staking certificate types
19
+ var CardanoCertificateType;
20
+ (function (CardanoCertificateType) {
21
+ CardanoCertificateType[CardanoCertificateType["STAKE_REGISTRATION"] = 0] = "STAKE_REGISTRATION";
22
+ CardanoCertificateType[CardanoCertificateType["STAKE_DEREGISTRATION"] = 1] = "STAKE_DEREGISTRATION";
23
+ CardanoCertificateType[CardanoCertificateType["STAKE_DELEGATION"] = 2] = "STAKE_DELEGATION";
24
+ CardanoCertificateType[CardanoCertificateType["STAKE_POOL_REGISTRATION"] = 3] = "STAKE_POOL_REGISTRATION";
25
+ })(CardanoCertificateType || (exports.CardanoCertificateType = CardanoCertificateType = {}));
@@ -4,6 +4,9 @@ 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';
9
+ import { CardanoCertificateType, } from './types';
7
10
  const getBalance = async (balances) => {
8
11
  const value = await assetsToValue(balances);
9
12
  return Buffer.from(value.to_bytes(), 'hex').toString('hex');
@@ -55,13 +58,14 @@ const getUtxos = async (address, utxos, amount) => {
55
58
  return converted === null || converted === void 0 ? void 0 : converted.map(utxo => Buffer.from(utxo.to_bytes(), 'hex').toString('hex'));
56
59
  };
57
60
  const convertCborTxToEncodeTx = async (txHex, utxos, addresses, changeAddress) => {
58
- var _a, _b;
61
+ var _a, _b, _c, _d;
59
62
  let body;
63
+ console.log('CARDANO LOCAL_VERSION : 1');
60
64
  try {
61
65
  const tx = CardanoWasm.Transaction.from_bytes(Buffer.from(txHex, 'hex'));
62
66
  body = tx.body();
63
67
  }
64
- catch (_c) {
68
+ catch (_e) {
65
69
  body = CardanoWasm.TransactionBody.from_bytes(Buffer.from(txHex, 'hex'));
66
70
  }
67
71
  // Fee
@@ -77,11 +81,79 @@ const convertCborTxToEncodeTx = async (txHex, utxos, addresses, changeAddress) =
77
81
  const index = input.index();
78
82
  inputs.push({ tx_hash: txHash, tx_index: index });
79
83
  const utxo = utxos.find(utxo => utxo.tx_hash === txHash && +utxo.tx_index === +index);
80
- encodeInputs.push(utxo);
84
+ // Only push matched UTXOs, skip external inputs (e.g., from DeFi protocols)
85
+ if (utxo) {
86
+ encodeInputs.push(utxo);
87
+ }
88
+ }
89
+ // Parse certificates (for staking transactions)
90
+ const certificates = [];
91
+ let poolId;
92
+ const certs = body.certs();
93
+ if (certs) {
94
+ const certsLen = certs.len();
95
+ for (let i = 0; i < certsLen; i++) {
96
+ const cert = certs.get(i);
97
+ const certKind = cert.kind();
98
+ // Stake Registration (kind = 0)
99
+ if (certKind === CardanoWasm.CertificateKind.StakeRegistration) {
100
+ const stakeReg = cert.as_stake_registration();
101
+ if (stakeReg) {
102
+ const stakeCredential = stakeReg.stake_credential();
103
+ const keyHash = stakeCredential.to_keyhash();
104
+ certificates.push({
105
+ type: CardanoCertificateType.STAKE_REGISTRATION,
106
+ stakeCredential: keyHash
107
+ ? Buffer.from(keyHash.to_bytes(), 'hex').toString('hex')
108
+ : undefined,
109
+ });
110
+ }
111
+ }
112
+ // Stake Deregistration (kind = 1)
113
+ if (certKind === CardanoWasm.CertificateKind.StakeDeregistration) {
114
+ const stakeDeReg = cert.as_stake_deregistration();
115
+ if (stakeDeReg) {
116
+ const stakeCredential = stakeDeReg.stake_credential();
117
+ const keyHash = stakeCredential.to_keyhash();
118
+ certificates.push({
119
+ type: CardanoCertificateType.STAKE_DEREGISTRATION,
120
+ stakeCredential: keyHash
121
+ ? Buffer.from(keyHash.to_bytes(), 'hex').toString('hex')
122
+ : undefined,
123
+ });
124
+ }
125
+ }
126
+ // Stake Delegation (kind = 2)
127
+ if (certKind === CardanoWasm.CertificateKind.StakeDelegation) {
128
+ const stakeDelegation = cert.as_stake_delegation();
129
+ if (stakeDelegation) {
130
+ const stakeCredential = stakeDelegation.stake_credential();
131
+ const keyHash = stakeCredential.to_keyhash();
132
+ const poolKeyHash = stakeDelegation.pool_keyhash();
133
+ poolId = Buffer.from(poolKeyHash.to_bytes(), 'hex').toString('hex');
134
+ certificates.push({
135
+ type: CardanoCertificateType.STAKE_DELEGATION,
136
+ stakeCredential: keyHash
137
+ ? Buffer.from(keyHash.to_bytes(), 'hex').toString('hex')
138
+ : undefined,
139
+ poolKeyHash: poolId,
140
+ });
141
+ }
142
+ }
143
+ }
81
144
  }
145
+ const isStakingTx = certificates.length > 0;
146
+ const stakingInfo = isStakingTx
147
+ ? { isStakingTx: true, certificates, poolId }
148
+ : undefined;
82
149
  // outputs txs
83
150
  const outputs = [];
84
151
  const outputsLen = body.outputs().len();
152
+ // All valid transactions must have at least one output
153
+ // Empty outputs means insufficient funds to cover the transaction
154
+ if (outputsLen === 0) {
155
+ throw new CoinSelectionError(ERROR.UTXO_BALANCE_INSUFFICIENT);
156
+ }
85
157
  for (let i = 0; i < outputsLen; i++) {
86
158
  const output = body.outputs().get(i);
87
159
  const address = output.address().to_bech32();
@@ -125,6 +197,12 @@ const convertCborTxToEncodeTx = async (txHex, utxos, addresses, changeAddress) =
125
197
  const token = (_b = (_a = outputs
126
198
  .filter(o => !addresses.includes(o.address))
127
199
  .find(o => o.assets.length > 0)) === null || _a === void 0 ? void 0 : _a.assets) === null || _b === void 0 ? void 0 : _b[0].unit;
200
+ // Determine 'from' address: prefer matched input, fallback to changeAddress
201
+ const fromAddress = ((_c = encodeInputs[0]) === null || _c === void 0 ? void 0 : _c.address) || changeAddress.address || '';
202
+ // For staking transactions, 'to' can be the pool id or empty
203
+ const toAddress = isStakingTx
204
+ ? poolId || ''
205
+ : ((_d = outputs[0]) === null || _d === void 0 ? void 0 : _d.address) || '';
128
206
  const encodedTx = {
129
207
  inputs: encodeInputs.map(input => (Object.assign(Object.assign({}, input), { txHash: input.tx_hash, outputIndex: input.tx_index }))),
130
208
  outputs,
@@ -132,8 +210,8 @@ const convertCborTxToEncodeTx = async (txHex, utxos, addresses, changeAddress) =
132
210
  totalSpent,
133
211
  totalFeeInNative,
134
212
  transferInfo: {
135
- from: encodeInputs[0].address,
136
- to: outputs[0].address,
213
+ from: fromAddress,
214
+ to: toAddress,
137
215
  amount: totalSpent,
138
216
  token,
139
217
  },
@@ -146,6 +224,7 @@ const convertCborTxToEncodeTx = async (txHex, utxos, addresses, changeAddress) =
146
224
  rawTxHex: txHex,
147
225
  },
148
226
  signOnly: true,
227
+ staking: stakingInfo,
149
228
  };
150
229
  console.log('Cardano DApp EncodedTx: ', encodedTx);
151
230
  return encodedTx;
@@ -44,6 +44,7 @@ type ITxInfo = {
44
44
  body: string;
45
45
  hash: string;
46
46
  size: number;
47
+ rawTxHex?: string;
47
48
  };
48
49
  export type IEncodedTxADA = {
49
50
  inputs: IEncodeInput[];
@@ -54,6 +55,7 @@ export type IEncodedTxADA = {
54
55
  transferInfo: ITransferInfo;
55
56
  tx: ITxInfo;
56
57
  signOnly?: boolean;
58
+ staking?: IStakingInfo;
57
59
  };
58
60
  declare enum CardanoAddressType {
59
61
  BASE = 0,
@@ -76,4 +78,20 @@ export type IChangeAddress = {
76
78
  stakingPath: string;
77
79
  };
78
80
  };
81
+ export declare enum CardanoCertificateType {
82
+ STAKE_REGISTRATION = 0,
83
+ STAKE_DEREGISTRATION = 1,
84
+ STAKE_DELEGATION = 2,
85
+ STAKE_POOL_REGISTRATION = 3
86
+ }
87
+ export type IStakingInfo = {
88
+ isStakingTx: boolean;
89
+ certificates: ICardanoCertificate[];
90
+ poolId?: string;
91
+ };
92
+ export type ICardanoCertificate = {
93
+ type: CardanoCertificateType;
94
+ stakeCredential?: string;
95
+ poolKeyHash?: string;
96
+ };
79
97
  export {};
@@ -12,4 +12,11 @@ var CardanoAddressType;
12
12
  CardanoAddressType[CardanoAddressType["REWARD"] = 14] = "REWARD";
13
13
  CardanoAddressType[CardanoAddressType["REWARD_SCRIPT"] = 15] = "REWARD_SCRIPT";
14
14
  })(CardanoAddressType || (CardanoAddressType = {}));
15
- export {};
15
+ // Staking certificate types
16
+ export var CardanoCertificateType;
17
+ (function (CardanoCertificateType) {
18
+ CardanoCertificateType[CardanoCertificateType["STAKE_REGISTRATION"] = 0] = "STAKE_REGISTRATION";
19
+ CardanoCertificateType[CardanoCertificateType["STAKE_DEREGISTRATION"] = 1] = "STAKE_DEREGISTRATION";
20
+ CardanoCertificateType[CardanoCertificateType["STAKE_DELEGATION"] = 2] = "STAKE_DELEGATION";
21
+ CardanoCertificateType[CardanoCertificateType["STAKE_POOL_REGISTRATION"] = 3] = "STAKE_POOL_REGISTRATION";
22
+ })(CardanoCertificateType || (CardanoCertificateType = {}));
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@onekeyfe/cardano-coin-selection-asmjs",
3
- "version": "1.1.6",
3
+ "version": "1.1.8",
4
4
  "description": "",
5
5
  "keywords": [
6
6
  "coin selection"