@meshsdk/core-csl 1.5.28

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 (102) hide show
  1. package/README.md +5 -0
  2. package/dist/cjs/core/adaptor/data.d.ts +3 -0
  3. package/dist/cjs/core/adaptor/data.js +21 -0
  4. package/dist/cjs/core/adaptor/index.d.ts +15 -0
  5. package/dist/cjs/core/adaptor/index.js +25 -0
  6. package/dist/cjs/core/adaptor/mint.d.ts +5 -0
  7. package/dist/cjs/core/adaptor/mint.js +42 -0
  8. package/dist/cjs/core/adaptor/output.d.ts +2 -0
  9. package/dist/cjs/core/adaptor/output.js +33 -0
  10. package/dist/cjs/core/adaptor/script.d.ts +3 -0
  11. package/dist/cjs/core/adaptor/script.js +46 -0
  12. package/dist/cjs/core/adaptor/txIn.d.ts +5 -0
  13. package/dist/cjs/core/adaptor/txIn.js +76 -0
  14. package/dist/cjs/core/adaptor/utxo.d.ts +2 -0
  15. package/dist/cjs/core/adaptor/utxo.js +20 -0
  16. package/dist/cjs/core/adaptor/withdrawal.d.ts +2 -0
  17. package/dist/cjs/core/adaptor/withdrawal.js +30 -0
  18. package/dist/cjs/core/index.d.ts +1 -0
  19. package/dist/cjs/core/index.js +17 -0
  20. package/dist/cjs/core/serializer.d.ts +12 -0
  21. package/dist/cjs/core/serializer.js +111 -0
  22. package/dist/cjs/deser/constants.d.ts +13 -0
  23. package/dist/cjs/deser/constants.js +16 -0
  24. package/dist/cjs/deser/converter.d.ts +17 -0
  25. package/dist/cjs/deser/converter.js +203 -0
  26. package/dist/cjs/deser/csl.d.ts +1 -0
  27. package/dist/cjs/deser/csl.js +27 -0
  28. package/dist/cjs/deser/deserializer.d.ts +19 -0
  29. package/dist/cjs/deser/deserializer.js +40 -0
  30. package/dist/cjs/deser/index.d.ts +5 -0
  31. package/dist/cjs/deser/index.js +21 -0
  32. package/dist/cjs/deser/resolver.d.ts +12 -0
  33. package/dist/cjs/deser/resolver.js +90 -0
  34. package/dist/cjs/index.d.ts +3 -0
  35. package/dist/cjs/index.js +19 -0
  36. package/dist/cjs/utils/address.d.ts +9 -0
  37. package/dist/cjs/utils/address.js +61 -0
  38. package/dist/cjs/utils/aiken.d.ts +17 -0
  39. package/dist/cjs/utils/aiken.js +52 -0
  40. package/dist/cjs/utils/credentials.d.ts +1 -0
  41. package/dist/cjs/utils/credentials.js +10 -0
  42. package/dist/cjs/utils/datum.d.ts +4 -0
  43. package/dist/cjs/utils/datum.js +18 -0
  44. package/dist/cjs/utils/index.d.ts +7 -0
  45. package/dist/cjs/utils/index.js +23 -0
  46. package/dist/cjs/utils/scripts.d.ts +1 -0
  47. package/dist/cjs/utils/scripts.js +6 -0
  48. package/dist/cjs/utils/staking.d.ts +4 -0
  49. package/dist/cjs/utils/staking.js +34 -0
  50. package/dist/cjs/utils/transaction.d.ts +2 -0
  51. package/dist/cjs/utils/transaction.js +15 -0
  52. package/dist/mjs/core/adaptor/data.d.ts +3 -0
  53. package/dist/mjs/core/adaptor/data.js +16 -0
  54. package/dist/mjs/core/adaptor/index.d.ts +15 -0
  55. package/dist/mjs/core/adaptor/index.js +21 -0
  56. package/dist/mjs/core/adaptor/mint.d.ts +5 -0
  57. package/dist/mjs/core/adaptor/mint.js +35 -0
  58. package/dist/mjs/core/adaptor/output.d.ts +2 -0
  59. package/dist/mjs/core/adaptor/output.js +29 -0
  60. package/dist/mjs/core/adaptor/script.d.ts +3 -0
  61. package/dist/mjs/core/adaptor/script.js +39 -0
  62. package/dist/mjs/core/adaptor/txIn.d.ts +5 -0
  63. package/dist/mjs/core/adaptor/txIn.js +68 -0
  64. package/dist/mjs/core/adaptor/utxo.d.ts +2 -0
  65. package/dist/mjs/core/adaptor/utxo.js +16 -0
  66. package/dist/mjs/core/adaptor/withdrawal.d.ts +2 -0
  67. package/dist/mjs/core/adaptor/withdrawal.js +26 -0
  68. package/dist/mjs/core/index.d.ts +1 -0
  69. package/dist/mjs/core/index.js +1 -0
  70. package/dist/mjs/core/serializer.d.ts +12 -0
  71. package/dist/mjs/core/serializer.js +105 -0
  72. package/dist/mjs/deser/constants.d.ts +13 -0
  73. package/dist/mjs/deser/constants.js +13 -0
  74. package/dist/mjs/deser/converter.d.ts +17 -0
  75. package/dist/mjs/deser/converter.js +184 -0
  76. package/dist/mjs/deser/csl.d.ts +1 -0
  77. package/dist/mjs/deser/csl.js +1 -0
  78. package/dist/mjs/deser/deserializer.d.ts +19 -0
  79. package/dist/mjs/deser/deserializer.js +20 -0
  80. package/dist/mjs/deser/index.d.ts +5 -0
  81. package/dist/mjs/deser/index.js +5 -0
  82. package/dist/mjs/deser/resolver.d.ts +12 -0
  83. package/dist/mjs/deser/resolver.js +75 -0
  84. package/dist/mjs/index.d.ts +3 -0
  85. package/dist/mjs/index.js +3 -0
  86. package/dist/mjs/utils/address.d.ts +9 -0
  87. package/dist/mjs/utils/address.js +50 -0
  88. package/dist/mjs/utils/aiken.d.ts +17 -0
  89. package/dist/mjs/utils/aiken.js +47 -0
  90. package/dist/mjs/utils/credentials.d.ts +1 -0
  91. package/dist/mjs/utils/credentials.js +6 -0
  92. package/dist/mjs/utils/datum.d.ts +4 -0
  93. package/dist/mjs/utils/datum.js +13 -0
  94. package/dist/mjs/utils/index.d.ts +7 -0
  95. package/dist/mjs/utils/index.js +7 -0
  96. package/dist/mjs/utils/scripts.d.ts +1 -0
  97. package/dist/mjs/utils/scripts.js +2 -0
  98. package/dist/mjs/utils/staking.d.ts +4 -0
  99. package/dist/mjs/utils/staking.js +25 -0
  100. package/dist/mjs/utils/transaction.d.ts +2 -0
  101. package/dist/mjs/utils/transaction.js +10 -0
  102. package/package.json +45 -0
