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

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 (47) 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 +3 -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 +3 -2
  19. package/lib/cjs/utils/onekey/txToOneKey.js +31 -22
  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 +3 -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 +3 -2
  41. package/lib/esm/utils/onekey/txToOneKey.js +27 -18
  42. package/lib/esm/utils/onekey/types.d.ts +9 -9
  43. package/lib/esm/utils/trezor/index.d.ts +2 -2
  44. package/lib/esm/utils/trezor/index.js +2 -2
  45. package/lib/esm/utils/trezor/transformations.d.ts +5 -1
  46. package/lib/esm/utils/trezor/transformations.js +25 -0
  47. package/package.json +20 -16
@@ -45,6 +45,19 @@ const generateKeys = (keys, xpub) => {
45
45
  stake: Object.assign(Object.assign({}, keys.stake), { hash: stakeKeyHash }),
46
46
  };
47
47
  };
48
+ const sortCanonicallyInPlace = (items, selector) => {
49
+ return items.sort((a, b) => {
50
+ const itemA = selector(a);
51
+ const itemB = selector(b);
52
+ if (itemA.length === itemB.length) {
53
+ return itemA > itemB ? 1 : -1;
54
+ }
55
+ else if (itemA.length > itemB.length)
56
+ return 1;
57
+ else
58
+ return -1;
59
+ });
60
+ };
48
61
  const outputsToOneKey = (outputs, changeAddress) => {
49
62
  const onekeyOutputs = [];
50
63
  for (let i = 0; i < outputs.len(); i++) {
@@ -66,20 +79,13 @@ const outputsToOneKey = (outputs, changeAddress) => {
66
79
  });
67
80
  }
68
81
  // sort canonical
69
- tokens.sort((a, b) => {
70
- if (a.assetNameBytes.length == b.assetNameBytes.length) {
71
- return a.assetNameBytes > b.assetNameBytes ? 1 : -1;
72
- }
73
- else if (a.assetNameBytes.length > b.assetNameBytes.length)
74
- return 1;
75
- else
76
- return -1;
77
- });
82
+ sortCanonicallyInPlace(tokens, item => item.assetNameBytes);
78
83
  tokenBundle.push({
79
84
  policyId: Buffer.from(policy.to_bytes()).toString('hex'),
80
85
  tokenAmounts: tokens,
81
86
  });
82
87
  }
88
+ sortCanonicallyInPlace(tokenBundle, item => item.policyId);
83
89
  }
84
90
  const outputAddressBech32 = output.address().to_bech32();
85
91
  const outputAddressHuman = (() => {
@@ -347,15 +353,7 @@ export const txToOneKey = async (rawTx, network, initKeys, xpub, changeAddress)
347
353
  });
348
354
  }
349
355
  // sort canonical
350
- tokens.sort((a, b) => {
351
- if (a.assetNameBytes.length == b.assetNameBytes.length) {
352
- return a.assetNameBytes > b.assetNameBytes ? 1 : -1;
353
- }
354
- else if (a.assetNameBytes.length > b.assetNameBytes.length)
355
- return 1;
356
- else
357
- return -1;
358
- });
356
+ sortCanonicallyInPlace(tokens, item => item.assetNameBytes);
359
357
  mintBundle.push({
360
358
  policyId: Buffer.from(policy.to_bytes()).toString('hex'),
361
359
  tokenAmounts: tokens,
@@ -443,6 +441,16 @@ export const txToOneKey = async (rawTx, network, initKeys, xpub, changeAddress)
443
441
  return null;
444
442
  })();
445
443
  const includeNetworkId = !!tx.body().network_id();
444
+ let tagCborSets = false;
445
+ try {
446
+ const tagCBOR = CardanoWasm.has_transaction_set_tag(tx.to_bytes()).valueOf();
447
+ tagCborSets =
448
+ tagCBOR === CardanoWasm.TransactionSetsState.AllSetsHaveTag.valueOf() ||
449
+ tagCBOR === CardanoWasm.TransactionSetsState.MixedSets.valueOf();
450
+ }
451
+ catch (error) {
452
+ // ignore
453
+ }
446
454
  const onekeyTx = {
447
455
  signingMode,
448
456
  outputs: onekeyOutputs,
@@ -463,6 +471,7 @@ export const txToOneKey = async (rawTx, network, initKeys, xpub, changeAddress)
463
471
  collateralReturn,
464
472
  totalCollateral,
465
473
  referenceInputs,
474
+ tagCborSets,
466
475
  };
467
476
  Object.keys(onekeyTx).forEach(key => !onekeyTx[key] && onekeyTx[key] != 0 && delete onekeyTx[key]);
