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

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,7 @@ 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 types_1 = require("./types");
36
37
  const getBalance = async (balances) => {
37
38
  const value = await (0, exports.assetsToValue)(balances);
38
39
  return Buffer.from(value.to_bytes(), 'hex').toString('hex');
@@ -85,9 +86,16 @@ const getUtxos = async (address, utxos, amount) => {
85
86
  return converted === null || converted === void 0 ? void 0 : converted.map(utxo => Buffer.from(utxo.to_bytes(), 'hex').toString('hex'));
86
87
  };
87
88
  const convertCborTxToEncodeTx = async (txHex, utxos, addresses, changeAddress) => {
88
- var _a, _b;
89
- const tx = CardanoWasm.Transaction.from_bytes(Buffer.from(txHex, 'hex'));
90
- const body = tx.body();
89
+ var _a, _b, _c, _d;
90
+ let body;
91
+ console.log('CARDANO LOCAL_VERSION : 1');
92
+ try {
93
+ const tx = CardanoWasm.Transaction.from_bytes(Buffer.from(txHex, 'hex'));
94
+ body = tx.body();
95
+ }
96
+ catch (_e) {
97
+ body = CardanoWasm.TransactionBody.from_bytes(Buffer.from(txHex, 'hex'));
98
+ }
91
99
  // Fee
92
100
  const fee = body.fee().to_str();
93
101
  const totalFeeInNative = new bignumber_js_1.default(fee).shiftedBy(-1 * 6).toFixed();
@@ -101,8 +109,71 @@ const convertCborTxToEncodeTx = async (txHex, utxos, addresses, changeAddress) =
101
109
  const index = input.index();
102
110
  inputs.push({ tx_hash: txHash, tx_index: index });
103
111
  const utxo = utxos.find(utxo => utxo.tx_hash === txHash && +utxo.tx_index === +index);
104
- encodeInputs.push(utxo);
112
+ // Only push matched UTXOs, skip external inputs (e.g., from DeFi protocols)
113
+ if (utxo) {
114
+ encodeInputs.push(utxo);
115
+ }
116
+ }
117
+ // Parse certificates (for staking transactions)
118
+ const certificates = [];
119
+ let poolId;
120
+ const certs = body.certs();
121
+ if (certs) {
122
+ const certsLen = certs.len();
123
+ for (let i = 0; i < certsLen; i++) {
124
+ const cert = certs.get(i);
125
+ const certKind = cert.kind();
126
+ // Stake Registration (kind = 0)
127
+ if (certKind === CardanoWasm.CertificateKind.StakeRegistration) {
128
+ const stakeReg = cert.as_stake_registration();
129
+ if (stakeReg) {
130
+ const stakeCredential = stakeReg.stake_credential();
131
+ const keyHash = stakeCredential.to_keyhash();
132
+ certificates.push({
133
+ type: types_1.CardanoCertificateType.STAKE_REGISTRATION,
134
+ stakeCredential: keyHash
135
+ ? Buffer.from(keyHash.to_bytes(), 'hex').toString('hex')
136
+ : undefined,
137
+ });
138
+ }
139
+ }
140
+ // Stake Deregistration (kind = 1)
141
+ if (certKind === CardanoWasm.CertificateKind.StakeDeregistration) {
142
+ const stakeDeReg = cert.as_stake_deregistration();
143
+ if (stakeDeReg) {
144
+ const stakeCredential = stakeDeReg.stake_credential();
145
+ const keyHash = stakeCredential.to_keyhash();
146
+ certificates.push({
147
+ type: types_1.CardanoCertificateType.STAKE_DEREGISTRATION,
148
+ stakeCredential: keyHash
149
+ ? Buffer.from(keyHash.to_bytes(), 'hex').toString('hex')
150
+ : undefined,
151
+ });
152
+ }
153
+ }
154
+ // Stake Delegation (kind = 2)
155
+ if (certKind === CardanoWasm.CertificateKind.StakeDelegation) {
156
+ const stakeDelegation = cert.as_stake_delegation();
157
+ if (stakeDelegation) {
158
+ const stakeCredential = stakeDelegation.stake_credential();
159
+ const keyHash = stakeCredential.to_keyhash();
160
+ const poolKeyHash = stakeDelegation.pool_keyhash();
161
+ poolId = Buffer.from(poolKeyHash.to_bytes(), 'hex').toString('hex');
162
+ certificates.push({
163
+ type: types_1.CardanoCertificateType.STAKE_DELEGATION,
164
+ stakeCredential: keyHash
165
+ ? Buffer.from(keyHash.to_bytes(), 'hex').toString('hex')
166
+ : undefined,
167
+ poolKeyHash: poolId,
168
+ });
169
+ }
170
+ }
171
+ }
105
172
  }
173
+ const isStakingTx = certificates.length > 0;
174
+ const stakingInfo = isStakingTx
175
+ ? { isStakingTx: true, certificates, poolId }
176
+ : undefined;
106
177
  // outputs txs
107
178
  const outputs = [];
108
179
  const outputsLen = body.outputs().len();
@@ -145,10 +216,18 @@ const convertCborTxToEncodeTx = async (txHex, utxos, addresses, changeAddress) =
145
216
  isChange,
146
217
  });
