@onekeyfe/cardano-coin-selection-asmjs 1.1.1 → 1.1.2

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.
Files changed (46) hide show
  1. package/CHANGELOG.MD +22 -0
  2. package/lib/cjs/constants/index.d.ts +7 -2
  3. package/lib/cjs/constants/index.js +7 -2
  4. package/lib/cjs/methods/largestFirst.js +7 -4
  5. package/lib/cjs/methods/randomImprove.d.ts +1 -1
  6. package/lib/cjs/methods/randomImprove.js +4 -2
  7. package/lib/cjs/types/trezor.d.ts +4 -4
  8. package/lib/cjs/types/trezor.js +1 -1
  9. package/lib/cjs/types/types.d.ts +27 -6
  10. package/lib/cjs/types/types.js +9 -2
  11. package/lib/cjs/utils/common.d.ts +4 -4
  12. package/lib/cjs/utils/common.js +35 -9
  13. package/lib/cjs/utils/errors.d.ts +2 -2
  14. package/lib/cjs/utils/onekey/dapp.js +3 -1
  15. package/lib/cjs/utils/onekey/index.d.ts +2 -2
  16. package/lib/cjs/utils/onekey/signTx.d.ts +1 -1
  17. package/lib/cjs/utils/onekey/signTx.js +4 -2
  18. package/lib/cjs/utils/onekey/txToOneKey.d.ts +2 -2
  19. package/lib/cjs/utils/onekey/txToOneKey.js +4 -4
  20. package/lib/cjs/utils/onekey/types.d.ts +9 -9
  21. package/lib/cjs/utils/trezor/index.d.ts +2 -2
  22. package/lib/cjs/utils/trezor/index.js +2 -1
  23. package/lib/cjs/utils/trezor/transformations.d.ts +5 -1
  24. package/lib/cjs/utils/trezor/transformations.js +50 -1
  25. package/lib/esm/constants/index.d.ts +7 -2
  26. package/lib/esm/constants/index.js +7 -2
  27. package/lib/esm/methods/largestFirst.js +7 -4
  28. package/lib/esm/methods/randomImprove.d.ts +1 -1
  29. package/lib/esm/methods/randomImprove.js +4 -2
  30. package/lib/esm/types/trezor.d.ts +4 -4
  31. package/lib/esm/types/types.d.ts +27 -6
  32. package/lib/esm/types/types.js +7 -0
  33. package/lib/esm/utils/common.d.ts +4 -4
  34. package/lib/esm/utils/common.js +35 -9
  35. package/lib/esm/utils/errors.d.ts +2 -2
  36. package/lib/esm/utils/onekey/dapp.js +3 -1
  37. package/lib/esm/utils/onekey/index.d.ts +2 -2
  38. package/lib/esm/utils/onekey/signTx.d.ts +1 -1
  39. package/lib/esm/utils/onekey/signTx.js +4 -2
  40. package/lib/esm/utils/onekey/txToOneKey.d.ts +2 -2
  41. package/lib/esm/utils/onekey/types.d.ts +9 -9
  42. package/lib/esm/utils/trezor/index.d.ts +2 -2
  43. package/lib/esm/utils/trezor/index.js +2 -2
  44. package/lib/esm/utils/trezor/transformations.d.ts +5 -1
  45. package/lib/esm/utils/trezor/transformations.js +25 -0
  46. package/package.json +20 -16
package/CHANGELOG.MD CHANGED
@@ -7,6 +7,28 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
7
7
 
8
8
  ## Unreleased
9
9
 
10
+ ### Added
11
+
12
+ - Support for voting delegation certificates
13
+
14
+ ### Changed
15
+
16
+ - Upgraded cardano-serialization-lib to Chang-compatible release (v13)
17
+ - All set types are now serialized with a tag "258", which will become mandatory after the next hard fork (HF)
18
+ - Upgraded cardano-serialization-lib to Chang-compatible release (v12)
19
+
20
+ ## [2.2.1] - 2023-11-29
21
+
22
+ ### Fixed
23
+
24
+ - UTXO_BALANCE_INSUFFICIENT while using `setMax` on ADA output
25
+
26
+ ## [2.2.0] - 2023-10-06
27
+
28
+ ### Added
29
+
30
+ - nodejs compatibility
31
+
10
32
  ## [2.1.0] - 2022-11-22
11
33
 
12
34
  ### Fixed
@@ -4,6 +4,9 @@ export declare const CertificateType: {
4
4
  readonly STAKE_DEREGISTRATION: 1;
5
5
  readonly STAKE_DELEGATION: 2;
6
6
  readonly STAKE_POOL_REGISTRATION: 3;
7
+ readonly STAKE_REGISTRATION_CONWAY: 7;
8
+ readonly STAKE_DEREGISTRATION_CONWAY: 8;
9
+ readonly VOTE_DELEGATION: 9;
7
10
  };