468
477
  console.log('format onekey cardano hardware Tx =====>>> : ', onekeyTx);
@@ -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,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,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 {};
@@ -1,3 +1,5 @@
1
+ import * as CardanoWasm from '@emurgo/cardano-serialization-lib-asmjs';
2
+ import { CardanoDRepType } from '../../types/types';
1
3
  import { parseAsset } from '../common';
2
4
  export const transformToTokenBundle = (assets) => {
3
5
  // prepare token bundle used in trezor output
@@ -58,3 +60,26 @@ export const transformToTrezorOutputs = (outputs, changeAddressParameters) => {
58
60
  return Object.assign(Object.assign({}, params), { amount: output.amount, tokenBundle: transformToTokenBundle(output.assets) });
59
61
  });
60
62
  };
63
+ export const drepIdToHex = (drepId) => {
64
+ var _a, _b;
65
+ const drep = CardanoWasm.DRep.from_bech32(drepId);
66
+ const kind = drep.kind();
67
+ let drepHex;
68
+ switch (kind) {
69
+ case CardanoDRepType.KEY_HASH:
70
+ drepHex = (_a = drep.to_key_hash()) === null || _a === void 0 ? void 0 : _a.to_hex();
71
+ break;
72
+ case CardanoDRepType.SCRIPT_HASH:
73
+ drepHex = (_b = drep.to_script_hash()) === null || _b === void 0 ? void 0 : _b.to_hex();
74
+ break;
75
+ }
76
+ if (!drepHex) {
77
+ throw Error('Invalid drepId');
78
+ }
79
+ const drepData = {
80
+ type: kind,
81
+ hex: drepHex,
82
+ };
83
+ drep.free();
84
+ return drepData;
85
+ };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@onekeyfe/cardano-coin-selection-asmjs",
3
- "version": "1.1.1",
3
+ "version": "1.1.3",
4
4
  "description": "",
5
5
  "keywords": [
6
6
  "coin selection"
@@ -32,30 +32,34 @@
32
32
  "test:badges": "make-coverage-badge --output-path ./docs/badge-coverage.svg"
33
33
  },
34
34
  "devDependencies": {
35
- "@emurgo/cardano-serialization-lib-nodejs": "11.0.0",
36
- "@swc-node/jest": "^1.5.2",
37
- "@types/jest": "^28.1.6",
35
+ "@emurgo/cardano-serialization-lib-nodejs": "13.2.0",
36
+ "@eslint/js": "^9.11.1",
37
+ "@swc-node/jest": "^1.8.12",
38
+ "@swc/core": "1.7.26",
39
+ "@swc/helpers": "^0.5.13",
40
+ "@types/jest": "^29.5.13",
38
41
  "@types/node": "^16.3.2",
39
- "@typescript-eslint/eslint-plugin": "^5.32.0",
40
- "@typescript-eslint/parser": "5.32.0",
42
+ "@typescript-eslint/eslint-plugin": "^8.8.0",
43
+ "@typescript-eslint/parser": "8.8.0",
41
44
  "bignumber.js": "^9.0.1",
42
- "eslint": "8.21.0",
43
- "eslint-config-prettier": "8.5.0",
44
- "eslint-plugin-import": "2.26.0",
45
- "eslint-plugin-prettier": "4.2.1",
46
- "jest": "^28.1.3",
45
+ "eslint": "^9.11.1",
46
+ "eslint-config-prettier": "^9.1.0",
47
+ "eslint-plugin-import": "^2.30.0",
48
+ "eslint-plugin-prettier": "^5.2.1",
49
+ "jest": "^29.7.0",
47
50
  "make-coverage-badge": "^1.2.0",
48
51
  "npm-run-all": "^4.1.5",
49
- "prettier": "2.7.1",
50
- "rimraf": "^3.0.2",
51
- "typescript": "^4.7.4"
52
+ "prettier": "3.3.3",
53
+ "rimraf": "^6.0.1",
54
+ "typescript": "^5.6.2",
55
+ "typescript-eslint": "^8.8.0"
52
56
  },
53
57
  "dependencies": {
54
58
  "@emurgo/cardano-message-signing-asmjs": "^1.0.1",
55
- "@emurgo/cardano-serialization-lib-asmjs": "11.1.0"
59
+ "@emurgo/cardano-serialization-lib-asmjs": "13.2.0"
56
60
  },
57
61
  "peerDependencies": {
58
62
  "bignumber.js": "^9.0.1"
59
63
  },
60
- "packageManager": "yarn@3.2.2"
64
+ "packageManager": "yarn@4.5.0"
61
65
  }