@meshsdk/common 1.0.0-alpha.4 → 1.0.0-alpha.6

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.
@@ -0,0 +1,2 @@
1
+ export * from './mesh';
2
+ export * from './plutus';
@@ -0,0 +1,18 @@
1
+ "use strict";
2
+ var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
3
+ if (k2 === undefined) k2 = k;
4
+ var desc = Object.getOwnPropertyDescriptor(m, k);
5
+ if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
6
+ desc = { enumerable: true, get: function() { return m[k]; } };
7
+ }
8
+ Object.defineProperty(o, k2, desc);
9
+ }) : (function(o, m, k, k2) {
10
+ if (k2 === undefined) k2 = k;
11
+ o[k2] = m[k];
12
+ }));
13
+ var __exportStar = (this && this.__exportStar) || function(m, exports) {
14
+ for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
15
+ };
16
+ Object.defineProperty(exports, "__esModule", { value: true });
17
+ __exportStar(require("./mesh"), exports);
18
+ __exportStar(require("./plutus"), exports);
@@ -0,0 +1,8 @@
1
+ import { Data } from '../../types';
2
+ export declare const mConStr: <T extends Data[]>(alternative: number, fields: T) => Data;
3
+ export declare const mConStr0: <T extends Data[]>(fields: T) => Data;
4
+ export declare const mConStr1: <T extends Data[]>(fields: T) => Data;
5
+ export declare const mConStr2: <T extends Data[]>(fields: T) => Data;
6
+ export declare const mMaybeStakingHash: (stakeCredential: string) => Data;
7
+ export declare const mPubKeyAddress: (bytes: string, stakeCredential?: string) => Data;
8
+ export declare const mScriptAddress: (bytes: string, stakeCredential?: string) => Data;
@@ -0,0 +1,34 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.mScriptAddress = exports.mPubKeyAddress = exports.mMaybeStakingHash = exports.mConStr2 = exports.mConStr1 = exports.mConStr0 = exports.mConStr = void 0;
4
+ const mConStr = (alternative, fields) => ({
5
+ alternative,
6
+ fields,
7
+ });
8
+ exports.mConStr = mConStr;
9
+ const mConStr0 = (fields) => ({
10
+ alternative: 0,
11
+ fields,
12
+ });
13
+ exports.mConStr0 = mConStr0;
14
+ const mConStr1 = (fields) => ({
15
+ alternative: 1,
16
+ fields,
17
+ });
18
+ exports.mConStr1 = mConStr1;
19
+ const mConStr2 = (fields) => ({
20
+ alternative: 2,
21
+ fields,
22
+ });
23
+ exports.mConStr2 = mConStr2;
24
+ const mMaybeStakingHash = (stakeCredential) => {
25
+ if (stakeCredential === '') {
26
+ return (0, exports.mConStr1)([]);
27
+ }
28
+ return (0, exports.mConStr0)([(0, exports.mConStr0)([(0, exports.mConStr0)([stakeCredential])])]);
29
+ };
30
+ exports.mMaybeStakingHash = mMaybeStakingHash;
31
+ const mPubKeyAddress = (bytes, stakeCredential) => (0, exports.mConStr0)([{ alternative: 0, fields: [bytes] }, (0, exports.mMaybeStakingHash)(stakeCredential || '')]);
32
+ exports.mPubKeyAddress = mPubKeyAddress;
33
+ const mScriptAddress = (bytes, stakeCredential) => (0, exports.mConStr0)([{ alternative: 1, fields: [bytes] }, (0, exports.mMaybeStakingHash)(stakeCredential || '')]);
34
+ exports.mScriptAddress = mScriptAddress;
@@ -0,0 +1,69 @@
1
+ import { Asset } from '../../types';
2
+ export type ConStr<N = any, T = any> = {
3
+ constructor: N;
4
+ fields: T;
5
+ };
6
+ export type ConStr0<T = any> = ConStr<0, T>;
7
+ export type ConStr1<T = any> = ConStr<1, T>;
8
+ export type ConStr2<T = any> = ConStr<2, T>;
9
+ export type Bool = ConStr0<[]> | ConStr1<[]>;
10
+ export type BuiltinByteString = {
11
+ bytes: string;
12
+ };
13
+ export type Integer = {
14
+ int: number;
15
+ };
16
+ export type List = {
17
+ list: PlutusData[];
18
+ };
19
+ export type ValidatorHash = BuiltinByteString;
20
+ export type PaymentPubKeyHash = BuiltinByteString;
21
+ export type PubKeyHash = PaymentPubKeyHash;
22
+ export type POSIXTime = Integer;
23
+ export type CurrencySymbol = BuiltinByteString;
24
+ export type TokenName = BuiltinByteString;
25
+ export type MaybeStakingHash = ConStr1<[]> | ConStr0<[ConStr0<[ConStr0<[BuiltinByteString]>]>]>;
26
+ export type PubKeyAddress = ConStr0<[ConStr0<[PubKeyHash]>, MaybeStakingHash]>;
27
+ export type ScriptAddress = ConStr0<[ConStr1<[ValidatorHash]>, MaybeStakingHash]>;
28
+ export type AssetClass = ConStr0<[CurrencySymbol, TokenName]>;
29
+ export type TxOutRef = ConStr0<[ConStr0<[BuiltinByteString]>, Integer]>;
30
+ export type AssocMapItem<K, V> = {
31
+ k: K;
32
+ v: V;
33
+ };
34
+ export type DictItem<K, V> = {
35
+ k: K;
36
+ v: V;
37
+ };
38
+ export type AssocMap<K, V> = {
39
+ map: AssocMapItem<K, V>[];
40
+ };
41
+ export type Dict<K, V> = {
42
+ map: DictItem<K, V>[];
43
+ };
44
+ export type Tuple<K, V> = ConStr0<[K, V]>;
45
+ export type Value = AssocMap<CurrencySymbol, AssocMap<TokenName, Integer>>;
46
+ export type PlutusData = BuiltinByteString | Integer | MaybeStakingHash | PubKeyAddress | ScriptAddress | AssetClass | PaymentPubKeyHash | PubKeyHash | POSIXTime | TxOutRef;
47
+ export declare const conStr: <N, T>(constructor: N, fields: T) => ConStr<N, T>;
48
+ export declare const conStr0: <T>(fields: T) => ConStr0<T>;
49
+ export declare const conStr1: <T>(fields: T) => ConStr1<T>;
50
+ export declare const conStr2: <T>(fields: T) => ConStr2<T>;
51
+ export declare const bool: (b: boolean) => Bool;
52
+ export declare const builtinByteString: (bytes: string) => BuiltinByteString;
53
+ export declare const integer: (int: number) => Integer;
54
+ export declare const list: (pList: PlutusData[]) => List;
55
+ export declare const currencySymbol: (bytes: string) => CurrencySymbol;
56
+ export declare const tokenName: (bytes: string) => TokenName;
57
+ export declare const maybeStakingHash: (stakeCredential: string) => MaybeStakingHash;
58
+ export declare const pubKeyAddress: (bytes: string, stakeCredential?: string) => PubKeyAddress;
59
+ export declare const scriptAddress: (bytes: string, stakeCredential?: string) => ScriptAddress;
60
+ export declare const assetClass: (policyId: string, assetName: string) => AssetClass;
61
+ export declare const txOutRef: (txHash: string, index: number) => TxOutRef;
62
+ export declare const paymentPubKeyHash: (bytes: string) => PaymentPubKeyHash;
63
+ export declare const pubKeyHash: (bytes: string) => PubKeyHash;
64
+ export declare const posixTime: (int: number) => POSIXTime;
65
+ export declare const assocMap: <K, V>(itemsMap: [K, V][]) => AssocMap<K, V>;
66
+ export declare const dict: <K, V>(itemsMap: [K, V][]) => AssocMap<K, V>;
67
+ export declare const tuple: <K, V>(key: K, value: V) => Tuple<K, V>;
68
+ export declare const value: (assets: Asset[]) => Value;
69
+ export declare const parsePlutusValueToAssets: (plutusValue: Value) => Asset[];
@@ -0,0 +1,105 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.parsePlutusValueToAssets = exports.value = exports.tuple = exports.dict = exports.assocMap = exports.posixTime = exports.pubKeyHash = exports.paymentPubKeyHash = exports.txOutRef = exports.assetClass = exports.scriptAddress = exports.pubKeyAddress = exports.maybeStakingHash = exports.tokenName = exports.currencySymbol = exports.list = exports.integer = exports.builtinByteString = exports.bool = exports.conStr2 = exports.conStr1 = exports.conStr0 = exports.conStr = void 0;
4
+ const conStr = (constructor, fields) => ({
5
+ constructor,
6
+ fields,
7
+ });
8
+ exports.conStr = conStr;
9
+ const conStr0 = (fields) => (0, exports.conStr)(0, fields);
10
+ exports.conStr0 = conStr0;
11
+ const conStr1 = (fields) => (0, exports.conStr)(1, fields);
12
+ exports.conStr1 = conStr1;
13
+ const conStr2 = (fields) => (0, exports.conStr)(2, fields);
14
+ exports.conStr2 = conStr2;
15
+ const bool = (b) => (b ? (0, exports.conStr1)([]) : (0, exports.conStr0)([]));
16
+ exports.bool = bool;
17
+ const builtinByteString = (bytes) => ({
18
+ bytes,
19
+ });
20
+ exports.builtinByteString = builtinByteString;
21
+ const integer = (int) => ({ int });
22
+ exports.integer = integer;
23
+ const list = (pList) => ({ list: pList });
24
+ exports.list = list;
25
+ const currencySymbol = (bytes) => (0, exports.builtinByteString)(bytes);
26
+ exports.currencySymbol = currencySymbol;
27
+ const tokenName = (bytes) => (0, exports.builtinByteString)(bytes);
28
+ exports.tokenName = tokenName;
29
+ const maybeStakingHash = (stakeCredential) => {
30
+ if (stakeCredential === '') {
31
+ return (0, exports.conStr1)([]);
32
+ }
33
+ return (0, exports.conStr0)([
34
+ (0, exports.conStr0)([(0, exports.conStr0)([(0, exports.builtinByteString)(stakeCredential)])]),
35
+ ]);
36
+ };
37
+ exports.maybeStakingHash = maybeStakingHash;
38
+ const pubKeyAddress = (bytes, stakeCredential) => (0, exports.conStr0)([(0, exports.conStr0)([(0, exports.builtinByteString)(bytes)]), (0, exports.maybeStakingHash)(stakeCredential || '')]);
39
+ exports.pubKeyAddress = pubKeyAddress;
40
+ const scriptAddress = (bytes, stakeCredential) => (0, exports.conStr0)([(0, exports.conStr1)([(0, exports.builtinByteString)(bytes)]), (0, exports.maybeStakingHash)(stakeCredential || '')]);
41
+ exports.scriptAddress = scriptAddress;
42
+ const assetClass = (policyId, assetName) => (0, exports.conStr0)([(0, exports.currencySymbol)(policyId), (0, exports.tokenName)(assetName)]);
43
+ exports.assetClass = assetClass;
44
+ const txOutRef = (txHash, index) => (0, exports.conStr0)([(0, exports.conStr0)([(0, exports.builtinByteString)(txHash)]), (0, exports.integer)(index)]);
45
+ exports.txOutRef = txOutRef;
46
+ const paymentPubKeyHash = (bytes) => (0, exports.builtinByteString)(bytes);
47
+ exports.paymentPubKeyHash = paymentPubKeyHash;
48
+ const pubKeyHash = (bytes) => (0, exports.builtinByteString)(bytes);
49
+ exports.pubKeyHash = pubKeyHash;
50
+ const posixTime = (int) => ({ int });
51
+ exports.posixTime = posixTime;
52
+ const assocMap = (itemsMap) => ({
53
+ map: itemsMap.map(([k, v]) => ({ k, v })),
54
+ });
55
+ exports.assocMap = assocMap;
56
+ const dict = (itemsMap) => ({
57
+ map: itemsMap.map(([k, v]) => ({ k, v })),
58
+ });
59
+ exports.dict = dict;
60
+ const tuple = (key, value) => (0, exports.conStr0)([key, value]);
61
+ exports.tuple = tuple;
62
+ const value = (assets) => {
63
+ const valueMapToParse = [];
64
+ const valueMap = {};
65
+ assets.forEach((asset) => {
66
+ const sanitizedName = asset.unit.replace('lovelace', '');
67
+ const policy = sanitizedName.slice(0, 56) || '';
68
+ const token = sanitizedName.slice(56) || '';
69
+ if (!valueMap[policy]) {
70
+ valueMap[policy] = {};
71
+ }
72
+ if (!valueMap[policy][token]) {
73
+ valueMap[policy][token] = Number(asset.quantity);
74
+ }
75
+ else {
76
+ valueMap[policy][token] += Number(asset.quantity);
77
+ }
78
+ });
79
+ Object.keys(valueMap).forEach((policy) => {
80
+ const policyByte = (0, exports.currencySymbol)(policy);
81
+ const tokens = Object.keys(valueMap[policy]).map((name) => [
82
+ (0, exports.tokenName)(name),
83
+ (0, exports.integer)(valueMap[policy][name]),
84
+ ]);
85
+ const policyMap = (0, exports.assocMap)(tokens);
86
+ valueMapToParse.push([policyByte, policyMap]);
87
+ });
88
+ return (0, exports.assocMap)(valueMapToParse);
89
+ };
90
+ exports.value = value;
91
+ const parsePlutusValueToAssets = (plutusValue) => {
92
+ const assets = [];
93
+ plutusValue.map.forEach((policyMap) => {
94
+ const policy = policyMap.k.bytes;
95
+ policyMap.v.map.forEach((tokenMap) => {
96
+ const token = tokenMap.k.bytes;
97
+ const quantity = tokenMap.v.int.toString();
98
+ const unsanitizedUnit = policy + token;
99
+ const unit = unsanitizedUnit === '' ? 'lovelace' : unsanitizedUnit;
100
+ assets.push({ unit, quantity });
101
+ });
102
+ });
103
+ return assets;
104
+ };
105
+ exports.parsePlutusValueToAssets = parsePlutusValueToAssets;
@@ -0,0 +1 @@
1
+ export {};
@@ -0,0 +1,98 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ const _1 = require(".");
4
+ describe('Plutus data type', () => {
5
+ describe('Value', () => {
6
+ test('Simple ADA Value', () => {
7
+ const val = [{ unit: 'lovelace', quantity: '1000000' }];
8
+ const datum = (0, _1.value)(val);
9
+ const nameMap = (0, _1.dict)([
10
+ [(0, _1.builtinByteString)(''), (0, _1.integer)(1000000)],
11
+ ]);
12
+ const valMap = (0, _1.dict)([
13
+ [(0, _1.builtinByteString)(''), nameMap],
14
+ ]);
15
+ expect(JSON.stringify(datum)).toBe(JSON.stringify(valMap));
16
+ });
17
+ test('Simple token Value', () => {
18
+ const val = [
19
+ {
20
+ unit: 'baefdc6c5b191be372a794cd8d40d839ec0dbdd3c28957267dc8170074657374696e676e657777616c2e616461',
21
+ quantity: '345',
22
+ },
23
+ ];
24
+ const datum = (0, _1.value)(val);
25
+ const nameMap = (0, _1.dict)([
26
+ [(0, _1.builtinByteString)('74657374696e676e657777616c2e616461'), (0, _1.integer)(345)],
27
+ ]);
28
+ const valMap = (0, _1.dict)([
29
+ [
30
+ (0, _1.builtinByteString)('baefdc6c5b191be372a794cd8d40d839ec0dbdd3c28957267dc81700'),
31
+ nameMap,
32
+ ],
33
+ ]);
34
+ expect(JSON.stringify(datum)).toBe(JSON.stringify(valMap));
35
+ });
36
+ test('Complex Value', () => {
37
+ const val = [
38
+ { unit: 'lovelace', quantity: '1000000' },
39
+ {
40
+ unit: 'baefdc6c5b191be372a794cd8d40d839ec0dbdd3c28957267dc8170074657374696e676e657777616c2e616461',
41
+ quantity: '345',
42
+ },
43
+ {
44
+ unit: 'baefdc6c5b191be372a794cd8d40d839ec0dbdd3c28957267dc817001234',
45
+ quantity: '567',
46
+ },
47
+ ];
48
+ const datum = (0, _1.value)(val);
49
+ const nameMap = (0, _1.dict)([
50
+ [(0, _1.builtinByteString)('1234'), (0, _1.integer)(567)],
51
+ [(0, _1.builtinByteString)('74657374696e676e657777616c2e616461'), (0, _1.integer)(345)],
52
+ ]);
53
+ const valMap = (0, _1.dict)([
54
+ [(0, _1.builtinByteString)(''), (0, _1.dict)([[(0, _1.builtinByteString)(''), (0, _1.integer)(1000000)]])],
55
+ [
56
+ (0, _1.builtinByteString)('baefdc6c5b191be372a794cd8d40d839ec0dbdd3c28957267dc81700'),
57
+ nameMap,
58
+ ],
59
+ ]);
60
+ expect(JSON.stringify(datum)).toBe(JSON.stringify(valMap));
61
+ });
62
+ });
63
+ });
64
+ describe('Value', () => {
65
+ test('Simple ADA Value', () => {
66
+ const val = [{ unit: 'lovelace', quantity: '1000000' }];
67
+ const plutusValue = (0, _1.value)(val);
68
+ const assets = (0, _1.parsePlutusValueToAssets)(plutusValue);
69
+ expect(JSON.stringify(val)).toBe(JSON.stringify(assets));
70
+ });
71
+ test('Simple token Value', () => {
72
+ const val = [
73
+ {
74
+ unit: 'baefdc6c5b191be372a794cd8d40d839ec0dbdd3c28957267dc8170074657374696e676e657777616c2e616461',
75
+ quantity: '345',
76
+ },
77
+ ];
78
+ const plutusValue = (0, _1.value)(val);
79
+ const assets = (0, _1.parsePlutusValueToAssets)(plutusValue);
80
+ expect(JSON.stringify(val)).toBe(JSON.stringify(assets));
81
+ });
82
+ test('Complex Value', () => {
83
+ const val = [
84
+ { unit: 'lovelace', quantity: '1000000' },
85
+ {
86
+ unit: 'baefdc6c5b191be372a794cd8d40d839ec0dbdd3c28957267dc817001234',
87
+ quantity: '567',
88
+ },
89
+ {
90
+ unit: 'baefdc6c5b191be372a794cd8d40d839ec0dbdd3c28957267dc8170074657374696e676e657777616c2e616461',
91
+ quantity: '345',
92
+ },
93
+ ];
94
+ const plutusValue = (0, _1.value)(val);
95
+ const assets = (0, _1.parsePlutusValueToAssets)(plutusValue);
96
+ expect(JSON.stringify(val)).toBe(JSON.stringify(assets));
97
+ });
98
+ });
@@ -0,0 +1,3 @@
1
+ export * from './serializer';
2
+ export * from './types';
3
+ export * from './data';
@@ -0,0 +1,19 @@
1
+ "use strict";
2
+ var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
3
+ if (k2 === undefined) k2 = k;
4
+ var desc = Object.getOwnPropertyDescriptor(m, k);
5
+ if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
6
+ desc = { enumerable: true, get: function() { return m[k]; } };
7
+ }
8
+ Object.defineProperty(o, k2, desc);
9
+ }) : (function(o, m, k, k2) {
10
+ if (k2 === undefined) k2 = k;
11
+ o[k2] = m[k];
12
+ }));
13
+ var __exportStar = (this && this.__exportStar) || function(m, exports) {
14
+ for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
15
+ };
16
+ Object.defineProperty(exports, "__esModule", { value: true });
17
+ __exportStar(require("./serializer"), exports);
18
+ __exportStar(require("./types"), exports);
19
+ __exportStar(require("./data"), exports);
@@ -0,0 +1,7 @@
1
+ import { MeshTxBuilderBody } from './types';
2
+ import { Protocol } from '../types';
3
+ export declare const emptyTxBuilderBody: () => MeshTxBuilderBody;
4
+ export interface IMeshSerializer {
5
+ serializeTxBody(txBody: MeshTxBuilderBody, protocolParams: Protocol): string;
6
+ addSigningKeys(txHex: string, signingKeys: string[]): string;
7
+ }
@@ -0,0 +1,19 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.emptyTxBuilderBody = void 0;
4
+ const emptyTxBuilderBody = () => ({
5
+ inputs: [],
6
+ outputs: [],
7
+ extraInputs: [],
8
+ selectionThreshold: 0,
9
+ collaterals: [],
10
+ requiredSignatures: [],
11
+ referenceInputs: [],
12
+ mints: [],
13
+ changeAddress: '',
14
+ metadata: [],
15
+ validityRange: {},
16
+ certificates: [],
17
+ signingKey: [],
18
+ });
19
+ exports.emptyTxBuilderBody = emptyTxBuilderBody;
@@ -0,0 +1,128 @@
1
+ import { Asset, Budget, Data, LanguageVersion, PlutusScript, UTxO, PoolParams } from '../types';
2
+ export type MeshTxBuilderBody = {
3
+ inputs: TxIn[];
4
+ outputs: Output[];
5
+ extraInputs: UTxO[];
6
+ selectionThreshold: number;
7
+ collaterals: PubKeyTxIn[];
8
+ requiredSignatures: string[];
9
+ referenceInputs: RefTxIn[];
10
+ mints: MintItem[];
11
+ changeAddress: string;
12
+ metadata: Metadata[];
13
+ validityRange: ValidityRange;
14
+ certificates: Certificate[];
15
+ signingKey: string[];
16
+ };
17
+ export type TxIn = PubKeyTxIn | ScriptTxIn;
18
+ export type PubKeyTxIn = {
19
+ type: 'PubKey';
20
+ txIn: TxInParameter;
21
+ };
22
+ export type TxInParameter = {
23
+ txHash: string;
24
+ txIndex: number;
25
+ amount?: Asset[];
26
+ address?: string;
27
+ };
28
+ export type ScriptTxIn = {
29
+ type: 'Script';
30
+ txIn: TxInParameter;
31
+ scriptTxIn: ScriptTxInParameter;
32
+ };
33
+ export type ScriptTxInParameter = {
34
+ scriptSource?: {
35
+ type: 'Provided';
36
+ script: PlutusScript;
37
+ } | {
38
+ type: 'Inline';
39
+ txInInfo: ScriptSourceInfo;
40
+ };
41
+ datumSource?: {
42
+ type: 'Provided';
43
+ data: BuilderData;
44
+ } | {
45
+ type: 'Inline';
46
+ txHash: string;
47
+ txIndex: number;
48
+ };
49
+ redeemer?: Redeemer;
50
+ };
51
+ export type ScriptSourceInfo = {
52
+ txHash: string;
53
+ txIndex: number;
54
+ spendingScriptHash?: string;
55
+ version: LanguageVersion;
56
+ };
57
+ export type RefTxIn = {
58
+ txHash: string;
59
+ txIndex: number;
60
+ };
61
+ export type Output = {
62
+ address: string;
63
+ amount: Asset[];
64
+ datum?: {
65
+ type: 'Hash' | 'Inline';
66
+ data: BuilderData;
67
+ };
68
+ referenceScript?: PlutusScript;
69
+ };
70
+ export type MintItem = {
71
+ type: 'Plutus' | 'Native';
72
+ policyId: string;
73
+ assetName: string;
74
+ amount: string;
75
+ redeemer?: Redeemer;
76
+ scriptSource?: {
77
+ type: 'Provided';
78
+ script: PlutusScript;
79
+ } | {
80
+ type: 'Reference Script';
81
+ txHash: string;
82
+ txIndex: number;
83
+ version: LanguageVersion;
84
+ };
85
+ };
86
+ export type ValidityRange = {
87
+ invalidBefore?: number;
88
+ invalidHereafter?: number;
89
+ };
90
+ export type BuilderData = {
91
+ type: 'Mesh';
92
+ content: Data;
93
+ } | {
94
+ type: 'JSON';
95
+ content: string;
96
+ } | {
97
+ type: 'CBOR';
98
+ content: string;
99
+ };
100
+ export type Redeemer = {
101
+ data: BuilderData;
102
+ exUnits: Budget;
103
+ };
104
+ export type Metadata = {
105
+ tag: string;
106
+ metadata: object;
107
+ };
108
+ export type Certificate = {
109
+ type: 'RegisterPool';
110
+ poolParams: PoolParams;
111
+ } | {
112
+ type: 'RegisterStake';
113
+ stakeKeyHash: string;
114
+ } | {
115
+ type: 'DelegateStake';
116
+ stakeKeyHash: string;
117
+ poolId: string;
118
+ } | {
119
+ type: 'DeregisterStake';
120
+ stakeKeyHash: string;
121
+ } | {
122
+ type: 'RetirePool';
123
+ poolId: string;
124
+ epoch: number;
125
+ };
126
+ export type RequiredWith<T, K extends keyof T> = Required<T> & {
127
+ [P in K]: Required<T[P]>;
128
+ };
@@ -0,0 +1,2 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
@@ -1,3 +1,4 @@
1
1
  export * from './constants';