8
11
  export declare const ERROR: {
9
12
  readonly UTXO_BALANCE_INSUFFICIENT: {
@@ -26,11 +29,13 @@ export declare const ERROR: {
26
29
  export declare const CARDANO_PARAMS: {
27
30
  readonly PROTOCOL_MAGICS: {
28
31
  readonly mainnet: number;
29
- readonly testnet: number;
32
+ readonly testnet_preprod: number;
33
+ readonly testnet_preview: number;
30
34
  };
31
35
  readonly NETWORK_IDS: {
32
36
  readonly mainnet: number;
33
- readonly testnet: number;
37
+ readonly testnet_preprod: number;
38
+ readonly testnet_preview: number;
34
39
  };
35
40
  readonly COINS_PER_UTXO_BYTE: "4310";
36
41
  readonly MAX_TX_SIZE: 16384;
@@ -30,6 +30,9 @@ exports.CertificateType = {
30
30
  STAKE_DEREGISTRATION: 1,
31
31
  STAKE_DELEGATION: 2,
32
32
  STAKE_POOL_REGISTRATION: 3,
33
+ STAKE_REGISTRATION_CONWAY: 7,
34
+ STAKE_DEREGISTRATION_CONWAY: 8,
35
+ VOTE_DELEGATION: 9,
33
36
  };
34
37
  exports.ERROR = {
35
38
  UTXO_BALANCE_INSUFFICIENT: {
@@ -52,11 +55,13 @@ exports.ERROR = {
52
55
  exports.CARDANO_PARAMS = {
53
56
  PROTOCOL_MAGICS: {
54
57
  mainnet: CardanoWasm.NetworkInfo.mainnet().protocol_magic(),
55
- testnet: CardanoWasm.NetworkInfo.testnet().protocol_magic(),
58
+ testnet_preprod: CardanoWasm.NetworkInfo.testnet_preprod().protocol_magic(),
59
+ testnet_preview: CardanoWasm.NetworkInfo.testnet_preview().protocol_magic(),
56
60
  },
57
61
  NETWORK_IDS: {
58
62
  mainnet: CardanoWasm.NetworkInfo.mainnet().network_id(),
59
- testnet: CardanoWasm.NetworkInfo.testnet().network_id(),
63
+ testnet_preprod: CardanoWasm.NetworkInfo.testnet_preprod().network_id(),
64
+ testnet_preview: CardanoWasm.NetworkInfo.testnet_preview().network_id(),
60
65
  },
61
66
  COINS_PER_UTXO_BYTE: '4310',
62
67
  MAX_TX_SIZE: 16384,
@@ -61,7 +61,7 @@ const largestFirst = (params, options) => {
61
61
  const addUtxoToSelection = (utxo) => {
62
62
  const { input, address, amount } = (0, common_1.buildTxInput)(utxo);
63
63
  const fee = txBuilder.fee_for_input(address, input, amount);
64
- txBuilder.add_input(address, input, amount);
64
+ txBuilder.add_regular_input(address, input, amount);
65
65
  usedUtxos.push(utxo);
66
66
  totalFeesAmount = totalFeesAmount.checked_add(fee);
67
67
  utxosTotalAmount = utxosTotalAmount.checked_add((0, common_1.bigNumFromStr)((0, common_1.getAssetAmount)(utxo)));
@@ -80,14 +80,15 @@ const largestFirst = (params, options) => {
80
80
  let forceAnotherRound = false;
81
81
  while (!sufficientUtxos) {
82
82
  if (maxOutput) {
83
- // reset previously computed maxOutput in order to correctly calculate a potential change output
83
+ // Reset previously computed maxOutput in order to correctly calculate a potential change output
84
+ // when new utxo is added to the set
84
85
  preparedOutputs[maxOutputIndex] = (0, common_1.setMinUtxoValueForOutputs)(txBuilder, [maxOutput], changeAddress)[0];
85
86
  }
86
87
  // Calculate change output
87
88
  let singleChangeOutput = (0, common_1.prepareChangeOutput)(txBuilder, usedUtxos, preparedOutputs, changeAddress, utxosTotalAmount, (0, common_1.getUserOutputQuantityWithDeposit)(preparedOutputs, deposit), totalFeesAmount);
88
89
  if (maxOutput) {
89
90
  // set amount for a max output from a changeOutput calculated above
90
- const { maxOutput: newMaxOutput } = (0, common_1.setMaxOutput)(maxOutput, singleChangeOutput);
91
+ const { maxOutput: newMaxOutput } = (0, common_1.setMaxOutput)(txBuilder, maxOutput, singleChangeOutput);
91
92
  // change output may be completely removed if all ADA are consumed by max output
92
93
  preparedOutputs[maxOutputIndex] = newMaxOutput;
93
94
  // recalculate total user outputs amount
@@ -173,7 +174,9 @@ const largestFirst = (params, options) => {
173
174
  }
174
175
  txBuilder.set_fee(totalFeesAmount);
175
176
  const txBody = txBuilder.build();
176
- const txHash = Buffer.from(CardanoWasm.hash_transaction(txBody).to_bytes()).toString('hex');
177
+ const txHash = CardanoWasm.FixedTransaction.new_from_body_bytes(txBody.to_bytes())
178
+ .transaction_hash()
179
+ .to_hex();
177
180
  const txBodyHex = Buffer.from(txBody.to_bytes()).toString('hex');
178
181
  const totalSpent = totalUserOutputsAmount.checked_add(totalFeesAmount);
179
182
  // Set max property with the value of an output which has setMax=true
@@ -1,2 +1,2 @@
1
1
  import { CoinSelectionParams, CoinSelectionResult, Options } from '../types/types';
2
- export declare const randomImprove: (params: Pick<CoinSelectionParams, 'utxos' | 'outputs' | 'changeAddress' | 'ttl'>, options?: Options) => CoinSelectionResult;
2
+ export declare const randomImprove: (params: Pick<CoinSelectionParams, "utxos" | "outputs" | "changeAddress" | "ttl">, options?: Options) => CoinSelectionResult;
@@ -82,7 +82,7 @@ const selection = (utxos, outputs, txBuilder, dummyAddress) => {
82
82
  if (improvesSelection(utxoSelected, utxo, (0, common_1.getOutputQuantity)(preparedOutputs, asset), asset)) {
83
83
  utxoSelected.push(utxo);
84
84
  const { input, address, amount } = (0, common_1.buildTxInput)(utxo);
85
- txBuilder.add_input(address, input, amount);
85
+ txBuilder.add_regular_input(address, input, amount);
86
86
  utxoRemaining.splice(utxoRemaining.indexOf(utxo), 1);
87
87
  }
88
88
  else {
@@ -169,7 +169,9 @@ const randomImprove = (params, options) => {
169
169
  const totalSpent = totalUserOutputsAmount.checked_add(fee);
170
170
  txBuilder.set_fee(fee);
171
171
  const txBody = txBuilder.build();
172
- const txHash = Buffer.from(CardanoWasm.hash_transaction(txBody).to_bytes()).toString('hex');
172
+ const txHash = CardanoWasm.FixedTransaction.new_from_body_bytes(txBody.to_bytes())
173
+ .transaction_hash()
174
+ .to_hex();
173
175
  const txBodyHex = Buffer.from(txBody.to_bytes()).toString('hex');
174
176
  // reorder inputs to match order within tx
175
177
  const orderedInputs = (0, common_1.orderInputs)(utxoSelected, txBody);
@@ -4,11 +4,11 @@ export interface CardanoInput {
4
4
  prev_hash: string;
5
5
  prev_index: number;
6
6
  }
7
- export declare type CardanoToken = {
7
+ export type CardanoToken = {
8
8
  assetNameBytes: string;
9
9
  amount: string;
10
10
  };
11
- export declare type CardanoAssetGroup = {
11
+ export type CardanoAssetGroup = {
12
12
  policyId: string;
13
13
  tokenAmounts: CardanoToken[];
14
14
  };
@@ -24,7 +24,7 @@ export interface CardanoAddressParameters {
24
24
  stakingKeyHash?: string;
25
25
  certificatePointer?: CardanoCertificatePointer;
26
26
  }
27
- export declare type CardanoOutput = {
27
+ export type CardanoOutput = {
28
28
  addressParameters: CardanoAddressParameters;
29
29
  amount: string;
30
30
  tokenBundle?: CardanoAssetGroup[];
@@ -37,7 +37,7 @@ export declare enum CardanoTxWitnessType {
37
37
  BYRON_WITNESS = 0,
38
38
  SHELLEY_WITNESS = 1
39
39
  }
40
- export declare type CardanoSignedTxWitness = {
40
+ export type CardanoSignedTxWitness = {
41
41
  type: CardanoTxWitnessType;
42
42
  pubKey: string;
43
43
  signature: string;
@@ -5,4 +5,4 @@ var CardanoTxWitnessType;
5
5
  (function (CardanoTxWitnessType) {
6
6
  CardanoTxWitnessType[CardanoTxWitnessType["BYRON_WITNESS"] = 0] = "BYRON_WITNESS";
7
7
  CardanoTxWitnessType[CardanoTxWitnessType["SHELLEY_WITNESS"] = 1] = "SHELLEY_WITNESS";
8
- })(CardanoTxWitnessType = exports.CardanoTxWitnessType || (exports.CardanoTxWitnessType = {}));
8
+ })(CardanoTxWitnessType || (exports.CardanoTxWitnessType = CardanoTxWitnessType = {}));
@@ -36,9 +36,9 @@ export interface ChangeOutput extends BaseOutput {
36
36
  address: string;
37
37
  isChange: true;
38
38
  }
39
- export declare type FinalOutput = ExternalOutput | ChangeOutput;
40
- export declare type UserOutput = ExternalOutput | ExternalOutputIncomplete;
41
- export declare type Output = FinalOutput | ExternalOutputIncomplete;
39
+ export type FinalOutput = ExternalOutput | ChangeOutput;
40
+ export type UserOutput = ExternalOutput | ExternalOutputIncomplete;
41
+ export type Output = FinalOutput | ExternalOutputIncomplete;
42
42
  export interface OutputCost {
43
43
  output: CardanoWasm.TransactionOutput;
44
44
  outputFee: BigNum;
@@ -57,6 +57,23 @@ export declare enum CardanoAddressType {
57
57
  REWARD = 14,
58
58
  REWARD_SCRIPT = 15
59
59
  }
60
+ export declare enum CardanoDRepType {
61
+ KEY_HASH = 0,
62
+ SCRIPT_HASH = 1,
63
+ ABSTAIN = 2,
64
+ NO_CONFIDENCE = 3
65
+ }
66
+ export type DRep = {
67
+ type: CardanoDRepType.KEY_HASH;
68
+ keyHash: string;
69
+ } | {
70
+ type: CardanoDRepType.SCRIPT_HASH;
71
+ scriptHash: string;
72
+ } | {
73
+ type: CardanoDRepType.ABSTAIN | CardanoDRepType.NO_CONFIDENCE;
74
+ keyHash?: never;
75
+ scriptHash?: never;
76
+ };
60
77
  export interface CoinSelectionResult {
61
78
  tx: {
62
79
  body: string;
@@ -72,7 +89,7 @@ export interface CoinSelectionResult {
72
89
  ttl?: number;
73
90
  max?: string;
74
91
  }
75
- export declare type PrecomposedTransaction = ({
92
+ export type PrecomposedTransaction = ({
76
93
  type: 'final';
77
94
  outputs: FinalOutput[];
78
95
  } & Omit<CoinSelectionResult, 'outputs'>) | ({
@@ -82,7 +99,7 @@ export interface Withdrawal {
82
99
  stakeAddress: string;
83
100
  amount: string;
84
101
  }
85
- export declare type CertificateTypeType = typeof CertificateType;
102
+ export type CertificateTypeType = typeof CertificateType;
86
103
  export interface CertificateStakeRegistration {
87
104
  type: CertificateTypeType['STAKE_REGISTRATION'] | CertificateTypeType['STAKE_DEREGISTRATION'];
88
105
  stakingKeyHash?: string;
@@ -96,7 +113,11 @@ export interface CertificateStakePoolRegistration {
96
113
  type: CertificateTypeType['STAKE_POOL_REGISTRATION'];
97
114
  pool_parameters: Record<string, unknown>;
98
115
  }
99
- export declare type Certificate = CertificateStakeRegistration | CertificateStakeDelegation | CertificateStakePoolRegistration;
116
+ export interface CertificateVoteDelegation {
117
+ type: CertificateTypeType['VOTE_DELEGATION'];
118
+ dRep: DRep;
119
+ }
120
+ export type Certificate = CertificateStakeRegistration | CertificateStakeDelegation | CertificateStakePoolRegistration | CertificateVoteDelegation;
100
121
  export interface Options {
101
122
  feeParams?: {
102
123
  a: string;
@@ -1,6 +1,6 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.CardanoAddressType = void 0;
3
+ exports.CardanoDRepType = exports.CardanoAddressType = void 0;
4
4
  var CardanoAddressType;
5
5
  (function (CardanoAddressType) {
6
6
  CardanoAddressType[CardanoAddressType["BASE"] = 0] = "BASE";
@@ -14,4 +14,11 @@ var CardanoAddressType;
14
14
  CardanoAddressType[CardanoAddressType["BYRON"] = 8] = "BYRON";
15
15
  CardanoAddressType[CardanoAddressType["REWARD"] = 14] = "REWARD";
16
16
  CardanoAddressType[CardanoAddressType["REWARD_SCRIPT"] = 15] = "REWARD_SCRIPT";
17
- })(CardanoAddressType = exports.CardanoAddressType || (exports.CardanoAddressType = {}));
17
+ })(CardanoAddressType || (exports.CardanoAddressType = CardanoAddressType = {}));
18
+ var CardanoDRepType;
19
+ (function (CardanoDRepType) {
20
+ CardanoDRepType[CardanoDRepType["KEY_HASH"] = 0] = "KEY_HASH";
21
+ CardanoDRepType[CardanoDRepType["SCRIPT_HASH"] = 1] = "SCRIPT_HASH";
22
+ CardanoDRepType[CardanoDRepType["ABSTAIN"] = 2] = "ABSTAIN";
23
+ CardanoDRepType[CardanoDRepType["NO_CONFIDENCE"] = 3] = "NO_CONFIDENCE";
24
+ })(CardanoDRepType || (exports.CardanoDRepType = CardanoDRepType = {}));
@@ -2,15 +2,15 @@ import * as CardanoWasm from '@emurgo/cardano-serialization-lib-asmjs';
2
2
  import { CARDANO_PARAMS } from '../constants';
3
3
  import { Certificate, Output, Utxo, Withdrawal, OutputCost, UserOutput, Asset } from '../types/types';
4
4
  export declare const bigNumFromStr: (num: string) => CardanoWasm.BigNum;
5
- export declare const getProtocolMagic: (tesnet?: boolean) => (typeof CARDANO_PARAMS.PROTOCOL_MAGICS)['mainnet'] | (typeof CARDANO_PARAMS.PROTOCOL_MAGICS)['testnet'];
6
- export declare const getNetworkId: (testnet?: boolean) => (typeof CARDANO_PARAMS.NETWORK_IDS)['mainnet'] | (typeof CARDANO_PARAMS.NETWORK_IDS)['testnet'];
5
+ export declare const getProtocolMagic: (tesnet?: boolean) => (typeof CARDANO_PARAMS.PROTOCOL_MAGICS)["mainnet"] | (typeof CARDANO_PARAMS.PROTOCOL_MAGICS)["testnet_preview"] | (typeof CARDANO_PARAMS.PROTOCOL_MAGICS)["testnet_preprod"];
6
+ export declare const getNetworkId: (testnet?: boolean) => (typeof CARDANO_PARAMS.NETWORK_IDS)["mainnet"] | (typeof CARDANO_PARAMS.NETWORK_IDS)["testnet_preprod"] | (typeof CARDANO_PARAMS.NETWORK_IDS)["testnet_preview"];
7
7
  export declare const parseAsset: (hex: string) => {
8
8
  policyId: string;
9
9
  assetNameInHex: string;
10
10
  };
11
11
  export declare const buildMultiAsset: (assets: Asset[]) => CardanoWasm.MultiAsset;
12
12
  export declare const multiAssetToArray: (multiAsset: CardanoWasm.MultiAsset | undefined) => Asset[];
13
- export declare const getAssetAmount: (obj: Pick<Utxo, 'amount'>, asset?: string) => string;
13
+ export declare const getAssetAmount: (obj: Pick<Utxo, "amount">, asset?: string) => string;
14
14
  export declare const getUtxoQuantity: (utxos: Utxo[], asset?: string) => CardanoWasm.BigNum;
15
15
  export declare const getOutputQuantity: (outputs: Output[], asset?: string) => CardanoWasm.BigNum;
16
16
  export declare const sortUtxos: (utxos: Utxo[], asset?: string) => Utxo[];
@@ -33,7 +33,7 @@ export declare const getInitialUtxoSet: (utxos: Utxo[], maxOutput: UserOutput |
33
33
  used: Utxo[];
34
34
  remaining: Utxo[];
35
35
  };
36
- export declare const setMaxOutput: (maxOutput: UserOutput, changeOutput: OutputCost | null) => {
36
+ export declare const setMaxOutput: (txBuilder: CardanoWasm.TransactionBuilder, maxOutput: UserOutput, changeOutput: OutputCost | null) => {
37
37
  maxOutput: UserOutput;
38
38
  };
39
39
  export declare const getUserOutputQuantityWithDeposit: (outputs: UserOutput[], deposit: number, asset?: string) => CardanoWasm.BigNum;
@@ -26,15 +26,16 @@ Object.defineProperty(exports, "__esModule", { value: true });
26
26
  exports.orderInputs = exports.calculateUserOutputsFee = exports.getRandomUtxo = exports.filterUtxos = exports.getUserOutputQuantityWithDeposit = exports.setMaxOutput = exports.getInitialUtxoSet = exports.getUnsatisfiedAssets = exports.getTxBuilder = exports.prepareChangeOutput = exports.splitChangeOutput = exports.setMinUtxoValueForOutputs = exports.calculateRequiredDeposit = exports.prepareCertificates = exports.prepareWithdrawals = exports.getOutputCost = exports.buildTxOutput = exports.buildTxInput = exports.sortUtxos = exports.getOutputQuantity = exports.getUtxoQuantity = exports.getAssetAmount = exports.multiAssetToArray = exports.buildMultiAsset = exports.parseAsset = exports.getNetworkId = exports.getProtocolMagic = exports.bigNumFromStr = void 0;
27
27
  const CardanoWasm = __importStar(require("@emurgo/cardano-serialization-lib-asmjs"));
28
28
  const constants_1 = require("../constants");
29
+ const types_1 = require("../types/types");
29
30
  const errors_1 = require("./errors");
30
31
  const bigNumFromStr = (num) => CardanoWasm.BigNum.from_str(num);
31
32
  exports.bigNumFromStr = bigNumFromStr;
32
33
  const getProtocolMagic = (tesnet) => tesnet
33
- ? constants_1.CARDANO_PARAMS.PROTOCOL_MAGICS.testnet
34
+ ? constants_1.CARDANO_PARAMS.PROTOCOL_MAGICS.testnet_preview
34
35
  : constants_1.CARDANO_PARAMS.PROTOCOL_MAGICS.mainnet;
35
36
  exports.getProtocolMagic = getProtocolMagic;
36
37
  const getNetworkId = (testnet) => testnet
37
- ? constants_1.CARDANO_PARAMS.NETWORK_IDS.testnet
38
+ ? constants_1.CARDANO_PARAMS.NETWORK_IDS.testnet_preview
38
39
  : constants_1.CARDANO_PARAMS.NETWORK_IDS.mainnet;
39
40
  exports.getNetworkId = getNetworkId;
40
41
  const parseAsset = (hex) => {
@@ -181,7 +182,7 @@ const prepareCertificates = (certificates, accountKey) => {
181
182
  if (certificates.length === 0)
182
183
  return preparedCertificates;
183
184
  const stakeKey = accountKey.derive(2).derive(0);
184
- const stakeCred = CardanoWasm.StakeCredential.from_keyhash(stakeKey.to_raw_key().hash());
185
+ const stakeCred = CardanoWasm.Credential.from_keyhash(stakeKey.to_raw_key().hash());
185
186
  certificates.forEach(cert => {
186
187
  if (cert.type === constants_1.CertificateType.STAKE_REGISTRATION) {
187
188
  preparedCertificates.add(CardanoWasm.Certificate.new_stake_registration(CardanoWasm.StakeRegistration.new(stakeCred)));
@@ -192,6 +193,26 @@ const prepareCertificates = (certificates, accountKey) => {
192
193
  else if (cert.type === constants_1.CertificateType.STAKE_DEREGISTRATION) {
193
194
  preparedCertificates.add(CardanoWasm.Certificate.new_stake_deregistration(CardanoWasm.StakeDeregistration.new(stakeCred)));
194
195
  }
196
+ else if (cert.type === constants_1.CertificateType.VOTE_DELEGATION) {
197
+ let targetDRep;
198
+ switch (cert.dRep.type) {
199
+ case types_1.CardanoDRepType.ABSTAIN:
200
+ targetDRep = CardanoWasm.DRep.new_always_abstain();
201
+ break;
202
+ case types_1.CardanoDRepType.NO_CONFIDENCE:
203
+ targetDRep = CardanoWasm.DRep.new_always_no_confidence();
204
+ break;
205
+ case types_1.CardanoDRepType.KEY_HASH:
206
+ targetDRep = CardanoWasm.DRep.new_key_hash(CardanoWasm.Ed25519KeyHash.from_hex(cert.dRep.keyHash));
207
+ break;
208
+ case types_1.CardanoDRepType.SCRIPT_HASH:
209
+ targetDRep = CardanoWasm.DRep.new_script_hash(CardanoWasm.ScriptHash.from_hex(cert.dRep.scriptHash));
210
+ break;
211
+ }
212
+ if (targetDRep) {
213
+ preparedCertificates.add(CardanoWasm.Certificate.new_vote_delegation(CardanoWasm.VoteDelegation.new(stakeCred, targetDRep)));
214
+ }
215
+ }
195
216
  else {
196
217
  throw new errors_1.CoinSelectionError(constants_1.ERROR.UNSUPPORTED_CERTIFICATE_TYPE);
197
218
  }
@@ -202,6 +223,7 @@ exports.prepareCertificates = prepareCertificates;
202
223
  const calculateRequiredDeposit = (certificates) => {
203
224
  const CertificateDeposit = {
204
225
  [constants_1.CertificateType.STAKE_DELEGATION]: 0,
226
+ [constants_1.CertificateType.VOTE_DELEGATION]: 0,
205
227
  [constants_1.CertificateType.STAKE_POOL_REGISTRATION]: 500000000,
206
228
  [constants_1.CertificateType.STAKE_REGISTRATION]: 2000000,
207
229
  [constants_1.CertificateType.STAKE_DEREGISTRATION]: -2000000,
@@ -420,18 +442,22 @@ const getInitialUtxoSet = (utxos, maxOutput) => {
420
442
  };
421
443
  };
422
444
  exports.getInitialUtxoSet = getInitialUtxoSet;
423
- const setMaxOutput = (maxOutput, changeOutput) => {
424
- var _a, _b, _c, _d;
445
+ const setMaxOutput = (txBuilder, maxOutput, changeOutput) => {
446
+ var _a, _b, _c, _d, _e;
425
447
  const maxOutputAsset = (_b = (_a = maxOutput.assets[0]) === null || _a === void 0 ? void 0 : _a.unit) !== null && _b !== void 0 ? _b : 'lovelace';
426
448
  let newMaxAmount = (0, exports.bigNumFromStr)('0');
427
449
  const changeOutputAssets = (0, exports.multiAssetToArray)(changeOutput === null || changeOutput === void 0 ? void 0 : changeOutput.output.amount().multiasset());
428
450
  if (maxOutputAsset === 'lovelace') {
429
451
  // set maxOutput for ADA
430
452
  if (changeOutput) {
453
+ // Calculate the cost of previous dummy set-max output
454
+ const previousMaxOutputCost = (0, exports.getOutputCost)(txBuilder, maxOutput, (_c = maxOutput.address) !== null && _c !== void 0 ? _c : changeOutput.output.address().to_bech32());
431
455
  newMaxAmount = changeOutput.output.amount().coin();
432
456
  if (changeOutputAssets.length === 0) {
433
- // we don't need the change output anymore
434
- newMaxAmount = newMaxAmount.checked_add(changeOutput.outputFee);
457
+ // Add a fee that was previously consumed by the dummy max output.
458
+ // Cost calculated for the change output will be greater (due to larger coin amount
459
+ // than in dummy output - which is 0) than the cost of the dummy set-max output.
460
+ newMaxAmount = newMaxAmount.checked_add(previousMaxOutputCost.outputFee);
435
461
  changeOutput = null;
436
462
  }
437
463
  else {
@@ -450,7 +476,7 @@ const setMaxOutput = (maxOutput, changeOutput) => {
450
476
  // set maxOutput for token
451
477
  if (changeOutput) {
452
478
  // max amount of the asset in output is equal to its quantity in change output
453
- newMaxAmount = (0, exports.bigNumFromStr)((_d = (_c = changeOutputAssets.find(a => a.unit === maxOutputAsset)) === null || _c === void 0 ? void 0 : _c.quantity) !== null && _d !== void 0 ? _d : '0');
479
+ newMaxAmount = (0, exports.bigNumFromStr)((_e = (_d = changeOutputAssets.find(a => a.unit === maxOutputAsset)) === null || _d === void 0 ? void 0 : _d.quantity) !== null && _e !== void 0 ? _e : '0');
454
480
  maxOutput.assets[0].quantity = newMaxAmount.to_str(); // TODO: set 0 if no change?
455
481
  const txOutput = CardanoWasm.TransactionOutput.new(changeOutput.output.address(),
456
482
  // new_from_assets does not automatically include required ADA
@@ -484,7 +510,7 @@ const getRandomUtxo = (txBuilder, utxoRemaining, utxoSelected) => {
484
510
  addUtxo: () => {
485
511
  utxoSelected.push(utxo);
486
512
  const { input, address, amount } = (0, exports.buildTxInput)(utxo);
487
- txBuilder.add_input(address, input, amount);
513
+ txBuilder.add_regular_input(address, input, amount);
488
514
  utxoRemaining.splice(utxoRemaining.indexOf(utxo), 1);
489
515
  },
490
516
  };
@@ -1,6 +1,6 @@
1
1
  import { ERROR } from '../constants';
2
- declare type ErrorTypeKeys = keyof typeof ERROR;
3
- declare type ErrorObjectType = typeof ERROR[ErrorTypeKeys];
2
+ type ErrorTypeKeys = keyof typeof ERROR;
3
+ type ErrorObjectType = typeof ERROR[ErrorTypeKeys];
4
4
  export declare class CoinSelectionError extends Error {
5
5
  code: ErrorObjectType['code'];
6
6
  constructor(errorObject: ErrorObjectType);
@@ -163,7 +163,9 @@ const convertCborTxToEncodeTx = async (txHex, utxos, addresses, changeAddress) =
163
163
  },
164
164
  tx: {
165
165
  body: body.to_hex(),
166
- hash: Buffer.from(CardanoWasm.hash_transaction(body).to_bytes(), 'utf8').toString('hex'),
166
+ hash: CardanoWasm.FixedTransaction.new_from_body_bytes(body.to_bytes())
167
+ .transaction_hash()
168
+ .to_hex(),
167
169
  size: 0,
168
170
  rawTxHex: txHex,
169
171
  },
@@ -2,8 +2,8 @@ import { dAppUtils } from './dapp';
2
2
  declare const onekeyUtils: {
3
3
  composeTxPlan: (transferInfo: import("./types").ITransferInfo, accountXpub: string, utxos: import("./types").IAdaUTXO[], changeAddress: string, outputs: import("./types").IOutput[], options?: {
4
4
  debug: boolean;
5
- } | undefined) => Promise<import("../../types/types").PrecomposedTransaction>;
6
- signTransaction: (txBodyHex: string, address: string, accountIndex: number, utxos: import("./types").IAdaUTXO[], xprv: string, signOnly: boolean, partialSign?: boolean | undefined) => Promise<{
5
+ }) => Promise<import("../../types/types").PrecomposedTransaction>;
6
+ signTransaction: (txBodyHex: string, address: string, accountIndex: number, utxos: import("./types").IAdaUTXO[], xprv: string, signOnly: boolean, partialSign?: boolean) => Promise<{
7
7
  signedTx: string;
8
8
  txid: string;
9
9
  }>;
@@ -1,6 +1,6 @@
1
1
  import * as CardanoWasm from '@emurgo/cardano-serialization-lib-asmjs';
2
2
  import { IAdaUTXO } from './types';
3
- declare type SignKeys = {
3
+ type SignKeys = {
4
4
  accountKey: CardanoWasm.Bip32PrivateKey;
5
5
  paymentKey: CardanoWasm.PrivateKey;
6
6
  stakeKey: CardanoWasm.PrivateKey;
@@ -44,7 +44,9 @@ const signTransaction = async (txBodyHex, address, accountIndex, utxos, xprv, si
44
44
  }
45
45
  const transaction = CardanoWasm.Transaction.new(rawTx.body(), witnessSet, rawTx.auxiliary_data());
46
46
  const signedTx = Buffer.from(transaction.to_bytes(), 'hex').toString('hex');
47
- const hash = CardanoWasm.hash_transaction(transaction.body()).to_hex();
47
+ const hash = CardanoWasm.FixedTransaction.new_from_body_bytes(transaction.body().to_bytes())
48
+ .transaction_hash()
49
+ .to_hex();
48
50
  return {
49
51
  signedTx,
50
52
  txid: hash,
@@ -65,7 +67,7 @@ const signTx = async (keys, tx, keyHashes, partialSign = false) => {
65
67
  const rawTx = CardanoWasm.Transaction.from_bytes(Buffer.from(tx, 'hex'));
66
68
  const txWitnessSet = CardanoWasm.TransactionWitnessSet.new();
67
69
  const vkeyWitnesses = CardanoWasm.Vkeywitnesses.new();
68
- const txHash = CardanoWasm.hash_transaction(rawTx.body());
70
+ const txHash = CardanoWasm.FixedTransaction.new_from_body_bytes(rawTx.body().to_bytes()).transaction_hash();
69
71
  keyHashes.forEach(keyHash => {
70
72
  let signingKey;
71
73
  if (keyHash === paymentKeyHash)
@@ -29,11 +29,11 @@ export declare enum CardanoPoolRelayType {
29
29
  SINGLE_HOST_NAME = 1,
30
30
  MULTIPLE_HOST_NAME = 2
31
31
  }
32
- declare type Key = {
32
+ type Key = {
33
33
  hash: string | null;
34
34
  path: string | null;
35
35
  };
36
- declare type Keys = {
36
+ type Keys = {
37
37
  payment: Key;
38
38
  stake: Key;
39
39
  };
@@ -39,27 +39,27 @@ var CardanoAddressType;
39
39
  CardanoAddressType[CardanoAddressType["BYRON"] = 8] = "BYRON";
40
40
  CardanoAddressType[CardanoAddressType["REWARD"] = 14] = "REWARD";
41
41
  CardanoAddressType[CardanoAddressType["REWARD_SCRIPT"] = 15] = "REWARD_SCRIPT";
42
- })(CardanoAddressType = exports.CardanoAddressType || (exports.CardanoAddressType = {}));
42
+ })(CardanoAddressType || (exports.CardanoAddressType = CardanoAddressType = {}));
43
43
  var CardanoTxSigningMode;
44
44
  (function (CardanoTxSigningMode) {
45
45
  CardanoTxSigningMode[CardanoTxSigningMode["ORDINARY_TRANSACTION"] = 0] = "ORDINARY_TRANSACTION";
46
46
  CardanoTxSigningMode[CardanoTxSigningMode["POOL_REGISTRATION_AS_OWNER"] = 1] = "POOL_REGISTRATION_AS_OWNER";
47
47
  CardanoTxSigningMode[CardanoTxSigningMode["MULTISIG_TRANSACTION"] = 2] = "MULTISIG_TRANSACTION";
48
48
  CardanoTxSigningMode[CardanoTxSigningMode["PLUTUS_TRANSACTION"] = 3] = "PLUTUS_TRANSACTION";
49
- })(CardanoTxSigningMode = exports.CardanoTxSigningMode || (exports.CardanoTxSigningMode = {}));
49
+ })(CardanoTxSigningMode || (exports.CardanoTxSigningMode = CardanoTxSigningMode = {}));
50
50
  var CardanoCertificateType;
51
51
  (function (CardanoCertificateType) {
52
52
  CardanoCertificateType[CardanoCertificateType["STAKE_REGISTRATION"] = 0] = "STAKE_REGISTRATION";
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 = exports.CardanoCertificateType || (exports.CardanoCertificateType = {}));
56
+ })(CardanoCertificateType || (exports.CardanoCertificateType = CardanoCertificateType = {}));
57
57
  var CardanoPoolRelayType;
58
58
  (function (CardanoPoolRelayType) {
59
59
  CardanoPoolRelayType[CardanoPoolRelayType["SINGLE_HOST_IP"] = 0] = "SINGLE_HOST_IP";
60
60
  CardanoPoolRelayType[CardanoPoolRelayType["SINGLE_HOST_NAME"] = 1] = "SINGLE_HOST_NAME";
61
61
  CardanoPoolRelayType[CardanoPoolRelayType["MULTIPLE_HOST_NAME"] = 2] = "MULTIPLE_HOST_NAME";
62
- })(CardanoPoolRelayType = exports.CardanoPoolRelayType || (exports.CardanoPoolRelayType = {}));
62
+ })(CardanoPoolRelayType || (exports.CardanoPoolRelayType = CardanoPoolRelayType = {}));
63
63
  const generateKeys = (keys, xpub) => {
64
64
  const publicKey = CardanoWasm.Bip32PublicKey.from_bytes(Buffer.from(xpub, 'hex'));
65
65
  const paymentKeyHashRaw = publicKey.derive(0).derive(0).to_raw_key().hash();
@@ -1,4 +1,4 @@
1
- export declare type ITransferInfo = {
1
+ export type ITransferInfo = {
2
2
  from: string;
3
3
  to: string;
4
4
  amount: string;
@@ -7,11 +7,11 @@ export declare type ITransferInfo = {
7
7
  tokenId?: string;
8
8
  type?: string;
9
9
  };
10
- export declare type IAdaAmount = {
10
+ export type IAdaAmount = {
11
11
  unit: string;
12
12
  quantity: string;
13
13
  };
14
- export declare type IAdaUTXO = {
14
+ export type IAdaUTXO = {
15
15
  path: string;
16
16
  address: string;
17
17
  tx_hash: string;
@@ -19,12 +19,12 @@ export declare type IAdaUTXO = {
19
19
  output_index: number;
20
20
  amount: IAdaAmount[];
21
21
  };
22
- export declare type IOutput = {
22
+ export type IOutput = {
23
23
  address: string;
24
24
  amount: string;
25
25
  assets: [];
26
26
  };
27
- export declare type IEncodeInput = {
27
+ export type IEncodeInput = {
28
28
  address: string;
29
29
  amount: IAdaAmount[];
30
30
  block: string;
@@ -34,18 +34,18 @@ export declare type IEncodeInput = {
34
34
  tx_hash: string;
35
35
  tx_index: number;
36
36
  };
37
- export declare type IEncodeOutput = {
37
+ export type IEncodeOutput = {
38
38
  address: string;
39
39
  amount: string;
40
40
  assets: IAdaAmount[];
41
41
  isChange?: boolean;
42
42
  };
43
- declare type ITxInfo = {
43
+ type ITxInfo = {
44
44
  body: string;
45
45
  hash: string;
46
46
  size: number;
47
47
  };
48
- export declare type IEncodedTxADA = {
48
+ export type IEncodedTxADA = {
49
49
  inputs: IEncodeInput[];
50
50
  outputs: IEncodeOutput[];
51
51
  fee: string;
@@ -68,7 +68,7 @@ declare enum CardanoAddressType {
68
68
  REWARD = 14,
69
69
  REWARD_SCRIPT = 15
70
70
  }
71
- export declare type IChangeAddress = {
71
+ export type IChangeAddress = {
72
72
  address: string;
73
73
  addressParameters: {
74
74
  path: string;
@@ -1,3 +1,3 @@
1
- import { transformToTokenBundle, transformToTrezorInputs, transformToTrezorOutputs } from './transformations';
1
+ import { transformToTokenBundle, transformToTrezorInputs, transformToTrezorOutputs, drepIdToHex } from './transformations';
2
2
  import { signTransaction } from './sign';
3
- export { transformToTokenBundle, transformToTrezorInputs, transformToTrezorOutputs, signTransaction, };
3
+ export { transformToTokenBundle, transformToTrezorInputs, transformToTrezorOutputs, signTransaction, drepIdToHex, };
@@ -1,9 +1,10 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.signTransaction = exports.transformToTrezorOutputs = exports.transformToTrezorInputs = exports.transformToTokenBundle = void 0;
3
+ exports.drepIdToHex = exports.signTransaction = exports.transformToTrezorOutputs = exports.transformToTrezorInputs = exports.transformToTokenBundle = void 0;
4
4
  const transformations_1 = require("./transformations");
5
5
  Object.defineProperty(exports, "transformToTokenBundle", { enumerable: true, get: function () { return transformations_1.transformToTokenBundle; } });
6
6
  Object.defineProperty(exports, "transformToTrezorInputs", { enumerable: true, get: function () { return transformations_1.transformToTrezorInputs; } });
7
7
  Object.defineProperty(exports, "transformToTrezorOutputs", { enumerable: true, get: function () { return transformations_1.transformToTrezorOutputs; } });
8
+ Object.defineProperty(exports, "drepIdToHex", { enumerable: true, get: function () { return transformations_1.drepIdToHex; } });
8
9
  const sign_1 = require("./sign");
9
10
  Object.defineProperty(exports, "signTransaction", { enumerable: true, get: function () { return sign_1.signTransaction; } });
@@ -1,5 +1,5 @@
1
1
  import { CardanoAddressParameters, CardanoInput, CardanoOutput } from '../../types/trezor';
2
- import { Asset, FinalOutput, Utxo } from '../../types/types';
2
+ import { Asset, CardanoDRepType, FinalOutput, Utxo } from '../../types/types';
3
3
  interface AssetInPolicy {
4
4
  assetNameBytes: string;
5
5
  amount: string;
@@ -14,4 +14,8 @@ export declare const transformToTrezorInputs: (utxos: Utxo[], trezorUtxos: {
14
14
  path: string;
15
15
  }[]) => CardanoInput[];
16
16
  export declare const transformToTrezorOutputs: (outputs: FinalOutput[], changeAddressParameters: CardanoAddressParameters) => CardanoOutput[];
17
+ export declare const drepIdToHex: (drepId: string) => {
18
+ type: CardanoDRepType.KEY_HASH | CardanoDRepType.SCRIPT_HASH;
19
+ hex: string;
20
+ };
17
21
  export {};