@onekeyfe/cardano-coin-selection-asmjs 1.1.0

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 (79) hide show
  1. package/CHANGELOG.MD +25 -0
  2. package/README.md +183 -0
  3. package/lib/cjs/constants/index.d.ts +40 -0
  4. package/lib/cjs/constants/index.js +69 -0
  5. package/lib/cjs/index.d.ts +6 -0
  6. package/lib/cjs/index.js +101 -0
  7. package/lib/cjs/methods/largestFirst.d.ts +2 -0
  8. package/lib/cjs/methods/largestFirst.js +201 -0
  9. package/lib/cjs/methods/randomImprove.d.ts +2 -0
  10. package/lib/cjs/methods/randomImprove.js +187 -0
  11. package/lib/cjs/types/trezor.d.ts +45 -0
  12. package/lib/cjs/types/trezor.js +8 -0
  13. package/lib/cjs/types/types.d.ts +116 -0
  14. package/lib/cjs/types/types.js +17 -0
  15. package/lib/cjs/utils/common.d.ts +46 -0
  16. package/lib/cjs/utils/common.js +514 -0
  17. package/lib/cjs/utils/errors.d.ts +8 -0
  18. package/lib/cjs/utils/errors.js +13 -0
  19. package/lib/cjs/utils/logger.d.ts +5 -0
  20. package/lib/cjs/utils/logger.js +27 -0
  21. package/lib/cjs/utils/onekey/dapp.d.ts +14 -0
  22. package/lib/cjs/utils/onekey/dapp.js +247 -0
  23. package/lib/cjs/utils/onekey/error.d.ts +28 -0
  24. package/lib/cjs/utils/onekey/error.js +25 -0
  25. package/lib/cjs/utils/onekey/index.d.ts +48 -0
  26. package/lib/cjs/utils/onekey/index.js +15 -0
  27. package/lib/cjs/utils/onekey/signTx.d.ts +26 -0
  28. package/lib/cjs/utils/onekey/signTx.js +296 -0
  29. package/lib/cjs/utils/onekey/transaction.d.ts +5 -0
  30. package/lib/cjs/utils/onekey/transaction.js +49 -0
  31. package/lib/cjs/utils/onekey/txToOneKey.d.ts +67 -0
  32. package/lib/cjs/utils/onekey/txToOneKey.js +504 -0
  33. package/lib/cjs/utils/onekey/types.d.ts +79 -0
  34. package/lib/cjs/utils/onekey/types.js +16 -0
  35. package/lib/cjs/utils/trezor/index.d.ts +3 -0
  36. package/lib/cjs/utils/trezor/index.js +9 -0
  37. package/lib/cjs/utils/trezor/sign.d.ts +5 -0
  38. package/lib/cjs/utils/trezor/sign.js +69 -0
  39. package/lib/cjs/utils/trezor/transformations.d.ts +17 -0
  40. package/lib/cjs/utils/trezor/transformations.js +66 -0
  41. package/lib/esm/constants/index.d.ts +40 -0
  42. package/lib/esm/constants/index.js +43 -0
  43. package/lib/esm/index.d.ts +6 -0
  44. package/lib/esm/index.js +73 -0
  45. package/lib/esm/methods/largestFirst.d.ts +2 -0
  46. package/lib/esm/methods/largestFirst.js +174 -0
  47. package/lib/esm/methods/randomImprove.d.ts +2 -0
  48. package/lib/esm/methods/randomImprove.js +160 -0
  49. package/lib/esm/types/trezor.d.ts +45 -0
  50. package/lib/esm/types/trezor.js +5 -0
  51. package/lib/esm/types/types.d.ts +116 -0
  52. package/lib/esm/types/types.js +14 -0
  53. package/lib/esm/utils/common.d.ts +46 -0
  54. package/lib/esm/utils/common.js +460 -0
  55. package/lib/esm/utils/errors.d.ts +8 -0
  56. package/lib/esm/utils/errors.js +9 -0
  57. package/lib/esm/utils/logger.d.ts +5 -0
  58. package/lib/esm/utils/logger.js +23 -0
  59. package/lib/esm/utils/onekey/dapp.d.ts +14 -0
  60. package/lib/esm/utils/onekey/dapp.js +216 -0
  61. package/lib/esm/utils/onekey/error.d.ts +28 -0
  62. package/lib/esm/utils/onekey/error.js +22 -0
  63. package/lib/esm/utils/onekey/index.d.ts +48 -0
  64. package/lib/esm/utils/onekey/index.js +11 -0
  65. package/lib/esm/utils/onekey/signTx.d.ts +26 -0
  66. package/lib/esm/utils/onekey/signTx.js +266 -0
  67. package/lib/esm/utils/onekey/transaction.d.ts +5 -0
  68. package/lib/esm/utils/onekey/transaction.js +42 -0
  69. package/lib/esm/utils/onekey/txToOneKey.d.ts +67 -0
  70. package/lib/esm/utils/onekey/txToOneKey.js +477 -0
  71. package/lib/esm/utils/onekey/types.d.ts +79 -0
  72. package/lib/esm/utils/onekey/types.js +15 -0
  73. package/lib/esm/utils/trezor/index.d.ts +3 -0
  74. package/lib/esm/utils/trezor/index.js +3 -0
  75. package/lib/esm/utils/trezor/sign.d.ts +5 -0
  76. package/lib/esm/utils/trezor/sign.js +42 -0
  77. package/lib/esm/utils/trezor/transformations.d.ts +17 -0
  78. package/lib/esm/utils/trezor/transformations.js +60 -0
  79. package/package.json +61 -0