2
2
  export * from './cip';
3
3
  export * from './types';
4
+ export * from './builder';
package/dist/cjs/index.js CHANGED
@@ -17,3 +17,4 @@ Object.defineProperty(exports, "__esModule", { value: true });
17
17
  __exportStar(require("./constants"), exports);
18
18
  __exportStar(require("./cip"), exports);
19
19
  __exportStar(require("./types"), exports);
20
+ __exportStar(require("./builder"), exports);
@@ -0,0 +1,2 @@
1
+ export * from './mesh';
2
+ export * from './plutus';
@@ -0,0 +1,2 @@
1
+ export * from './mesh';
2
+ export * from './plutus';
@@ -0,0 +1,8 @@
1
+ import { Data } from '../../types';
2
+ export declare const mConStr: <T extends Data[]>(alternative: number, fields: T) => Data;
3
+ export declare const mConStr0: <T extends Data[]>(fields: T) => Data;
4
+ export declare const mConStr1: <T extends Data[]>(fields: T) => Data;
5
+ export declare const mConStr2: <T extends Data[]>(fields: T) => Data;
6
+ export declare const mMaybeStakingHash: (stakeCredential: string) => Data;
7
+ export declare const mPubKeyAddress: (bytes: string, stakeCredential?: string) => Data;
8
+ export declare const mScriptAddress: (bytes: string, stakeCredential?: string) => Data;
@@ -0,0 +1,24 @@
1
+ export const mConStr = (alternative, fields) => ({
2
+ alternative,
3
+ fields,
4
+ });
5
+ export const mConStr0 = (fields) => ({
6
+ alternative: 0,
7
+ fields,
8
+ });
9
+ export const mConStr1 = (fields) => ({
10
+ alternative: 1,
11
+ fields,
12
+ });
13
+ export const mConStr2 = (fields) => ({
14
+ alternative: 2,
15
+ fields,
16
+ });
17
+ export const mMaybeStakingHash = (stakeCredential) => {
18
+ if (stakeCredential === '') {
19
+ return mConStr1([]);
20
+ }
21
+ return mConStr0([mConStr0([mConStr0([stakeCredential])])]);
22
+ };
23
+ export const mPubKeyAddress = (bytes, stakeCredential) => mConStr0([{ alternative: 0, fields: [bytes] }, mMaybeStakingHash(stakeCredential || '')]);
24
+ export const mScriptAddress = (bytes, stakeCredential) => mConStr0([{ alternative: 1, fields: [bytes] }, mMaybeStakingHash(stakeCredential || '')]);
@@ -0,0 +1,69 @@
1
+ import { Asset } from '../../types';
2
+ export type ConStr<N = any, T = any> = {
3
+ constructor: N;
4
+ fields: T;
5
+ };
6
+ export type ConStr0<T = any> = ConStr<0, T>;
7
+ export type ConStr1<T = any> = ConStr<1, T>;
8
+ export type ConStr2<T = any> = ConStr<2, T>;
9
+ export type Bool = ConStr0<[]> | ConStr1<[]>;
10
+ export type BuiltinByteString = {
11
+ bytes: string;
12
+ };
13
+ export type Integer = {
14
+ int: number;
15
+ };
16
+ export type List = {
17
+ list: PlutusData[];
18
+ };
19
+ export type ValidatorHash = BuiltinByteString;
20
+ export type PaymentPubKeyHash = BuiltinByteString;
21
+ export type PubKeyHash = PaymentPubKeyHash;
22
+ export type POSIXTime = Integer;
23
+ export type CurrencySymbol = BuiltinByteString;
24
+ export type TokenName = BuiltinByteString;
25
+ export type MaybeStakingHash = ConStr1<[]> | ConStr0<[ConStr0<[ConStr0<[BuiltinByteString]>]>]>;
26
+ export type PubKeyAddress = ConStr0<[ConStr0<[PubKeyHash]>, MaybeStakingHash]>;
27
+ export type ScriptAddress = ConStr0<[ConStr1<[ValidatorHash]>, MaybeStakingHash]>;
28
+ export type AssetClass = ConStr0<[CurrencySymbol, TokenName]>;
29
+ export type TxOutRef = ConStr0<[ConStr0<[BuiltinByteString]>, Integer]>;
30
+ export type AssocMapItem<K, V> = {
31
+ k: K;
32
+ v: V;
33
+ };
34
+ export type DictItem<K, V> = {
35
+ k: K;
36
+ v: V;
37
+ };
38
+ export type AssocMap<K, V> = {
39
+ map: AssocMapItem<K, V>[];
40
+ };
41
+ export type Dict<K, V> = {
42
+ map: DictItem<K, V>[];
43
+ };
44
+ export type Tuple<K, V> = ConStr0<[K, V]>;
45
+ export type Value = AssocMap<CurrencySymbol, AssocMap<TokenName, Integer>>;
46
+ export type PlutusData = BuiltinByteString | Integer | MaybeStakingHash | PubKeyAddress | ScriptAddress | AssetClass | PaymentPubKeyHash | PubKeyHash | POSIXTime | TxOutRef;
47
+ export declare const conStr: <N, T>(constructor: N, fields: T) => ConStr<N, T>;
48
+ export declare const conStr0: <T>(fields: T) => ConStr0<T>;
49
+ export declare const conStr1: <T>(fields: T) => ConStr1<T>;
50
+ export declare const conStr2: <T>(fields: T) => ConStr2<T>;
51
+ export declare const bool: (b: boolean) => Bool;
52
+ export declare const builtinByteString: (bytes: string) => BuiltinByteString;
53
+ export declare const integer: (int: number) => Integer;
54
+ export declare const list: (pList: PlutusData[]) => List;
55
+ export declare const currencySymbol: (bytes: string) => CurrencySymbol;
56
+ export declare const tokenName: (bytes: string) => TokenName;
57
+ export declare const maybeStakingHash: (stakeCredential: string) => MaybeStakingHash;
58
+ export declare const pubKeyAddress: (bytes: string, stakeCredential?: string) => PubKeyAddress;
59
+ export declare const scriptAddress: (bytes: string, stakeCredential?: string) => ScriptAddress;
60
+ export declare const assetClass: (policyId: string, assetName: string) => AssetClass;
61
+ export declare const txOutRef: (txHash: string, index: number) => TxOutRef;
62
+ export declare const paymentPubKeyHash: (bytes: string) => PaymentPubKeyHash;
63
+ export declare const pubKeyHash: (bytes: string) => PubKeyHash;
64
+ export declare const posixTime: (int: number) => POSIXTime;
65
+ export declare const assocMap: <K, V>(itemsMap: [K, V][]) => AssocMap<K, V>;
66
+ export declare const dict: <K, V>(itemsMap: [K, V][]) => AssocMap<K, V>;
67
+ export declare const tuple: <K, V>(key: K, value: V) => Tuple<K, V>;
68
+ export declare const value: (assets: Asset[]) => Value;
69
+ export declare const parsePlutusValueToAssets: (plutusValue: Value) => Asset[];
@@ -0,0 +1,79 @@
1
+ export const conStr = (constructor, fields) => ({
2
+ constructor,
3
+ fields,
4
+ });
5
+ export const conStr0 = (fields) => conStr(0, fields);
6
+ export const conStr1 = (fields) => conStr(1, fields);
7
+ export const conStr2 = (fields) => conStr(2, fields);
8
+ export const bool = (b) => (b ? conStr1([]) : conStr0([]));
9
+ export const builtinByteString = (bytes) => ({
10
+ bytes,
11
+ });
12
+ export const integer = (int) => ({ int });
13
+ export const list = (pList) => ({ list: pList });
14
+ export const currencySymbol = (bytes) => builtinByteString(bytes);
15
+ export const tokenName = (bytes) => builtinByteString(bytes);
16
+ export const maybeStakingHash = (stakeCredential) => {
17
+ if (stakeCredential === '') {
18
+ return conStr1([]);
19
+ }
20
+ return conStr0([
21
+ conStr0([conStr0([builtinByteString(stakeCredential)])]),
22
+ ]);
23
+ };
24
+ export const pubKeyAddress = (bytes, stakeCredential) => conStr0([conStr0([builtinByteString(bytes)]), maybeStakingHash(stakeCredential || '')]);
25
+ export const scriptAddress = (bytes, stakeCredential) => conStr0([conStr1([builtinByteString(bytes)]), maybeStakingHash(stakeCredential || '')]);
26
+ export const assetClass = (policyId, assetName) => conStr0([currencySymbol(policyId), tokenName(assetName)]);
27
+ export const txOutRef = (txHash, index) => conStr0([conStr0([builtinByteString(txHash)]), integer(index)]);
28
+ export const paymentPubKeyHash = (bytes) => builtinByteString(bytes);
29
+ export const pubKeyHash = (bytes) => builtinByteString(bytes);
30
+ export const posixTime = (int) => ({ int });
31
+ export const assocMap = (itemsMap) => ({
32
+ map: itemsMap.map(([k, v]) => ({ k, v })),
33
+ });
34
+ export const dict = (itemsMap) => ({
35
+ map: itemsMap.map(([k, v]) => ({ k, v })),
36
+ });
37
+ export const tuple = (key, value) => conStr0([key, value]);
38
+ export const value = (assets) => {
39
+ const valueMapToParse = [];
40
+ const valueMap = {};
41
+ assets.forEach((asset) => {
42
+ const sanitizedName = asset.unit.replace('lovelace', '');
43
+ const policy = sanitizedName.slice(0, 56) || '';
44
+ const token = sanitizedName.slice(56) || '';
45
+ if (!valueMap[policy]) {
46
+ valueMap[policy] = {};
47
+ }
48
+ if (!valueMap[policy][token]) {
49
+ valueMap[policy][token] = Number(asset.quantity);
50
+ }
51
+ else {
52
+ valueMap[policy][token] += Number(asset.quantity);
53
+ }
54
+ });
55
+ Object.keys(valueMap).forEach((policy) => {
56
+ const policyByte = currencySymbol(policy);
57
+ const tokens = Object.keys(valueMap[policy]).map((name) => [
58
+ tokenName(name),
59
+ integer(valueMap[policy][name]),
60
+ ]);
61
+ const policyMap = assocMap(tokens);
62
+ valueMapToParse.push([policyByte, policyMap]);
63
+ });
64
+ return assocMap(valueMapToParse);
65
+ };
66
+ export const parsePlutusValueToAssets = (plutusValue) => {
67
+ const assets = [];
68
+ plutusValue.map.forEach((policyMap) => {
69
+ const policy = policyMap.k.bytes;
70
+ policyMap.v.map.forEach((tokenMap) => {
71
+ const token = tokenMap.k.bytes;
72
+ const quantity = tokenMap.v.int.toString();
73
+ const unsanitizedUnit = policy + token;
74
+ const unit = unsanitizedUnit === '' ? 'lovelace' : unsanitizedUnit;
75
+ assets.push({ unit, quantity });
76
+ });
77
+ });
78
+ return assets;
79
+ };
@@ -0,0 +1 @@
1
+ export {};
@@ -0,0 +1,96 @@
1
+ import { value, dict, builtinByteString, integer, parsePlutusValueToAssets, } from '.';
2
+ describe('Plutus data type', () => {
3
+ describe('Value', () => {
4
+ test('Simple ADA Value', () => {
5
+ const val = [{ unit: 'lovelace', quantity: '1000000' }];
6
+ const datum = value(val);
7
+ const nameMap = dict([
8
+ [builtinByteString(''), integer(1000000)],
9
+ ]);
10
+ const valMap = dict([
11
+ [builtinByteString(''), nameMap],
12
+ ]);
13
+ expect(JSON.stringify(datum)).toBe(JSON.stringify(valMap));
14
+ });
15
+ test('Simple token Value', () => {
16
+ const val = [
17
+ {
18
+ unit: 'baefdc6c5b191be372a794cd8d40d839ec0dbdd3c28957267dc8170074657374696e676e657777616c2e616461',
19
+ quantity: '345',
20
+ },
21
+ ];
22
+ const datum = value(val);
23
+ const nameMap = dict([
24
+ [builtinByteString('74657374696e676e657777616c2e616461'), integer(345)],
25
+ ]);
26
+ const valMap = dict([
27
+ [
28
+ builtinByteString('baefdc6c5b191be372a794cd8d40d839ec0dbdd3c28957267dc81700'),
29
+ nameMap,
30
+ ],
31
+ ]);
32
+ expect(JSON.stringify(datum)).toBe(JSON.stringify(valMap));
33
+ });
34
+ test('Complex Value', () => {
35
+ const val = [
36
+ { unit: 'lovelace', quantity: '1000000' },
37
+ {
38
+ unit: 'baefdc6c5b191be372a794cd8d40d839ec0dbdd3c28957267dc8170074657374696e676e657777616c2e616461',
39
+ quantity: '345',
40
+ },
41
+ {
42
+ unit: 'baefdc6c5b191be372a794cd8d40d839ec0dbdd3c28957267dc817001234',
43
+ quantity: '567',
44
+ },
45
+ ];
46
+ const datum = value(val);
47
+ const nameMap = dict([
48
+ [builtinByteString('1234'), integer(567)],
49
+ [builtinByteString('74657374696e676e657777616c2e616461'), integer(345)],
50
+ ]);
51
+ const valMap = dict([
52
+ [builtinByteString(''), dict([[builtinByteString(''), integer(1000000)]])],
53
+ [
54
+ builtinByteString('baefdc6c5b191be372a794cd8d40d839ec0dbdd3c28957267dc81700'),
55
+ nameMap,
56
+ ],
57
+ ]);
58
+ expect(JSON.stringify(datum)).toBe(JSON.stringify(valMap));
59
+ });
60
+ });
61
+ });
62
+ describe('Value', () => {
63
+ test('Simple ADA Value', () => {
64
+ const val = [{ unit: 'lovelace', quantity: '1000000' }];
65
+ const plutusValue = value(val);
66
+ const assets = parsePlutusValueToAssets(plutusValue);
67
+ expect(JSON.stringify(val)).toBe(JSON.stringify(assets));
68
+ });
69
+ test('Simple token Value', () => {
70
+ const val = [
71
+ {
72
+ unit: 'baefdc6c5b191be372a794cd8d40d839ec0dbdd3c28957267dc8170074657374696e676e657777616c2e616461',
73
+ quantity: '345',
74
+ },
75
+ ];
76
+ const plutusValue = value(val);
77
+ const assets = parsePlutusValueToAssets(plutusValue);
78
+ expect(JSON.stringify(val)).toBe(JSON.stringify(assets));
79
+ });
80
+ test('Complex Value', () => {
81
+ const val = [
82
+ { unit: 'lovelace', quantity: '1000000' },
83
+ {
84
+ unit: 'baefdc6c5b191be372a794cd8d40d839ec0dbdd3c28957267dc817001234',
85
+ quantity: '567',
86
+ },
87
+ {
88
+ unit: 'baefdc6c5b191be372a794cd8d40d839ec0dbdd3c28957267dc8170074657374696e676e657777616c2e616461',
89
+ quantity: '345',
90
+ },
91
+ ];
92
+ const plutusValue = value(val);
93
+ const assets = parsePlutusValueToAssets(plutusValue);
94
+ expect(JSON.stringify(val)).toBe(JSON.stringify(assets));
95
+ });
96
+ });
@@ -0,0 +1,3 @@
1
+ export * from './serializer';
2
+ export * from './types';
3
+ export * from './data';
@@ -0,0 +1,3 @@
1
+ export * from './serializer';
2
+ export * from './types';
3
+ export * from './data';
@@ -0,0 +1,7 @@
1
+ import { MeshTxBuilderBody } from './types';
2
+ import { Protocol } from '../types';
3
+ export declare const emptyTxBuilderBody: () => MeshTxBuilderBody;
4
+ export interface IMeshSerializer {
5
+ serializeTxBody(txBody: MeshTxBuilderBody, protocolParams: Protocol): string;
6
+ addSigningKeys(txHex: string, signingKeys: string[]): string;
7
+ }
@@ -0,0 +1,15 @@
1
+ export const emptyTxBuilderBody = () => ({
2
+ inputs: [],
3
+ outputs: [],
4
+ extraInputs: [],
5
+ selectionThreshold: 0,
6
+ collaterals: [],
7
+ requiredSignatures: [],
8
+ referenceInputs: [],
9
+ mints: [],
10
+ changeAddress: '',
11
+ metadata: [],
12
+ validityRange: {},
13
+ certificates: [],
14
+ signingKey: [],
15
+ });
@@ -0,0 +1,128 @@
1
+ import { Asset, Budget, Data, LanguageVersion, PlutusScript, UTxO, PoolParams } from '../types';
2
+ export type MeshTxBuilderBody = {
3
+ inputs: TxIn[];
4
+ outputs: Output[];
5
+ extraInputs: UTxO[];
6
+ selectionThreshold: number;
7
+ collaterals: PubKeyTxIn[];
8
+ requiredSignatures: string[];
9
+ referenceInputs: RefTxIn[];
10
+ mints: MintItem[];
11
+ changeAddress: string;
12
+ metadata: Metadata[];
13
+ validityRange: ValidityRange;
14
+ certificates: Certificate[];
15
+ signingKey: string[];
16
+ };
17
+ export type TxIn = PubKeyTxIn | ScriptTxIn;
18
+ export type PubKeyTxIn = {
19
+ type: 'PubKey';
20
+ txIn: TxInParameter;
21
+ };
22
+ export type TxInParameter = {
23
+ txHash: string;
24
+ txIndex: number;
25
+ amount?: Asset[];
26
+ address?: string;
27
+ };
28
+ export type ScriptTxIn = {
29
+ type: 'Script';
30
+ txIn: TxInParameter;
31
+ scriptTxIn: ScriptTxInParameter;
32
+ };
33
+ export type ScriptTxInParameter = {
34
+ scriptSource?: {
35
+ type: 'Provided';
36
+ script: PlutusScript;
37
+ } | {
38
+ type: 'Inline';
39
+ txInInfo: ScriptSourceInfo;
40
+ };
41
+ datumSource?: {
42
+ type: 'Provided';
43
+ data: BuilderData;
44
+ } | {
45
+ type: 'Inline';
46
+ txHash: string;
47
+ txIndex: number;
48
+ };
49
+ redeemer?: Redeemer;
50
+ };
51
+ export type ScriptSourceInfo = {
52
+ txHash: string;
53
+ txIndex: number;
54
+ spendingScriptHash?: string;
55
+ version: LanguageVersion;
56
+ };
57
+ export type RefTxIn = {
58
+ txHash: string;
59
+ txIndex: number;
60
+ };
61
+ export type Output = {
62
+ address: string;
63
+ amount: Asset[];
64
+ datum?: {
65
+ type: 'Hash' | 'Inline';
66
+ data: BuilderData;
67
+ };
68
+ referenceScript?: PlutusScript;
69
+ };
70
+ export type MintItem = {
71
+ type: 'Plutus' | 'Native';
72
+ policyId: string;
73
+ assetName: string;
74
+ amount: string;
75
+ redeemer?: Redeemer;
76
+ scriptSource?: {
77
+ type: 'Provided';
78
+ script: PlutusScript;
79
+ } | {
80
+ type: 'Reference Script';
81
+ txHash: string;
82
+ txIndex: number;
83
+ version: LanguageVersion;
84
+ };
85
+ };
86
+ export type ValidityRange = {
87
+ invalidBefore?: number;
88
+ invalidHereafter?: number;
89
+ };
90
+ export type BuilderData = {
91
+ type: 'Mesh';
92
+ content: Data;
93
+ } | {
94
+ type: 'JSON';
95
+ content: string;
96
+ } | {
97
+ type: 'CBOR';
98
+ content: string;
99
+ };
100
+ export type Redeemer = {
101
+ data: BuilderData;
102
+ exUnits: Budget;
103
+ };
104
+ export type Metadata = {
105
+ tag: string;
106
+ metadata: object;
107
+ };
108
+ export type Certificate = {
109
+ type: 'RegisterPool';
110
+ poolParams: PoolParams;
111
+ } | {
112
+ type: 'RegisterStake';
113
+ stakeKeyHash: string;
114
+ } | {
115
+ type: 'DelegateStake';
116
+ stakeKeyHash: string;
117
+ poolId: string;
118
+ } | {
119
+ type: 'DeregisterStake';
120
+ stakeKeyHash: string;
121
+ } | {
122
+ type: 'RetirePool';
123
+ poolId: string;
124
+ epoch: number;
125
+ };
126
+ export type RequiredWith<T, K extends keyof T> = Required<T> & {
127
+ [P in K]: Required<T[P]>;
128
+ };
@@ -0,0 +1 @@
1
+ export {};
@@ -1,3 +1,4 @@
1
1
  export * from './constants';
2
2
  export * from './cip';
3
3
  export * from './types';
4
+ export * from './builder';
package/dist/mjs/index.js CHANGED
@@ -1,3 +1,4 @@
1
1
  export * from './constants';
2
2
  export * from './cip';
3
3
  export * from './types';
4
+ export * from './builder';
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@meshsdk/common",
3
3
  "description": "Cardano common utils & constants for Mesh SDK",
4
- "version": "1.0.0-alpha.4",
4
+ "version": "1.0.0-alpha.6",
5
5
  "license": "Apache-2.0",
6
6
  "main": "dist/cjs/index.js",
7
7
  "module": "dist/mjs/index.js",