@meshsdk/core-cst 1.6.0-alpha.11
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.
- package/README.md +5 -0
- package/jest.config.js +5 -0
- package/package.json +41 -0
- package/src/cip8/index.ts +24 -0
- package/src/index.ts +16 -0
- package/src/resolvers/index.ts +141 -0
- package/src/serializer/index.ts +678 -0
- package/src/stricahq/index.ts +24 -0
- package/src/types/cardano-sdk.ts +243 -0
- package/src/types/index.ts +2 -0
- package/src/types/signer.ts +7 -0
- package/src/utils/builder.ts +130 -0
- package/src/utils/converter.ts +371 -0
- package/src/utils/deserializer.ts +76 -0
- package/src/utils/encoding.ts +12 -0
- package/src/utils/index.ts +4 -0
- package/src/utils/script-data-hash.ts +87 -0
- package/src/utils/value.ts +61 -0
- package/test/resolvers.test.ts +140 -0
- package/test/utils/converter.test.ts +18 -0
- package/tsconfig.json +5 -0
package/README.md
ADDED
package/jest.config.js
ADDED
package/package.json
ADDED
|
@@ -0,0 +1,41 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@meshsdk/core-cst",
|
|
3
|
+
"version": "1.6.0-alpha.11",
|
|
4
|
+
"description": "",
|
|
5
|
+
"main": "./dist/index.js",
|
|
6
|
+
"module": "./dist/index.mjs",
|
|
7
|
+
"types": "./dist/index.d.ts",
|
|
8
|
+
"exports": {
|
|
9
|
+
".": {
|
|
10
|
+
"import": "./dist/index.mjs",
|
|
11
|
+
"require": "./dist/index.js"
|
|
12
|
+
}
|
|
13
|
+
},
|
|
14
|
+
"scripts": {
|
|
15
|
+
"build:mesh": "tsup src/index.ts --format esm,cjs --dts",
|
|
16
|
+
"dev": "tsup src/index.ts --format esm,cjs --watch --dts",
|
|
17
|
+
"lint": "eslint",
|
|
18
|
+
"test": "jest --verbose",
|
|
19
|
+
"clean": "rm -rf .turbo && rm -rf dist && rm -rf node_modules",
|
|
20
|
+
"format": "prettier --check . --ignore-path ../../.gitignore"
|
|
21
|
+
},
|
|
22
|
+
"devDependencies": {
|
|
23
|
+
"@meshsdk/eslint-config": "*",
|
|
24
|
+
"@meshsdk/typescript-config": "*",
|
|
25
|
+
"@types/pbkdf2": "^3.1.2",
|
|
26
|
+
"eslint": "^8.57.0",
|
|
27
|
+
"ts-jest": "^29.1.4",
|
|
28
|
+
"tsup": "^8.0.2",
|
|
29
|
+
"typescript": "^5.3.3"
|
|
30
|
+
},
|
|
31
|
+
"dependencies": {
|
|
32
|
+
"@cardano-sdk/core": "^0.35.4",
|
|
33
|
+
"@cardano-sdk/crypto": "^0.1.28",
|
|
34
|
+
"@cardano-sdk/util": "^0.15.4",
|
|
35
|
+
"@meshsdk/common": "*",
|
|
36
|
+
"@stricahq/bip32ed25519": "^1.0.4",
|
|
37
|
+
"hash.js": "^1.1.7",
|
|
38
|
+
"pbkdf2": "^3.1.2"
|
|
39
|
+
},
|
|
40
|
+
"prettier": "@meshsdk/prettier-config"
|
|
41
|
+
}
|
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
import { DataSignature } from "@meshsdk/common";
|
|
2
|
+
|
|
3
|
+
import { PrivateKey, PublicKey } from "../stricahq";
|
|
4
|
+
|
|
5
|
+
export const signData = (
|
|
6
|
+
data: string,
|
|
7
|
+
privateKey: PrivateKey,
|
|
8
|
+
): DataSignature => {
|
|
9
|
+
const payload = Buffer.from(data);
|
|
10
|
+
const signature = privateKey.sign(payload);
|
|
11
|
+
|
|
12
|
+
return {
|
|
13
|
+
key: privateKey.toPublicKey().toBytes().toString("hex"),
|
|
14
|
+
signature: signature.toString("hex"),
|
|
15
|
+
};
|
|
16
|
+
};
|
|
17
|
+
|
|
18
|
+
export const checkSignature = (
|
|
19
|
+
data: string,
|
|
20
|
+
{ key, signature }: DataSignature,
|
|
21
|
+
) => {
|
|
22
|
+
const publicKey = new PublicKey(Buffer.from(key, "hex"));
|
|
23
|
+
return publicKey.verify(Buffer.from(signature, "hex"), Buffer.from(data));
|
|
24
|
+
};
|
package/src/index.ts
ADDED
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
import { Cardano, Serialization } from "@cardano-sdk/core";
|
|
2
|
+
|
|
3
|
+
import { PrivateKey, PublicKey } from "./stricahq";
|
|
4
|
+
|
|
5
|
+
export * from "./types";
|
|
6
|
+
export * from "./cip8";
|
|
7
|
+
export * from "./resolvers";
|
|
8
|
+
export * from "./serializer";
|
|
9
|
+
export * from "./utils";
|
|
10
|
+
|
|
11
|
+
export { Cardano, Serialization };
|
|
12
|
+
export { PrivateKey, PublicKey };
|
|
13
|
+
|
|
14
|
+
export * as CardanoSDKUtil from "@cardano-sdk/util";
|
|
15
|
+
export * as Crypto from "@cardano-sdk/crypto";
|
|
16
|
+
export * as CardanoSDK from "@cardano-sdk/core";
|
|
@@ -0,0 +1,141 @@
|
|
|
1
|
+
import { Cardano } from "@cardano-sdk/core";
|
|
2
|
+
import { blake2b } from "@cardano-sdk/crypto";
|
|
3
|
+
import { HexBlob } from "@cardano-sdk/util";
|
|
4
|
+
|
|
5
|
+
import { Data, NativeScript, PlutusScript } from "@meshsdk/common";
|
|
6
|
+
|
|
7
|
+
import { Ed25519KeyHashHex, EnterpriseAddress } from "../types";
|
|
8
|
+
import {
|
|
9
|
+
deserializePlutusScript,
|
|
10
|
+
deserializeTx,
|
|
11
|
+
toAddress,
|
|
12
|
+
toBaseAddress,
|
|
13
|
+
toEnterpriseAddress,
|
|
14
|
+
toNativeScript,
|
|
15
|
+
toPlutusData,
|
|
16
|
+
toRewardAddress,
|
|
17
|
+
toScriptRef,
|
|
18
|
+
} from "../utils";
|
|
19
|
+
import { buildRewardAddress } from "../utils/builder";
|
|
20
|
+
import { hexToBytes } from "../utils/encoding";
|
|
21
|
+
|
|
22
|
+
export const resolveDataHash = (data: Data) => {
|
|
23
|
+
const plutusData = toPlutusData(data);
|
|
24
|
+
return plutusData.hash().toString();
|
|
25
|
+
};
|
|
26
|
+
|
|
27
|
+
export const resolveNativeScriptAddress = (
|
|
28
|
+
script: NativeScript,
|
|
29
|
+
networkId = 0,
|
|
30
|
+
) => {
|
|
31
|
+
const nativeScript = toNativeScript(script);
|
|
32
|
+
|
|
33
|
+
const enterpriseAddress = EnterpriseAddress.fromCredentials(networkId, {
|
|
34
|
+
hash: nativeScript.hash(),
|
|
35
|
+
type: Cardano.CredentialType.ScriptHash,
|
|
36
|
+
});
|
|
37
|
+
|
|
38
|
+
return enterpriseAddress.toAddress().toBech32();
|
|
39
|
+
};
|
|
40
|
+
|
|
41
|
+
export const resolveNativeScriptHash = (script: NativeScript) => {
|
|
42
|
+
return toNativeScript(script).hash().toString();
|
|
43
|
+
};
|
|
44
|
+
|
|
45
|
+
export const resolvePaymentKeyHash = (bech32: string) => {
|
|
46
|
+
try {
|
|
47
|
+
const paymentKeyHash = [
|
|
48
|
+
toBaseAddress(bech32)?.getPaymentCredential().hash,
|
|
49
|
+
toEnterpriseAddress(bech32)?.getPaymentCredential().hash,
|
|
50
|
+
].find((kh) => kh !== undefined);
|
|
51
|
+
|
|
52
|
+
if (paymentKeyHash !== undefined) return paymentKeyHash.toString();
|
|
53
|
+
|
|
54
|
+
throw new Error(
|
|
55
|
+
`Couldn't resolve payment key hash from address: ${bech32}`,
|
|
56
|
+
);
|
|
57
|
+
} catch (error) {
|
|
58
|
+
throw new Error(
|
|
59
|
+
`An error occurred during resolvePaymentKeyHash: ${error}.`,
|
|
60
|
+
);
|
|
61
|
+
}
|
|
62
|
+
};
|
|
63
|
+
|
|
64
|
+
export const resolvePlutusScriptAddress = (
|
|
65
|
+
script: PlutusScript,
|
|
66
|
+
networkId = 0,
|
|
67
|
+
) => {
|
|
68
|
+
const plutusScript = deserializePlutusScript(script.code, script.version);
|
|
69
|
+
|
|
70
|
+
const enterpriseAddress = EnterpriseAddress.fromCredentials(networkId, {
|
|
71
|
+
hash: plutusScript.hash(),
|
|
72
|
+
type: Cardano.CredentialType.ScriptHash,
|
|
73
|
+
});
|
|
74
|
+
|
|
75
|
+
return enterpriseAddress.toAddress().toBech32();
|
|
76
|
+
};
|
|
77
|
+
|
|
78
|
+
export const resolvePlutusScriptHash = (bech32: string) => {
|
|
79
|
+
try {
|
|
80
|
+
const enterpriseAddress = toEnterpriseAddress(bech32);
|
|
81
|
+
const scriptHash = enterpriseAddress?.getPaymentCredential().hash;
|
|
82
|
+
|
|
83
|
+
if (scriptHash !== undefined) return scriptHash.toString();
|
|
84
|
+
|
|
85
|
+
throw new Error(`Couldn't resolve script hash from address: ${bech32}`);
|
|
86
|
+
} catch (error) {
|
|
87
|
+
throw new Error(`An error occurred during resolveScriptHash: ${error}.`);
|
|
88
|
+
}
|
|
89
|
+
};
|
|
90
|
+
|
|
91
|
+
export const resolvePoolId = (hash: string) => {
|
|
92
|
+
return Ed25519KeyHashHex(hash).toString();
|
|
93
|
+
};
|
|
94
|
+
|
|
95
|
+
export const resolvePrivateKey = (words: string[]) => {
|
|
96
|
+
return "not implemented";
|
|
97
|
+
};
|
|
98
|
+
|
|
99
|
+
export const resolveScriptRef = (script: PlutusScript | NativeScript) => {
|
|
100
|
+
return toScriptRef(script).toCbor().toString();
|
|
101
|
+
};
|
|
102
|
+
|
|
103
|
+
export const resolveRewardAddress = (bech32: string) => {
|
|
104
|
+
try {
|
|
105
|
+
const address = toAddress(bech32);
|
|
106
|
+
const baseAddress = toBaseAddress(bech32);
|
|
107
|
+
const stakeKeyHash = baseAddress?.getStakeCredential().hash;
|
|
108
|
+
|
|
109
|
+
if (stakeKeyHash !== undefined)
|
|
110
|
+
return buildRewardAddress(address.getNetworkId(), stakeKeyHash)
|
|
111
|
+
.toAddress()
|
|
112
|
+
.toBech32();
|
|
113
|
+
|
|
114
|
+
throw new Error(`Couldn't resolve reward address from address: ${bech32}`);
|
|
115
|
+
} catch (error) {
|
|
116
|
+
throw new Error(`An error occurred during resolveRewardAddress: ${error}.`);
|
|
117
|
+
}
|
|
118
|
+
};
|
|
119
|
+
|
|
120
|
+
export const resolveStakeKeyHash = (bech32: string) => {
|
|
121
|
+
try {
|
|
122
|
+
const stakeKeyHash = [
|
|
123
|
+
toBaseAddress(bech32)?.getStakeCredential().hash,
|
|
124
|
+
toRewardAddress(bech32)?.getPaymentCredential().hash,
|
|
125
|
+
].find((kh) => kh !== undefined);
|
|
126
|
+
|
|
127
|
+
if (stakeKeyHash !== undefined) return stakeKeyHash.toString();
|
|
128
|
+
|
|
129
|
+
throw new Error(`Couldn't resolve stake key hash from address: ${bech32}`);
|
|
130
|
+
} catch (error) {
|
|
131
|
+
throw new Error(`An error occurred during resolveStakeKeyHash: ${error}.`);
|
|
132
|
+
}
|
|
133
|
+
};
|
|
134
|
+
|
|
135
|
+
export const resolveTxHash = (txHex: string) => {
|
|
136
|
+
const txBody = deserializeTx(txHex).body();
|
|
137
|
+
const hash = blake2b(blake2b.BYTES)
|
|
138
|
+
.update(hexToBytes(txBody.toCbor()))
|
|
139
|
+
.digest();
|
|
140
|
+
return Cardano.TransactionId.fromHexBlob(HexBlob.fromBytes(hash));
|
|
141
|
+
};
|