@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,42 @@
1
+ import * as CardanoWasm from '@emurgo/cardano-serialization-lib-asmjs';
2
+ import { CardanoTxWitnessType, } from '../../types/trezor';
3
+ import { getProtocolMagic } from '../common';
4
+ export const signTransaction = (txBodyHex,
5
+ // txMetadata: CardanoWasm.AuxiliaryData,
6
+ signedWitnesses, options) => {
7
+ const txBody = CardanoWasm.TransactionBody.from_bytes(Uint8Array.from(Buffer.from(txBodyHex, 'hex')));
8
+ const witnesses = CardanoWasm.TransactionWitnessSet.new();
9
+ const vkeyWitnesses = CardanoWasm.Vkeywitnesses.new();
10
+ const bootstrapWitnesses = CardanoWasm.BootstrapWitnesses.new();
11
+ signedWitnesses.forEach(w => {
12
+ const vKey = CardanoWasm.Vkey.new(CardanoWasm.PublicKey.from_bytes(Buffer.from(w.pubKey, 'hex')));
13
+ const signature = CardanoWasm.Ed25519Signature.from_bytes(Buffer.from(w.signature, 'hex'));
14
+ if (w.type === CardanoTxWitnessType.SHELLEY_WITNESS) {
15
+ // Shelley witness
16
+ const vKeyWitness = CardanoWasm.Vkeywitness.new(vKey, signature);
17
+ vkeyWitnesses.add(vKeyWitness);
18
+ }
19
+ else if (w.type === CardanoTxWitnessType.BYRON_WITNESS) {
20
+ // Byron witness (TODO: not used, needs testing)
21
+ if (w.chainCode) {
22
+ const xpubHex = `${w.pubKey}${w.chainCode}`;
23
+ const bip32Key = CardanoWasm.Bip32PublicKey.from_bytes(Buffer.from(xpubHex, 'hex'));
24
+ const byronAddress = CardanoWasm.ByronAddress.icarus_from_key(bip32Key, getProtocolMagic(!!(options === null || options === void 0 ? void 0 : options.testnet)));
25
+ const bootstrapWitness = CardanoWasm.BootstrapWitness.new(vKey, signature, Buffer.from(w.chainCode, 'hex'), byronAddress.attributes());
26
+ bootstrapWitnesses.add(bootstrapWitness);
27
+ }
28
+ }
29
+ });
30
+ if (bootstrapWitnesses.len() > 0) {
31
+ witnesses.set_bootstraps(bootstrapWitnesses);
32
+ }
33
+ if (vkeyWitnesses.len() > 0) {
34
+ witnesses.set_vkeys(vkeyWitnesses);
35
+ }
36
+ if (options === null || options === void 0 ? void 0 : options.signOnly) {
37
+ return Promise.resolve(Buffer.from(witnesses.to_bytes(), 'utf8').toString('hex'));
38
+ }
39
+ const transaction = CardanoWasm.Transaction.new(txBody, witnesses);
40
+ const serializedTx = Buffer.from(transaction.to_bytes()).toString('hex');
41
+ return Promise.resolve(serializedTx);
42
+ };
@@ -0,0 +1,17 @@
1
+ import { CardanoAddressParameters, CardanoInput, CardanoOutput } from '../../types/trezor';
2
+ import { Asset, FinalOutput, Utxo } from '../../types/types';
3
+ interface AssetInPolicy {
4
+ assetNameBytes: string;
5
+ amount: string;
6
+ }
7
+ export declare const transformToTokenBundle: (assets: Asset[]) => {
8
+ policyId: string;
9
+ tokenAmounts: AssetInPolicy[];
10
+ }[] | undefined;
11
+ export declare const transformToTrezorInputs: (utxos: Utxo[], trezorUtxos: {
12
+ txid: string;
13
+ vout: number;
14
+ path: string;
15
+ }[]) => CardanoInput[];
16
+ export declare const transformToTrezorOutputs: (outputs: FinalOutput[], changeAddressParameters: CardanoAddressParameters) => CardanoOutput[];
17
+ export {};
@@ -0,0 +1,60 @@
1
+ import { parseAsset } from '../common';
2
+ export const transformToTokenBundle = (assets) => {
3
+ // prepare token bundle used in trezor output
4
+ if (assets.length === 0)
5
+ return undefined;
6
+ const uniquePolicies = [];
7
+ assets.forEach(asset => {
8
+ const { policyId } = parseAsset(asset.unit);
9
+ if (!uniquePolicies.includes(policyId)) {
10
+ uniquePolicies.push(policyId);
11
+ }
12
+ });
13
+ const assetsByPolicy = [];
14
+ uniquePolicies.forEach(policyId => {
15
+ const assetsInPolicy = [];
16
+ assets.forEach(asset => {
17
+ const assetInfo = parseAsset(asset.unit);
18
+ if (assetInfo.policyId !== policyId)
19
+ return;
20
+ assetsInPolicy.push({
21
+ assetNameBytes: assetInfo.assetNameInHex,
22
+ amount: asset.quantity,
23
+ });
24
+ }),
25
+ assetsByPolicy.push({
26
+ policyId,
27
+ tokenAmounts: assetsInPolicy,
28
+ });
29
+ });
30
+ return assetsByPolicy;
31
+ };
32
+ export const transformToTrezorInputs = (utxos, trezorUtxos) => {
33
+ return utxos.map(utxo => {
34
+ const utxoWithPath = trezorUtxos.find(u => u.txid === utxo.txHash && u.vout === utxo.outputIndex);
35
+ // shouldn't happen since utxos should be subset of trezorUtxos (with different shape/fields)
36
+ if (!utxoWithPath)
37
+ throw Error(`Cannot transform utxo ${utxo.txHash}:${utxo.outputIndex}`);
38
+ return {
39
+ path: utxoWithPath.path,
40
+ prev_hash: utxo.txHash,
41
+ prev_index: utxo.outputIndex,
42
+ };
43
+ });
44
+ };
45
+ export const transformToTrezorOutputs = (outputs, changeAddressParameters) => {
46
+ return outputs.map(output => {
47
+ let params;
48
+ if (output.isChange) {
49
+ params = {
50
+ addressParameters: changeAddressParameters,
51
+ };
52
+ }
53
+ else {
54
+ params = {
55
+ address: output.address,
56
+ };
57
+ }
58
+ return Object.assign(Object.assign({}, params), { amount: output.amount, tokenBundle: transformToTokenBundle(output.assets) });
59
+ });
60
+ };
package/package.json ADDED
@@ -0,0 +1,61 @@
1
+ {
2
+ "name": "@onekeyfe/cardano-coin-selection-asmjs",
3
+ "version": "1.1.0",
4
+ "description": "",
5
+ "keywords": [
6
+ "coin selection"
7
+ ],
8
+ "license": "MIT",
9
+ "author": "OneKeyHQ",
10
+ "homepage": "https://github.com/OneKeyHQ/coin-selection",
11
+ "repository": {
12
+ "type": "git",
13
+ "url": "git+https://github.com/OneKeyHQ/coin-selection.git"
14
+ },
15
+ "publishConfig": {
16
+ "access": "public"
17
+ },
18
+ "main": "lib/cjs/index.js",
19
+ "module": "lib/esm/index.js",
20
+ "files": [
21
+ "lib/**/*.js",
22
+ "lib/**/*.ts"
23
+ ],
24
+ "scripts": {
25
+ "build": "yarn clean && tsc -p tsconfig.esm.json && tsc -p tsconfig.cjs.json",
26
+ "clean": "rimraf lib",
27
+ "lint": "eslint ./src/**/*.ts",
28
+ "prepublishOnly": "yarn build",
29
+ "type-check": "yarn tsc -p tsconfig.types.json",
30
+ "test": "yarn run-s 'test:*'",
31
+ "test:unit": "jest -c ./jest.config.js",
32
+ "test:badges": "make-coverage-badge --output-path ./docs/badge-coverage.svg"
33
+ },
34
+ "devDependencies": {
35
+ "@emurgo/cardano-serialization-lib-nodejs": "11.0.0",
36
+ "@swc-node/jest": "^1.5.2",
37
+ "@types/jest": "^28.1.6",
38
+ "@types/node": "^16.3.2",
39
+ "@typescript-eslint/eslint-plugin": "^5.32.0",
40
+ "@typescript-eslint/parser": "5.32.0",
41
+ "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",
47
+ "make-coverage-badge": "^1.2.0",
48
+ "npm-run-all": "^4.1.5",
49
+ "prettier": "2.7.1",
50
+ "rimraf": "^3.0.2",
51
+ "typescript": "^4.7.4"
52
+ },
53
+ "dependencies": {
54
+ "@emurgo/cardano-message-signing-asmjs": "^1.0.1",
55
+ "@emurgo/cardano-serialization-lib-asmjs": "11.1.0"
56
+ },
57
+ "peerDependencies": {
58
+ "bignumber.js": "^9.0.1"
59
+ },
60
+ "packageManager": "yarn@3.2.2"
61
+ }