@@ -0,0 +1,45 @@
1
+ import { CardanoAddressType } from './types';
2
+ export interface CardanoInput {
3
+ path: string | number[];
4
+ prev_hash: string;
5
+ prev_index: number;
6
+ }
7
+ export declare type CardanoToken = {
8
+ assetNameBytes: string;
9
+ amount: string;
10
+ };
11
+ export declare type CardanoAssetGroup = {
12
+ policyId: string;
13
+ tokenAmounts: CardanoToken[];
14
+ };
15
+ export interface CardanoCertificatePointer {
16
+ blockIndex: number;
17
+ txIndex: number;
18
+ certificateIndex: number;
19
+ }
20
+ export interface CardanoAddressParameters {
21
+ addressType: CardanoAddressType;
22
+ path: string | number[];
23
+ stakingPath?: string | number[];
24
+ stakingKeyHash?: string;
25
+ certificatePointer?: CardanoCertificatePointer;
26
+ }
27
+ export declare type CardanoOutput = {
28
+ addressParameters: CardanoAddressParameters;
29
+ amount: string;
30
+ tokenBundle?: CardanoAssetGroup[];
31
+ } | {
32
+ address: string;
33
+ amount: string;
34
+ tokenBundle?: CardanoAssetGroup[];
35
+ };
36
+ export declare enum CardanoTxWitnessType {
37
+ BYRON_WITNESS = 0,
38
+ SHELLEY_WITNESS = 1
39
+ }
40
+ export declare type CardanoSignedTxWitness = {
41
+ type: CardanoTxWitnessType;
42
+ pubKey: string;
43
+ signature: string;
44
+ chainCode?: string | null;
45
+ };
@@ -0,0 +1,5 @@
1
+ export var CardanoTxWitnessType;
2
+ (function (CardanoTxWitnessType) {
3
+ CardanoTxWitnessType[CardanoTxWitnessType["BYRON_WITNESS"] = 0] = "BYRON_WITNESS";
4
+ CardanoTxWitnessType[CardanoTxWitnessType["SHELLEY_WITNESS"] = 1] = "SHELLEY_WITNESS";
5
+ })(CardanoTxWitnessType || (CardanoTxWitnessType = {}));
@@ -0,0 +1,116 @@
1
+ import * as CardanoWasm from '@emurgo/cardano-serialization-lib-asmjs';
2
+ import { BigNum } from '@emurgo/cardano-serialization-lib-asmjs';
3
+ import { CertificateType } from '../constants';
4
+ export interface Asset {
5
+ unit: string;
6
+ quantity: string;
7
+ }
8
+ export interface Utxo {
9
+ address: string;
10
+ txHash: string;
11
+ outputIndex: number;
12
+ amount: Asset[];
13
+ }
14
+ export interface CardanoCertificatePointer {
15
+ blockIndex: number;
16
+ txIndex: number;
17
+ certificateIndex: number;
18
+ }
19
+ export interface BaseOutput {
20
+ setMax?: boolean;
21
+ isChange?: boolean;
22
+ assets: Asset[];
23
+ }
24
+ export interface ExternalOutput extends BaseOutput {
25
+ amount: string;
26
+ address: string;
27
+ setMax?: false;
28
+ }
29
+ export interface ExternalOutputIncomplete extends BaseOutput {
30
+ amount?: string | undefined;
31
+ address?: string;
32
+ setMax: boolean;
33
+ }
34
+ export interface ChangeOutput extends BaseOutput {
35
+ amount: string;
36
+ address: string;
37
+ isChange: true;
38
+ }
39
+ export declare type FinalOutput = ExternalOutput | ChangeOutput;
40
+ export declare type UserOutput = ExternalOutput | ExternalOutputIncomplete;
41
+ export declare type Output = FinalOutput | ExternalOutputIncomplete;
42
+ export interface OutputCost {
43
+ output: CardanoWasm.TransactionOutput;
44
+ outputFee: BigNum;
45
+ minOutputAmount: BigNum;
46
+ }
47
+ export declare enum CardanoAddressType {
48
+ BASE = 0,
49
+ BASE_SCRIPT_KEY = 1,
50
+ BASE_KEY_SCRIPT = 2,
51
+ BASE_SCRIPT_SCRIPT = 3,
52
+ POINTER = 4,
53
+ POINTER_SCRIPT = 5,
54
+ ENTERPRISE = 6,
55
+ ENTERPRISE_SCRIPT = 7,
56
+ BYRON = 8,
57
+ REWARD = 14,
58
+ REWARD_SCRIPT = 15
59
+ }
60
+ export interface CoinSelectionResult {
61
+ tx: {
62
+ body: string;
63
+ hash: string;
64
+ size: number;
65
+ };
66
+ inputs: Utxo[];
67
+ outputs: Output[];
68
+ fee: string;
69
+ totalSpent: string;
70
+ deposit: string;
71
+ withdrawal: string;
72
+ ttl?: number;
73
+ max?: string;
74
+ }
75
+ export declare type PrecomposedTransaction = ({
76
+ type: 'final';
77
+ outputs: FinalOutput[];
78
+ } & Omit<CoinSelectionResult, 'outputs'>) | ({
79
+ type: 'nonfinal';
80
+ } & Pick<CoinSelectionResult, 'fee' | 'totalSpent' | 'deposit' | 'withdrawal' | 'max'>);
81
+ export interface Withdrawal {
82
+ stakeAddress: string;
83
+ amount: string;
84
+ }
85
+ export declare type CertificateTypeType = typeof CertificateType;
86
+ export interface CertificateStakeRegistration {
87
+ type: CertificateTypeType['STAKE_REGISTRATION'] | CertificateTypeType['STAKE_DEREGISTRATION'];
88
+ stakingKeyHash?: string;
89
+ }
90
+ export interface CertificateStakeDelegation {
91
+ type: CertificateTypeType['STAKE_DELEGATION'];
92
+ stakingKeyHash?: string;
93
+ pool: string;
94
+ }
95
+ export interface CertificateStakePoolRegistration {
96
+ type: CertificateTypeType['STAKE_POOL_REGISTRATION'];
97
+ pool_parameters: Record<string, unknown>;
98
+ }
99
+ export declare type Certificate = CertificateStakeRegistration | CertificateStakeDelegation | CertificateStakePoolRegistration;
100
+ export interface Options {
101
+ feeParams?: {
102
+ a: string;
103
+ };
104
+ debug?: boolean;
105
+ forceLargestFirstSelection?: boolean;
106
+ _maxTokensPerOutput?: number;
107
+ }
108
+ export interface CoinSelectionParams {
109
+ utxos: Utxo[];
110
+ outputs: UserOutput[];
111
+ changeAddress: string;
112
+ certificates: Certificate[];
113
+ withdrawals: Withdrawal[];
114
+ accountPubKey: string;
115
+ ttl?: number;
116
+ }
@@ -0,0 +1,14 @@
1
+ export var CardanoAddressType;
2
+ (function (CardanoAddressType) {
3
+ CardanoAddressType[CardanoAddressType["BASE"] = 0] = "BASE";
4
+ CardanoAddressType[CardanoAddressType["BASE_SCRIPT_KEY"] = 1] = "BASE_SCRIPT_KEY";
5
+ CardanoAddressType[CardanoAddressType["BASE_KEY_SCRIPT"] = 2] = "BASE_KEY_SCRIPT";
6
+ CardanoAddressType[CardanoAddressType["BASE_SCRIPT_SCRIPT"] = 3] = "BASE_SCRIPT_SCRIPT";
7
+ CardanoAddressType[CardanoAddressType["POINTER"] = 4] = "POINTER";
8
+ CardanoAddressType[CardanoAddressType["POINTER_SCRIPT"] = 5] = "POINTER_SCRIPT";
9
+ CardanoAddressType[CardanoAddressType["ENTERPRISE"] = 6] = "ENTERPRISE";
10
+ CardanoAddressType[CardanoAddressType["ENTERPRISE_SCRIPT"] = 7] = "ENTERPRISE_SCRIPT";
11
+ CardanoAddressType[CardanoAddressType["BYRON"] = 8] = "BYRON";
12
+ CardanoAddressType[CardanoAddressType["REWARD"] = 14] = "REWARD";
13
+ CardanoAddressType[CardanoAddressType["REWARD_SCRIPT"] = 15] = "REWARD_SCRIPT";
14
+ })(CardanoAddressType || (CardanoAddressType = {}));
@@ -0,0 +1,46 @@
1
+ import * as CardanoWasm from '@emurgo/cardano-serialization-lib-asmjs';
2
+ import { CARDANO_PARAMS } from '../constants';
3
+ import { Certificate, Output, Utxo, Withdrawal, OutputCost, UserOutput, Asset } from '../types/types';
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'];
7
+ export declare const parseAsset: (hex: string) => {
8
+ policyId: string;
9
+ assetNameInHex: string;
10
+ };
11
+ export declare const buildMultiAsset: (assets: Asset[]) => CardanoWasm.MultiAsset;
12
+ export declare const multiAssetToArray: (multiAsset: CardanoWasm.MultiAsset | undefined) => Asset[];
13
+ export declare const getAssetAmount: (obj: Pick<Utxo, 'amount'>, asset?: string) => string;
14
+ export declare const getUtxoQuantity: (utxos: Utxo[], asset?: string) => CardanoWasm.BigNum;
15
+ export declare const getOutputQuantity: (outputs: Output[], asset?: string) => CardanoWasm.BigNum;
16
+ export declare const sortUtxos: (utxos: Utxo[], asset?: string) => Utxo[];
17
+ export declare const buildTxInput: (utxo: Utxo) => {
18
+ input: CardanoWasm.TransactionInput;
19
+ address: CardanoWasm.Address;
20
+ amount: CardanoWasm.Value;
21
+ };
22
+ export declare const buildTxOutput: (output: Output, dummyAddress: string) => CardanoWasm.TransactionOutput;
23
+ export declare const getOutputCost: (txBuilder: CardanoWasm.TransactionBuilder, output: Output, dummyAddress: string) => OutputCost;
24
+ export declare const prepareWithdrawals: (withdrawals: Withdrawal[]) => CardanoWasm.Withdrawals;
25
+ export declare const prepareCertificates: (certificates: Certificate[], accountKey: CardanoWasm.Bip32PublicKey) => CardanoWasm.Certificates;
26
+ export declare const calculateRequiredDeposit: (certificates: Certificate[]) => number;
27
+ export declare const setMinUtxoValueForOutputs: (txBuilder: CardanoWasm.TransactionBuilder, outputs: UserOutput[], dummyAddress: string) => UserOutput[];
28
+ export declare const splitChangeOutput: (txBuilder: CardanoWasm.TransactionBuilder, singleChangeOutput: OutputCost, changeAddress: string, maxTokensPerOutput?: number) => OutputCost[];
29
+ export declare const prepareChangeOutput: (txBuilder: CardanoWasm.TransactionBuilder, usedUtxos: Utxo[], preparedOutputs: Output[], changeAddress: string, utxosTotalAmount: CardanoWasm.BigNum, totalOutputAmount: CardanoWasm.BigNum, totalFeesAmount: CardanoWasm.BigNum, pickAdditionalUtxo?: () => ReturnType<typeof getRandomUtxo>) => OutputCost | null;
30
+ export declare const getTxBuilder: (a?: string) => CardanoWasm.TransactionBuilder;
31
+ export declare const getUnsatisfiedAssets: (selectedUtxos: Utxo[], outputs: Output[]) => string[];
32
+ export declare const getInitialUtxoSet: (utxos: Utxo[], maxOutput: UserOutput | undefined) => {
33
+ used: Utxo[];
34
+ remaining: Utxo[];
35
+ };
36
+ export declare const setMaxOutput: (maxOutput: UserOutput, changeOutput: OutputCost | null) => {
37
+ maxOutput: UserOutput;
38
+ };
39
+ export declare const getUserOutputQuantityWithDeposit: (outputs: UserOutput[], deposit: number, asset?: string) => CardanoWasm.BigNum;
40
+ export declare const filterUtxos: (utxos: Utxo[], asset: string) => Utxo[];
41
+ export declare const getRandomUtxo: (txBuilder: CardanoWasm.TransactionBuilder, utxoRemaining: Utxo[], utxoSelected: Utxo[]) => {
42
+ utxo: Utxo;
43
+ addUtxo: () => void;
44
+ } | null;
45
+ export declare const calculateUserOutputsFee: (txBuilder: CardanoWasm.TransactionBuilder, userOutputs: UserOutput[], changeAddress: string) => CardanoWasm.BigNum;
46
+ export declare const orderInputs: (inputsToOrder: Utxo[], txBody: CardanoWasm.TransactionBody) => Utxo[];