@onekeyfe/cardano-coin-selection-asmjs 1.1.6 → 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,13 +86,14 @@ 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
+ var _a, _b, _c, _d;
89
90
  let body;
91
+ console.log('CARDANO LOCAL_VERSION : 1');
90
92
  try {
91
93
  const tx = CardanoWasm.Transaction.from_bytes(Buffer.from(txHex, 'hex'));
92
94
  body = tx.body();
93
95
  }
94
- catch (_c) {
96
+ catch (_e) {
95
97
  body = CardanoWasm.TransactionBody.from_bytes(Buffer.from(txHex, 'hex'));
96
98
  }
97
99
  // Fee
@@ -107,8 +109,71 @@ const convertCborTxToEncodeTx = async (txHex, utxos, addresses, changeAddress) =
107
109
  const index = input.index();
108
110
  inputs.push({ tx_hash: txHash, tx_index: index });
109
111
  const utxo = utxos.find(utxo => utxo.tx_hash === txHash && +utxo.tx_index === +index);
110
- 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
+ }
111
172
  }
173
+ const isStakingTx = certificates.length > 0;
174
+ const stakingInfo = isStakingTx
175
+ ? { isStakingTx: true, certificates, poolId }
176
+ : undefined;
112
177
  // outputs txs
113
178
  const outputs = [];
114
179
  const outputsLen = body.outputs().len();
@@ -151,10 +216,18 @@ const convertCborTxToEncodeTx = async (txHex, utxos, addresses, changeAddress) =
151
216
  isChange,
152
217
  });
153
218
  }
154
- 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';
155
222
  const token = (_b = (_a = outputs
156
223
  .filter(o => !addresses.includes(o.address))
157
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) || '';
158
231
  const encodedTx = {
159
232
  inputs: encodeInputs.map(input => (Object.assign(Object.assign({}, input), { txHash: input.tx_hash, outputIndex: input.tx_index }))),
160
233
  outputs,
@@ -162,8 +235,8 @@ const convertCborTxToEncodeTx = async (txHex, utxos, addresses, changeAddress) =
162
235
  totalSpent,
163
236
  totalFeeInNative,
164
237
  transferInfo: {
165
- from: encodeInputs[0].address,
166
- to: outputs[0].address,
238
+ from: fromAddress,
239
+ to: toAddress,
167
240
  amount: totalSpent,
168
241
  token,
169
242
  },
@@ -176,6 +249,7 @@ const convertCborTxToEncodeTx = async (txHex, utxos, addresses, changeAddress) =
176
249
  rawTxHex: txHex,
177
250
  },
178
251
  signOnly: true,
252
+ staking: stakingInfo,
179
253
  };
180
254
  console.log('Cardano DApp EncodedTx: ', encodedTx);
181
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,13 +56,14 @@ 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
+ var _a, _b, _c, _d;
59
60
  let body;
61
+ console.log('CARDANO LOCAL_VERSION : 1');
60
62
  try {
61
63
  const tx = CardanoWasm.Transaction.from_bytes(Buffer.from(txHex, 'hex'));
62
64
  body = tx.body();
63
65
  }
64
- catch (_c) {
66
+ catch (_e) {
65
67
  body = CardanoWasm.TransactionBody.from_bytes(Buffer.from(txHex, 'hex'));
66
68
  }
67
69
  // Fee
@@ -77,8 +79,71 @@ const convertCborTxToEncodeTx = async (txHex, utxos, addresses, changeAddress) =
77
79
  const index = input.index();
78
80
  inputs.push({ tx_hash: txHash, tx_index: index });
79
81
  const utxo = utxos.find(utxo => utxo.tx_hash === txHash && +utxo.tx_index === +index);
80
- 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
+ }
81
142
  }
143
+ const isStakingTx = certificates.length > 0;
144
+ const stakingInfo = isStakingTx
145
+ ? { isStakingTx: true, certificates, poolId }
146
+ : undefined;
82
147
  // outputs txs
83
148
  const outputs = [];
84
149
  const outputsLen = body.outputs().len();
@@ -121,10 +186,18 @@ const convertCborTxToEncodeTx = async (txHex, utxos, addresses, changeAddress) =
121
186
  isChange,
122
187
  });
123
188
  }
124
- 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';
125
192
  const token = (_b = (_a = outputs
126
193
  .filter(o => !addresses.includes(o.address))
127
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) || '';
128
201
  const encodedTx = {
129
202
  inputs: encodeInputs.map(input => (Object.assign(Object.assign({}, input), { txHash: input.tx_hash, outputIndex: input.tx_index }))),
130
203
  outputs,
@@ -132,8 +205,8 @@ const convertCborTxToEncodeTx = async (txHex, utxos, addresses, changeAddress) =
132
205
  totalSpent,
133
206
  totalFeeInNative,
134
207
  transferInfo: {
135
- from: encodeInputs[0].address,
136
- to: outputs[0].address,
208
+ from: fromAddress,
209
+ to: toAddress,
137
210
  amount: totalSpent,
138
211
  token,
139
212
  },
@@ -146,6 +219,7 @@ const convertCborTxToEncodeTx = async (txHex, utxos, addresses, changeAddress) =
146
219
  rawTxHex: txHex,
147
220
  },
148
221
  signOnly: true,
222
+ staking: stakingInfo,
149
223
  };
150
224
  console.log('Cardano DApp EncodedTx: ', encodedTx);
151
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.6",
3
+ "version": "1.1.7",
4
4
  "description": "",
5
5
  "keywords": [
6
6
  "coin selection"