147
218
  }
148
- const totalSpent = bignumber_js_1.default.sum(...outputs.map(o => o.amount)).toFixed();
219
+ const totalSpent = outputs.length > 0
220
+ ? bignumber_js_1.default.sum(...outputs.map(o => o.amount)).toFixed()
221
+ : '0';
149
222
  const token = (_b = (_a = outputs
150
223
  .filter(o => !addresses.includes(o.address))
151
224
  .find(o => o.assets.length > 0)) === null || _a === void 0 ? void 0 : _a.assets) === null || _b === void 0 ? void 0 : _b[0].unit;
225
+ // Determine 'from' address: prefer matched input, fallback to changeAddress
226
+ const fromAddress = ((_c = encodeInputs[0]) === null || _c === void 0 ? void 0 : _c.address) || changeAddress.address || '';
227
+ // For staking transactions, 'to' can be the pool id or empty
228
+ const toAddress = isStakingTx
229
+ ? poolId || ''
230
+ : ((_d = outputs[0]) === null || _d === void 0 ? void 0 : _d.address) || '';
152
231
  const encodedTx = {
153
232
  inputs: encodeInputs.map(input => (Object.assign(Object.assign({}, input), { txHash: input.tx_hash, outputIndex: input.tx_index }))),
154
233
  outputs,
@@ -156,8 +235,8 @@ const convertCborTxToEncodeTx = async (txHex, utxos, addresses, changeAddress) =
156
235
  totalSpent,
157
236
  totalFeeInNative,
158
237
  transferInfo: {
159
- from: encodeInputs[0].address,
160
- to: outputs[0].address,
238
+ from: fromAddress,
239
+ to: toAddress,
161
240
  amount: totalSpent,
162
241
  token,
163
242
  },
@@ -170,6 +249,7 @@ const convertCborTxToEncodeTx = async (txHex, utxos, addresses, changeAddress) =
170
249
  rawTxHex: txHex,
171
250
  },
172
251
  signOnly: true,
252
+ staking: stakingInfo,
173
253
  };
174
254
  console.log('Cardano DApp EncodedTx: ', encodedTx);
175
255
  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,7 @@ 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 { CardanoCertificateType, } from './types';