@@ -0,0 +1,184 @@
1
+ /* eslint-disable consistent-return */
2
+ /* eslint-disable default-case */
3
+ import JSONbig from "json-bigint";
4
+ // import { LANGUAGE_VERSIONS } from ".";
5
+ import { csl } from "./csl";
6
+ import { deserializeEd25519KeyHash, deserializePlutusScript,
7
+ // deserializePlutusScript,
8
+ } from "./deserializer";
9
+ /* -----------------[ Address ]----------------- */
10
+ export const toAddress = (bech32) => csl.Address.from_bech32(bech32);
11
+ export const toBaseAddress = (bech32) => csl.BaseAddress.from_address(toAddress(bech32));
12
+ export const toEnterpriseAddress = (bech32) => csl.EnterpriseAddress.from_address(toAddress(bech32));
13
+ export const toRewardAddress = (bech32) => csl.RewardAddress.from_address(toAddress(bech32));
14
+ /* -----------------[ Bytes ]----------------- */
15
+ export const fromBytes = (bytes) => Buffer.from(bytes).toString("hex");
16
+ export const toBytes = (hex) => {
17
+ if (hex.length % 2 === 0 && /^[0-9A-F]*$/i.test(hex))
18
+ return Buffer.from(hex, "hex");
19
+ return Buffer.from(hex, "utf-8");
20
+ };
21
+ /* -----------------[ UTF-8 ]----------------- */
22
+ export const fromUTF8 = (utf8) => {
23
+ if (utf8.length % 2 === 0 && /^[0-9A-F]*$/i.test(utf8))
24
+ return utf8;
25
+ return fromBytes(Buffer.from(utf8, "utf-8"));
26
+ };
27
+ export const toUTF8 = (hex) => Buffer.from(hex, "hex").toString("utf-8");
28
+ /* -----------------[ Lovelace ]----------------- */
29
+ export const fromLovelace = (lovelace) => lovelace / 1_000_000;
30
+ export const toLovelace = (ada) => ada * 1_000_000;
31
+ // /* -----------------[ NativeScript ]----------------- */
32
+ // export const fromNativeScript = (script: csl.NativeScript) => {
33
+ // const fromNativeScripts = (scripts: csl.NativeScripts) => {
34
+ // const nativeScripts = new Array<NativeScript>();
35
+ // for (let index = 0; index < scripts.len(); index += 1) {
36
+ // nativeScripts.push(fromNativeScript(scripts.get(index)));
37
+ // }
38
+ // return nativeScripts;
39
+ // };
40
+ // switch (script.kind()) {
41
+ // case csl.NativeScriptKind.ScriptAll: {
42
+ // const scriptAll = script.as_script_all()!;
43
+ // return <NativeScript>{
44
+ // type: "all",
45
+ // scripts: fromNativeScripts(scriptAll.native_scripts()),
46
+ // };
47
+ // }
48
+ // case csl.NativeScriptKind.ScriptAny: {
49
+ // const scriptAny = script.as_script_any()!;
50
+ // return <NativeScript>{
51
+ // type: "any",
52
+ // scripts: fromNativeScripts(scriptAny.native_scripts()),
53
+ // };
54
+ // }
55
+ // case csl.NativeScriptKind.ScriptNOfK: {
56
+ // const scriptNOfK = script.as_script_n_of_k()!;
57
+ // return <NativeScript>{
58
+ // type: "atLeast",
59
+ // required: scriptNOfK.n(),
60
+ // scripts: fromNativeScripts(scriptNOfK.native_scripts()),
61
+ // };
62
+ // }
63
+ // case csl.NativeScriptKind.TimelockStart: {
64
+ // const timelockStart = script.as_timelock_start()!;
65
+ // return <NativeScript>{
66
+ // type: "after",
67
+ // slot: timelockStart.slot_bignum().to_str(),
68
+ // };
69
+ // }
70
+ // case csl.NativeScriptKind.TimelockExpiry: {
71
+ // const timelockExpiry = script.as_timelock_expiry()!;
72
+ // return <NativeScript>{
73
+ // type: "before",
74
+ // slot: timelockExpiry.slot_bignum().to_str(),
75
+ // };
76
+ // }
77
+ // case csl.NativeScriptKind.ScriptPubkey: {
78
+ // const scriptPubkey = script.as_script_pubkey()!;
79
+ // return <NativeScript>{
80
+ // type: "sig",
81
+ // keyHash: scriptPubkey.addr_keyhash().to_hex(),
82
+ // };
83
+ // }
84
+ // default:
85
+ // throw new Error(`Script Kind: ${script.kind()}, is not supported`);
86
+ // }
87
+ // };
88
+ // /* -----------------[ Scripts ]----------------- */
89
+ // export const fromScriptRef = (
90
+ // scriptRef: string,
91
+ // ): PlutusScript | NativeScript | undefined => {
92
+ // const script = csl.ScriptRef.from_hex(scriptRef);
93
+ // if (script.is_plutus_script()) {
94
+ // const plutusScript = script.plutus_script()!;
95
+ // return <PlutusScript>{
96
+ // code: plutusScript.to_hex(),
97
+ // version: Object.keys(LANGUAGE_VERSIONS).find(
98
+ // (key) =>
99
+ // LANGUAGE_VERSIONS[key as LanguageVersion].to_hex() ===
100
+ // plutusScript.language_version().to_hex(),
101
+ // ),
102
+ // };
103
+ // }
104
+ // const nativeScript = script.native_script()!;
105
+ // return fromNativeScript(nativeScript);
106
+ // };
107
+ export const toScriptRef = (script) => {
108
+ if ("code" in script) {
109
+ const plutusScript = deserializePlutusScript(script.code, script.version);
110
+ return csl.ScriptRef.new_plutus_script(plutusScript);
111
+ }
112
+ return csl.ScriptRef.new_native_script(toNativeScript(script));
113
+ };
114
+ /* -----------------[ PlutusData ]----------------- */
115
+ export const toPlutusData = (data) => {
116
+ const toPlutusList = (dataArray) => {
117
+ const plutusList = csl.PlutusList.new();
118
+ dataArray.forEach((element) => {
119
+ plutusList.add(toPlutusData(element));
120
+ });
121
+ return plutusList;
122
+ };
123
+ switch (typeof data) {
124
+ case "string":
125
+ return csl.PlutusData.new_bytes(toBytes(data));
126
+ case "number":
127
+ return csl.PlutusData.new_integer(csl.BigInt.from_str(data.toString()));
128
+ case "bigint":
129
+ return csl.PlutusData.new_integer(csl.BigInt.from_str(data.toString()));
130
+ case "object":
131
+ if (data instanceof Array) {
132
+ const plutusList = toPlutusList(data);
133
+ return csl.PlutusData.new_list(plutusList);
134
+ }
135
+ if (data instanceof Map) {
136
+ const plutusMap = csl.PlutusMap.new();
137
+ data.forEach((value, key) => {
138
+ const plutusMapValue = csl.PlutusMapValues.new();
139
+ plutusMapValue.add(toPlutusData(value));
140
+ plutusMap.insert(toPlutusData(key), plutusMapValue);
141
+ });
142
+ return csl.PlutusData.new_map(plutusMap);
143
+ }
144
+ return csl.PlutusData.new_constr_plutus_data(csl.ConstrPlutusData.new(csl.BigNum.from_str(data.alternative.toString()), toPlutusList(data.fields)));
145
+ }
146
+ };
147
+ export const castRawDataToJsonString = (rawData) => {
148
+ if (typeof rawData === "object") {
149
+ return JSONbig.stringify(rawData);
150
+ }
151
+ return rawData;
152
+ };
153
+ export const castDataToPlutusData = ({ type, content, }) => {
154
+ if (type === "Mesh") {
155
+ return toPlutusData(content);
156
+ }
157
+ if (type === "CBOR") {
158
+ return csl.PlutusData.from_hex(content);
159
+ }
160
+ return csl.PlutusData.from_json(content, csl.PlutusDatumSchema.DetailedSchema);
161
+ };
162
+ export const toNativeScript = (script) => {
163
+ const toNativeScripts = (scripts) => {
164
+ const nativeScripts = csl.NativeScripts.new();
165
+ scripts.forEach((script) => {
166
+ nativeScripts.add(toNativeScript(script));
167
+ });
168
+ return nativeScripts;
169
+ };
170
+ switch (script.type) {
171
+ case "all":
172
+ return csl.NativeScript.new_script_all(csl.ScriptAll.new(toNativeScripts(script.scripts)));
173
+ case "any":
174
+ return csl.NativeScript.new_script_any(csl.ScriptAny.new(toNativeScripts(script.scripts)));
175
+ case "atLeast":
176
+ return csl.NativeScript.new_script_n_of_k(csl.ScriptNOfK.new(script.required, toNativeScripts(script.scripts)));
177
+ case "after":
178
+ return csl.NativeScript.new_timelock_start(csl.TimelockStart.new_timelockstart(csl.BigNum.from_str(script.slot)));
179
+ case "before":
180
+ return csl.NativeScript.new_timelock_expiry(csl.TimelockExpiry.new_timelockexpiry(csl.BigNum.from_str(script.slot)));
181
+ case "sig":
182
+ return csl.NativeScript.new_script_pubkey(csl.ScriptPubkey.new(deserializeEd25519KeyHash(script.keyHash)));
183
+ }
184
+ };
@@ -0,0 +1 @@
1
+ export * as csl from "@sidan-lab/sidan-csl-rs-nodejs";
@@ -0,0 +1 @@
1
+ export * as csl from "@sidan-lab/sidan-csl-rs-nodejs";
@@ -0,0 +1,19 @@
1
+ import { LanguageVersion } from "@meshsdk/common";
2
+ import { csl } from "./csl";
3
+ export declare const deserializeAddress: (address: string) => csl.Address;
4
+ export declare const deserializeBip32PrivateKey: (bip32PrivateKey: string) => csl.Bip32PrivateKey;
5
+ export declare const deserializeDataHash: (dataHash: string) => csl.DataHash;
6
+ export declare const deserializeEd25519KeyHash: (ed25519KeyHash: string) => csl.Ed25519KeyHash;
7
+ export declare const deserializeEd25519Signature: (ed25519Signature: string) => csl.Ed25519Signature;
8
+ export declare const deserializeNativeScript: (nativeScript: string) => csl.NativeScript;
9
+ export declare const deserializePublicKey: (publicKey: string) => csl.PublicKey;
10
+ export declare const deserializePlutusData: (plutusData: string) => csl.PlutusData;
11
+ export declare const deserializePlutusScript: (plutusScript: string, version: LanguageVersion) => csl.PlutusScript;
12
+ export declare const deserializeScriptRef: (scriptRef: string) => csl.ScriptRef;
13
+ export declare const deserializeScriptHash: (scriptHash: string) => csl.ScriptHash;
14
+ export declare const deserializeTx: (tx: string) => csl.Transaction;
15
+ export declare const deserializeTxBody: (txBody: string) => csl.TransactionBody;
16
+ export declare const deserializeTxHash: (txHash: string) => csl.TransactionHash;
17
+ export declare const deserializeTxUnspentOutput: (txUnspentOutput: string) => csl.TransactionUnspentOutput;
18
+ export declare const deserializeTxWitnessSet: (txWitnessSet: string) => csl.TransactionWitnessSet;
19
+ export declare const deserializeValue: (value: string) => csl.Value;
@@ -0,0 +1,20 @@
1
+ import { LANGUAGE_VERSIONS } from "./constants";
2
+ import { toBytes } from "./converter";
3
+ import { csl } from "./csl";
4
+ export const deserializeAddress = (address) => csl.Address.from_bytes(toBytes(address));
5
+ export const deserializeBip32PrivateKey = (bip32PrivateKey) => csl.Bip32PrivateKey.from_bytes(toBytes(bip32PrivateKey));
6
+ export const deserializeDataHash = (dataHash) => csl.DataHash.from_bytes(toBytes(dataHash));
7
+ export const deserializeEd25519KeyHash = (ed25519KeyHash) => csl.Ed25519KeyHash.from_bytes(toBytes(ed25519KeyHash));
8
+ export const deserializeEd25519Signature = (ed25519Signature) => csl.Ed25519Signature.from_bytes(toBytes(ed25519Signature));
9
+ export const deserializeNativeScript = (nativeScript) => csl.NativeScript.from_bytes(toBytes(nativeScript));
10
+ export const deserializePublicKey = (publicKey) => csl.PublicKey.from_bytes(toBytes(publicKey));
11
+ export const deserializePlutusData = (plutusData) => csl.PlutusData.from_bytes(toBytes(plutusData));
12
+ export const deserializePlutusScript = (plutusScript, version) => csl.PlutusScript.from_bytes_with_version(toBytes(plutusScript), LANGUAGE_VERSIONS[version]);
13
+ export const deserializeScriptRef = (scriptRef) => csl.ScriptRef.from_bytes(toBytes(scriptRef));
14
+ export const deserializeScriptHash = (scriptHash) => csl.ScriptHash.from_bytes(toBytes(scriptHash));
15
+ export const deserializeTx = (tx) => csl.Transaction.from_bytes(toBytes(tx));
16
+ export const deserializeTxBody = (txBody) => csl.TransactionBody.from_bytes(toBytes(txBody));
17
+ export const deserializeTxHash = (txHash) => csl.TransactionHash.from_bytes(toBytes(txHash));
18
+ export const deserializeTxUnspentOutput = (txUnspentOutput) => csl.TransactionUnspentOutput.from_bytes(toBytes(txUnspentOutput));
19
+ export const deserializeTxWitnessSet = (txWitnessSet) => csl.TransactionWitnessSet.from_bytes(toBytes(txWitnessSet));
20
+ export const deserializeValue = (value) => csl.Value.from_bytes(toBytes(value));
@@ -0,0 +1,5 @@
1
+ export * from "./deserializer";
2
+ export * from "./converter";
3
+ export * from "./csl";
4
+ export * from "./constants";
5
+ export * from "./resolver";
@@ -0,0 +1,5 @@
1
+ export * from "./deserializer";
2
+ export * from "./converter";
3
+ export * from "./csl";
4
+ export * from "./constants";
5
+ export * from "./resolver";
@@ -0,0 +1,12 @@
1
+ import { Data, NativeScript, PlutusScript } from "@meshsdk/common";
2
+ export declare const resolveStakeKeyHash: (bech32: string) => string;
3
+ export declare const resolvePrivateKey: (words: string[]) => string;
4
+ export declare const resolveNativeScriptAddress: (script: NativeScript, networkId?: number) => string;
5
+ export declare const resolvePlutusScriptAddress: (script: PlutusScript, networkId?: number) => string;
6
+ export declare const resolveNativeScriptHash: (script: NativeScript) => string;
7
+ export declare const resolveRewardAddress: (bech32: string) => string;
8
+ export declare const resolveDataHash: (data: Data) => string;
9
+ export declare const resolveNativeScriptHex: (script: NativeScript) => string;
10
+ export declare const resolvePoolId: (hash: string) => string;
11
+ export declare const resolveScriptRef: (script: PlutusScript | NativeScript) => string;
12
+ export declare const resolveEd25519KeyHash: (bech32: string) => string;
@@ -0,0 +1,75 @@
1
+ import { mnemonicToEntropy, } from "@meshsdk/common";
2
+ import { deserializePlutusScript, fromUTF8, toAddress, toBaseAddress, toBytes, toNativeScript, toPlutusData, toRewardAddress, toScriptRef, } from ".";
3
+ import { csl } from "./csl";
4
+ export const resolveStakeKeyHash = (bech32) => {
5
+ try {
6
+ const stakeKeyHash = [
7
+ toBaseAddress(bech32)?.stake_cred().to_keyhash(),
8
+ toRewardAddress(bech32)?.payment_cred().to_keyhash(),
9
+ ].find((kh) => kh !== undefined);
10
+ if (stakeKeyHash !== undefined)
11
+ return stakeKeyHash.to_hex();
12
+ throw new Error(`Couldn't resolve stake key hash from address: ${bech32}`);
13
+ }
14
+ catch (error) {
15
+ throw new Error(`An error occurred during resolveStakeKeyHash: ${error}.`);
16
+ }
17
+ };
18
+ export const resolvePrivateKey = (words) => {
19
+ const buildBip32PrivateKey = (entropy, password = "") => {
20
+ return csl.Bip32PrivateKey.from_bip39_entropy(toBytes(entropy), toBytes(fromUTF8(password)));
21
+ };
22
+ const entropy = mnemonicToEntropy(words.join(" "));
23
+ const bip32PrivateKey = buildBip32PrivateKey(entropy);
24
+ const bech32PrivateKey = bip32PrivateKey.to_bech32();
25
+ bip32PrivateKey.free();
26
+ return bech32PrivateKey;
27
+ };
28
+ export const resolveNativeScriptAddress = (script, networkId = 0) => {
29
+ const nativeScript = toNativeScript(script);
30
+ const enterpriseAddress = csl.EnterpriseAddress.new(networkId, csl.Credential.from_scripthash(nativeScript.hash()));
31
+ return enterpriseAddress.to_address().to_bech32();
32
+ };
33
+ export const resolvePlutusScriptAddress = (script, networkId = 0) => {
34
+ const plutusScript = deserializePlutusScript(script.code, script.version);
35
+ const enterpriseAddress = csl.EnterpriseAddress.new(networkId, csl.Credential.from_scripthash(plutusScript.hash()));
36
+ return enterpriseAddress.to_address().to_bech32();
37
+ };
38
+ export const resolveNativeScriptHash = (script) => {
39
+ return toNativeScript(script).hash().to_hex();
40
+ };
41
+ export const resolveRewardAddress = (bech32) => {
42
+ const buildRewardAddress = (networkId, stakeKeyHash) => {
43
+ return csl.RewardAddress.new(networkId, csl.Credential.from_keyhash(stakeKeyHash));
44
+ };
45
+ try {
46
+ const address = toAddress(bech32);
47
+ const baseAddress = toBaseAddress(bech32);
48
+ const stakeKeyHash = baseAddress?.stake_cred().to_keyhash();
49
+ if (stakeKeyHash !== undefined)
50
+ return buildRewardAddress(address.network_id(), stakeKeyHash)
51
+ .to_address()
52
+ .to_bech32();
53
+ throw new Error(`Couldn't resolve reward address from address: ${bech32}`);
54
+ }
55
+ catch (error) {
56
+ throw new Error(`An error occurred during resolveRewardAddress: ${error}.`);
57
+ }
58
+ };
59
+ export const resolveDataHash = (data) => {
60
+ const plutusData = toPlutusData(data);
61
+ const dataHash = csl.hash_plutus_data(plutusData);
62
+ return dataHash.to_hex();
63
+ };
64
+ export const resolveNativeScriptHex = (script) => {
65
+ return toNativeScript(script).to_hex();
66
+ };
67
+ export const resolvePoolId = (hash) => {
68
+ return csl.Ed25519KeyHash.from_hex(hash).to_bech32("pool1");
69
+ };
70
+ export const resolveScriptRef = (script) => {
71
+ return toScriptRef(script).to_hex();
72
+ };
73
+ export const resolveEd25519KeyHash = (bech32) => {
74
+ return csl.Ed25519KeyHash.from_bech32(bech32).to_hex();
75
+ };
@@ -0,0 +1,3 @@
1
+ export * from "./utils";
2
+ export * from "./core";
3
+ export * from "./deser";
@@ -0,0 +1,3 @@
1
+ export * from "./utils";
2
+ export * from "./core";
3
+ export * from "./deser";
@@ -0,0 +1,9 @@
1
+ import { DeserializedAddress, PubKeyAddress, ScriptAddress } from "@meshsdk/common";
2
+ export declare const serialzeAddress: (deserializedAddress: Partial<DeserializedAddress>, networkId?: number) => string;
3
+ export declare const addrBech32ToHex: (bech32: string) => string;
4
+ export declare const addrBech32ToObj: <T>(bech32: string) => T;
5
+ export declare const serializeAddressObj: (plutusDataAddressObject: PubKeyAddress | ScriptAddress, networkId?: number) => string;
6
+ export declare const serializePlutusAddressToBech32: (plutusHex: string, networkId?: number) => string;
7
+ export declare const deserializeBech32Address: (bech32Addr: string) => DeserializedAddress;
8
+ export declare const scriptHashToBech32: (scriptHash: string, stakeCredentialHash?: string, networkId?: number, isScriptStakeCredentialHash?: boolean) => string;
9
+ export declare const v2ScriptToBech32: (scriptCbor: string, stakeCredential?: string, networkId?: number, isScriptStakeCredential?: boolean) => string;
@@ -0,0 +1,50 @@
1
+ import { pubKeyAddress, scriptAddress, } from "@meshsdk/common";
2
+ import { csl } from "../deser";
3
+ import { getV2ScriptHash } from "./scripts";
4
+ export const serialzeAddress = (deserializedAddress, networkId = 0) => {
5
+ const { pubKeyHash, scriptHash, stakeCredentialHash, stakeScriptCredentialHash, } = deserializedAddress;
6
+ const isPaymentScript = !pubKeyHash;
7
+ const isStakeScript = !stakeCredentialHash;
8
+ const paymentHash = isPaymentScript ? scriptHash : pubKeyHash;
9
+ const stakeHash = isStakeScript
10
+ ? stakeScriptCredentialHash
11
+ : stakeCredentialHash;
12
+ if (!paymentHash)
13
+ throw new Error("Error: serializeAddress: Address must contain a payment part");
14
+ const addressObj = isPaymentScript
15
+ ? pubKeyAddress(paymentHash, stakeHash, isStakeScript)
16
+ : scriptAddress(paymentHash, stakeHash, isStakeScript);
17
+ return serializeAddressObj(addressObj, networkId);
18
+ };
19
+ export const addrBech32ToHex = (bech32) => {
20
+ const hexAddress = csl.Address.from_bech32(bech32).to_hex();
21
+ const cslAddress = csl.Address.from_hex(hexAddress);
22
+ const hex = csl.PlutusData.from_address(cslAddress).to_hex();
23
+ return hex;
24
+ };
25
+ export const addrBech32ToObj = (bech32) => {
26
+ const hexAddress = csl.Address.from_bech32(bech32).to_hex();
27
+ const cslAddress = csl.Address.from_hex(hexAddress);
28
+ const json = JSON.parse(csl.PlutusData.from_address(cslAddress).to_json(1));
29
+ return json;
30
+ };
31
+ export const serializeAddressObj = (plutusDataAddressObject, networkId = 0) => {
32
+ const bech32Addr = csl.parse_plutus_address_obj_to_bech32(JSON.stringify(plutusDataAddressObject), networkId);
33
+ return bech32Addr;
34
+ };
35
+ export const serializePlutusAddressToBech32 = (plutusHex, networkId = 0) => {
36
+ const cslPlutusDataAddress = csl.PlutusData.from_hex(plutusHex);
37
+ const plutusDataAddressObject = JSON.parse(cslPlutusDataAddress.to_json(csl.PlutusDatumSchema.DetailedSchema));
38
+ return serializeAddressObj(plutusDataAddressObject, networkId);
39
+ };
40
+ export const deserializeBech32Address = (bech32Addr) => {
41
+ const serializedAddress = csl.serialize_bech32_address(bech32Addr);
42
+ return {
43
+ pubKeyHash: serializedAddress.get_pub_key_hash(),
44
+ scriptHash: serializedAddress.get_script_hash(),
45
+ stakeCredentialHash: serializedAddress.get_stake_key_hash(),
46
+ stakeScriptCredentialHash: serializedAddress.get_stake_key_script_hash(),
47
+ };
48
+ };
49
+ export const scriptHashToBech32 = (scriptHash, stakeCredentialHash, networkId = 0, isScriptStakeCredentialHash = false) => csl.wasm_script_to_address(networkId, scriptHash, stakeCredentialHash, isScriptStakeCredentialHash);
50
+ export const v2ScriptToBech32 = (scriptCbor, stakeCredential, networkId = 0, isScriptStakeCredential = false) => scriptHashToBech32(getV2ScriptHash(scriptCbor), stakeCredential, networkId, isScriptStakeCredential);
@@ -0,0 +1,17 @@
1
+ import { Data } from "@meshsdk/common";
2
+ /**
3
+ * Apply parameters to a given script blueprint.
4
+ *
5
+ * @param rawScript - The raw script CborHex from blueprint.
6
+ * @param params - The parameters to apply, in an array.
7
+ * @param type - The type of the parameters, default to be Mesh's Data type. It could also be in JSON and raw CBOR.
8
+ * @returns The double-cbor encoded script CborHex with the parameters applied.
9
+ */
10
+ export declare const applyParamsToScript: (rawScript: string, params: object[] | Data[], type?: "Mesh" | "JSON" | "CBOR") => string;
11
+ /**
12
+ * Apply Cbor encoding.
13
+ *
14
+ * @param rawScript - The raw script CborHex from blueprint.
15
+ * @returns The double-cbor encoded script CborHex.
16
+ */
17
+ export declare const applyCborEncoding: (rawScript: string) => string;
@@ -0,0 +1,47 @@
1
+ import { csl, toPlutusData } from "../deser";
2
+ /**
3
+ * Apply parameters to a given script blueprint.
4
+ *
5
+ * @param rawScript - The raw script CborHex from blueprint.
6
+ * @param params - The parameters to apply, in an array.
7
+ * @param type - The type of the parameters, default to be Mesh's Data type. It could also be in JSON and raw CBOR.
8
+ * @returns The double-cbor encoded script CborHex with the parameters applied.
9
+ */
10
+ export const applyParamsToScript = (rawScript, params, type = "Mesh") => {
11
+ const cslParams = csl.JsVecString.new();
12
+ let paramType = csl.BuilderDataType.CBOR;
13
+ switch (type) {
14
+ case "JSON":
15
+ paramType = csl.BuilderDataType.JSON;
16
+ params.forEach((param) => {
17
+ if (typeof param === "object") {
18
+ cslParams.add(JSON.stringify(param));
19
+ }
20
+ else if (typeof param === "string") {
21
+ cslParams.add(param);
22
+ }
23
+ });
24
+ break;
25
+ case "Mesh":
26
+ params.forEach((param) => {
27
+ const paramCbor = toPlutusData(param);
28
+ cslParams.add(paramCbor.to_hex());
29
+ });
30
+ break;
31
+ default:
32
+ params.forEach((param) => {
33
+ cslParams.add(param);
34
+ });
35
+ break;
36
+ }
37
+ return csl.apply_params_to_script(rawScript, cslParams, paramType);
38
+ };
39
+ /**
40
+ * Apply Cbor encoding.
41
+ *
42
+ * @param rawScript - The raw script CborHex from blueprint.
43
+ * @returns The double-cbor encoded script CborHex.
44
+ */
45
+ export const applyCborEncoding = (rawScript) => {
46
+ return csl.apply_params_to_script(rawScript, csl.JsVecString.new(), csl.BuilderDataType.CBOR);
47
+ };
@@ -0,0 +1 @@
1
+ export declare const skeyToPubKeyHash: (skeyHex: string) => string;
@@ -0,0 +1,6 @@
1
+ import { csl } from "../deser";
2
+ export const skeyToPubKeyHash = (skeyHex) => {
3
+ const isHexUnclean = skeyHex.slice(0, 4) === "5820" && skeyHex.length === 68;
4
+ const cleanHex = isHexUnclean ? skeyHex.slice(4) : skeyHex;
5
+ return csl.PrivateKey.from_hex(cleanHex).to_public().hash().to_hex();
6
+ };
@@ -0,0 +1,4 @@
1
+ export declare const parseInlineDatum: <T extends {
2
+ inline_datum?: string;
3
+ }, X>(utxo: T) => X;
4
+ export declare const parseDatumCbor: <T = any>(datumCbor: string) => T;
@@ -0,0 +1,13 @@
1
+ /* eslint-disable @typescript-eslint/no-explicit-any */
2
+ import { csl } from "../deser";
3
+ export const parseInlineDatum = (utxo) => {
4
+ const datumCbor = utxo.inline_datum || "";
5
+ const parsedDatum = csl.PlutusData.from_hex(datumCbor);
6
+ const datum = JSON.parse(parsedDatum.to_json(1));
7
+ return datum;
8
+ };
9
+ export const parseDatumCbor = (datumCbor) => {
10
+ const parsedDatum = csl.PlutusData.from_hex(datumCbor);
11
+ const datum = JSON.parse(parsedDatum.to_json(1));
12
+ return datum;
13
+ };
@@ -0,0 +1,7 @@
1
+ export * from "./address";
2
+ export * from "./datum";
3
+ export * from "./scripts";
4
+ export * from "./credentials";
5
+ export * from "./staking";
6
+ export * from "./transaction";
7
+ export * from "./aiken";
@@ -0,0 +1,7 @@
1
+ export * from "./address";
2
+ export * from "./datum";
3
+ export * from "./scripts";
4
+ export * from "./credentials";
5
+ export * from "./staking";
6
+ export * from "./transaction";
7
+ export * from "./aiken";
@@ -0,0 +1 @@
1
+ export declare const getV2ScriptHash: (script: string) => string;
@@ -0,0 +1,2 @@
1
+ import { csl } from "../deser";
2
+ export const getV2ScriptHash = (script) => csl.get_v2_script_hash(script);
@@ -0,0 +1,4 @@
1
+ export declare const poolIdHexToBech32: (poolIdHash: string) => string;
2
+ export declare const poolIdBech32ToHex: (poolIdBech32: string) => string;
3
+ export declare const baseAddressToStakeAddress: (baseAddressBech32: string, network?: number) => string;
4
+ export declare const rewardAddressToKeyHash: (rewardBech32: string) => string | undefined;
@@ -0,0 +1,25 @@
1
+ import { csl, toRewardAddress } from "../deser";
2
+ export const poolIdHexToBech32 = (poolIdHash) => {
3
+ const cslPoolIdHash = csl.Ed25519KeyHash.from_hex(poolIdHash);
4
+ return cslPoolIdHash.to_bech32("pool");
5
+ };
6
+ export const poolIdBech32ToHex = (poolIdBech32) => {
7
+ const cslPoolIdHash = csl.Ed25519KeyHash.from_bech32(poolIdBech32);
8
+ return Buffer.from(cslPoolIdHash.to_bytes()).toString("hex");
9
+ };
10
+ export const baseAddressToStakeAddress = (baseAddressBech32, network = 1) => {
11
+ const networkId = network === 1
12
+ ? csl.NetworkId.mainnet().kind()
13
+ : csl.NetworkId.testnet().kind();
14
+ const stakeCred = csl.BaseAddress.from_address(csl.Address.from_bech32(baseAddressBech32))?.stake_cred();
15
+ if (stakeCred) {
16
+ const stakeAddress = csl.RewardAddress.new(networkId, stakeCred)
17
+ .to_address()
18
+ .to_bech32();
19
+ return stakeAddress;
20
+ }
21
+ return "";
22
+ };
23
+ export const rewardAddressToKeyHash = (rewardBech32) => {
24
+ return toRewardAddress(rewardBech32)?.payment_cred().to_keyhash()?.to_hex();
25
+ };
@@ -0,0 +1,2 @@
1
+ export declare const calculateTxHash: (txHex: string) => string;
2
+ export declare const signTransaction: (txHex: string, signingKeys: string[]) => string;
@@ -0,0 +1,10 @@
1
+ import { csl } from "../deser";
2
+ export const calculateTxHash = (txHex) => csl.calculate_tx_hash(txHex);
3
+ export const signTransaction = (txHex, signingKeys) => {
4
+ const cslSigningKeys = csl.JsVecString.new();
5
+ signingKeys.forEach((key) => {
6
+ cslSigningKeys.add(key);
7
+ });
8
+ const signedTx = csl.sign_transaction(txHex, cslSigningKeys);
9
+ return signedTx;
10
+ };
package/package.json ADDED
@@ -0,0 +1,45 @@
1
+ {
2
+ "name": "@meshsdk/core-csl",
3
+ "version": "1.5.28",
4
+ "description": "",
5
+ "main": "./dist/index.js",
6
+ "module": "./dist/index.mjs",
7
+ "types": "./dist/index.d.ts",
8
+ "exports": {
9
+ ".": {
10
+ "import": "./dist/mjs/index.js",
11
+ "require": "./dist/cjs/index.js"
12
+ }
13
+ },
14
+ "files": [
15
+ "dist/**"
16
+ ],
17
+ "scripts": {
18
+ "build:mesh": "rm -fr dist/* && tsc -p ./tsconfig.json && tsc -p ./tsconfig-cjs.json",
19
+ "dev": "tsup src/index.ts --format esm,cjs --watch --dts",
20
+ "lint": "eslint",
21
+ "test": "jest --verbose",
22
+ "clean": "rm -rf .turbo && rm -rf dist && rm -rf node_modules",
23
+ "format": "prettier --check . --ignore-path ../../.gitignore",
24
+ "build:docs": "typedoc src/index.ts --json ../../apps/docs/src/data/mesh-core-csl.json"
25
+ },
26
+ "browser": {
27
+ "@sidan-lab/sidan-csl-rs-nodejs": "@sidan-lab/sidan-csl-rs-browser"
28
+ },
29
+ "devDependencies": {
30
+ "@meshsdk/eslint-config": "*",
31
+ "@meshsdk/typescript-config": "*",
32
+ "@types/json-bigint": "^1.0.4",
33
+ "eslint": "^8.57.0",
34
+ "ts-jest": "^29.1.4",
35
+ "tsup": "^8.0.2",
36
+ "typescript": "^5.3.3"
37
+ },
38
+ "dependencies": {
39
+ "@meshsdk/common": "*",
40
+ "@sidan-lab/sidan-csl-rs-browser": "^0.6.13",
41
+ "@sidan-lab/sidan-csl-rs-nodejs": "^0.6.13",
42
+ "json-bigint": "^1.0.0"
43
+ },
44
+ "prettier": "@meshsdk/prettier-config"
45
+ }