7
8
  const getBalance = async (balances) => {
8
9
  const value = await assetsToValue(balances);
9
10
  return Buffer.from(value.to_bytes(), 'hex').toString('hex');
@@ -55,9 +56,16 @@ const getUtxos = async (address, utxos, amount) => {
55
56
  return converted === null || converted === void 0 ? void 0 : converted.map(utxo => Buffer.from(utxo.to_bytes(), 'hex').toString('hex'));
56
57
  };
57
58
  const convertCborTxToEncodeTx = async (txHex, utxos, addresses, changeAddress) => {
58
- var _a, _b;
59
- const tx = CardanoWasm.Transaction.from_bytes(Buffer.from(txHex, 'hex'));
60
- const body = tx.body();
59
+ var _a, _b, _c, _d;
60
+ let body;
61
+ console.log('CARDANO LOCAL_VERSION : 1');
62
+ try {
63
+ const tx = CardanoWasm.Transaction.from_bytes(Buffer.from(txHex, 'hex'));
64
+ body = tx.body();
65
+ }
66
+ catch (_e) {
67
+ body = CardanoWasm.TransactionBody.from_bytes(Buffer.from(txHex, 'hex'));
68
+ }
61
69
  // Fee
62
70
  const fee = body.fee().to_str();
63
71
  const totalFeeInNative = new BigNumber(fee).shiftedBy(-1 * 6).toFixed();
@@ -71,8 +79,71 @@ const convertCborTxToEncodeTx = async (txHex, utxos, addresses, changeAddress) =
71
79
  const index = input.index();
72
80
  inputs.push({ tx_hash: txHash, tx_index: index });
73
81
  const utxo = utxos.find(utxo => utxo.tx_hash === txHash && +utxo.tx_index === +index);
74
- encodeInputs.push(utxo);
82
+ // Only push matched UTXOs, skip external inputs (e.g., from DeFi protocols)
83
+ if (utxo) {
84
+ encodeInputs.push(utxo);
85
+ }
86
+ }
87
+ // Parse certificates (for staking transactions)
88
+ const certificates = [];
89
+ let poolId;
90
+ const certs = body.certs();
91
+ if (certs) {
92
+ const certsLen = certs.len();
93
+ for (let i = 0; i < certsLen; i++) {
94
+ const cert = certs.get(i);
95
+ const certKind = cert.kind();
96
+ // Stake Registration (kind = 0)
97
+ if (certKind === CardanoWasm.CertificateKind.StakeRegistration) {
98
+ const stakeReg = cert.as_stake_registration();
99
+ if (stakeReg) {
100
+ const stakeCredential = stakeReg.stake_credential();
101
+ const keyHash = stakeCredential.to_keyhash();
102
+ certificates.push({
103
+ type: CardanoCertificateType.STAKE_REGISTRATION,
104
+ stakeCredential: keyHash
105
+ ? Buffer.from(keyHash.to_bytes(), 'hex').toString('hex')
106
+ : undefined,
107
+ });
108
+ }
109
+ }
110
+ // Stake Deregistration (kind = 1)
111
+ if (certKind === CardanoWasm.CertificateKind.StakeDeregistration) {
112
+ const stakeDeReg = cert.as_stake_deregistration();
113
+ if (stakeDeReg) {
114
+ const stakeCredential = stakeDeReg.stake_credential();
115
+ const keyHash = stakeCredential.to_keyhash();
116
+ certificates.push({
117
+ type: CardanoCertificateType.STAKE_DEREGISTRATION,
118
+ stakeCredential: keyHash
119
+ ? Buffer.from(keyHash.to_bytes(), 'hex').toString('hex')
120
+ : undefined,
121
+ });
122
+ }
123
+ }
124
+ // Stake Delegation (kind = 2)
125
+ if (certKind === CardanoWasm.CertificateKind.StakeDelegation) {
126
+ const stakeDelegation = cert.as_stake_delegation();
127
+ if (stakeDelegation) {
128
+ const stakeCredential = stakeDelegation.stake_credential();
129
+ const keyHash = stakeCredential.to_keyhash();
130
+ const poolKeyHash = stakeDelegation.pool_keyhash();
131
+ poolId = Buffer.from(poolKeyHash.to_bytes(), 'hex').toString('hex');
132
+ certificates.push({
133
+ type: CardanoCertificateType.STAKE_DELEGATION,
134
+ stakeCredential: keyHash
135
+ ? Buffer.from(keyHash.to_bytes(), 'hex').toString('hex')
136
+ : undefined,
137
+ poolKeyHash: poolId,
138
+ });
139
+ }
140
+ }
141
+ }
75
142
  }
143
+ const isStakingTx = certificates.length > 0;
144
+ const stakingInfo = isStakingTx
145
+ ? { isStakingTx: true, certificates, poolId }
146
+ : undefined;
76
147
  // outputs txs
77
148
  const outputs = [];
78
149
  const outputsLen = body.outputs().len();
@@ -115,10 +186,18 @@ const convertCborTxToEncodeTx = async (txHex, utxos, addresses, changeAddress) =
115
186
  isChange,
116
187
  });
117
188
  }
118
- const totalSpent = BigNumber.sum(...outputs.map(o => o.amount)).toFixed();
189
+ const totalSpent = outputs.length > 0
190
+ ? BigNumber.sum(...outputs.map(o => o.amount)).toFixed()
191
+ : '0';
119
192
  const token = (_b = (_a = outputs
120
193
  .filter(o => !addresses.includes(o.address))
121
194
  .find(o => o.assets.length > 0)) === null || _a === void 0 ? void 0 : _a.assets) === null || _b === void 0 ? void 0 : _b[0].unit;
195
+ // Determine 'from' address: prefer matched input, fallback to changeAddress
196
+ const fromAddress = ((_c = encodeInputs[0]) === null || _c === void 0 ? void 0 : _c.address) || changeAddress.address || '';
197
+ // For staking transactions, 'to' can be the pool id or empty
198
+ const toAddress = isStakingTx
199
+ ? poolId || ''
200
+ : ((_d = outputs[0]) === null || _d === void 0 ? void 0 : _d.address) || '';
122
201
  const encodedTx = {
123
202
  inputs: encodeInputs.map(input => (Object.assign(Object.assign({}, input), { txHash: input.tx_hash, outputIndex: input.tx_index }))),
124
203
  outputs,
@@ -126,8 +205,8 @@ const convertCborTxToEncodeTx = async (txHex, utxos, addresses, changeAddress) =
126
205
  totalSpent,
127
206
  totalFeeInNative,
128
207
  transferInfo: {
129
- from: encodeInputs[0].address,
130
- to: outputs[0].address,
208
+ from: fromAddress,
209
+ to: toAddress,
131
210
  amount: totalSpent,
132
211
  token,
133
212
  },
@@ -140,6 +219,7 @@ const convertCborTxToEncodeTx = async (txHex, utxos, addresses, changeAddress) =
140
219
  rawTxHex: txHex,
141
220
  },
142
221
  signOnly: true,
222
+ staking: stakingInfo,
143
223
  };
144
224
  console.log('Cardano DApp EncodedTx: ', encodedTx);
145
225
  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.5",
3
+ "version": "1.1.7",
4
4
  "description": "",
5
5
  "keywords": [
6
6
  "coin selection"