@meshsdk/core-csl 1.6.0-alpha.3 → 1.6.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.
- package/dist/cjs/core/builder.d.ts +1 -29
- package/dist/cjs/core/builder.js +1 -335
- package/dist/cjs/deser/converter.d.ts +3 -17
- package/dist/cjs/deser/converter.js +17 -289
- package/dist/cjs/deser/index.d.ts +0 -2
- package/dist/cjs/deser/index.js +0 -2
- package/dist/cjs/utils/address.js +1 -18
- package/dist/cjs/utils/aiken.d.ts +10 -2
- package/dist/cjs/utils/aiken.js +36 -14
- package/dist/mjs/core/builder.d.ts +1 -29
- package/dist/mjs/core/builder.js +3 -332
- package/dist/mjs/deser/converter.d.ts +3 -17
- package/dist/mjs/deser/converter.js +11 -269
- package/dist/mjs/deser/index.d.ts +0 -2
- package/dist/mjs/deser/index.js +0 -2
- package/dist/mjs/utils/address.js +1 -18
- package/dist/mjs/utils/aiken.d.ts +10 -2
- package/dist/mjs/utils/aiken.js +36 -13
- package/package.json +4 -4
- package/dist/cjs/deser/builder.d.ts +0 -18
- package/dist/cjs/deser/builder.js +0 -130
- package/dist/cjs/deser/types.d.ts +0 -146
- package/dist/cjs/deser/types.js +0 -2
- package/dist/mjs/deser/builder.d.ts +0 -18
- package/dist/mjs/deser/builder.js +0 -118
- package/dist/mjs/deser/types.d.ts +0 -146
- package/dist/mjs/deser/types.js +0 -1
|
@@ -1,38 +1,10 @@
|
|
|
1
|
-
import type {
|
|
2
|
-
import { TransactionBuilder, csl } from '../deser';
|
|
1
|
+
import type { MeshTxBuilderBody, Protocol, IMeshSerializer } from '@meshsdk/common';
|
|
3
2
|
export declare const emptyTxBuilderBody: () => MeshTxBuilderBody;
|
|
4
3
|
export declare class CSLSerializer implements IMeshSerializer {
|
|
5
|
-
txBuilder: TransactionBuilder;
|
|
6
4
|
protocolParams: Protocol;
|
|
7
5
|
meshTxBuilderBody: MeshTxBuilderBody;
|
|
8
6
|
constructor(protocolParams?: Protocol);
|
|
9
7
|
serializeTxBody(txBody: MeshTxBuilderBody, protocolParams?: Protocol): string;
|
|
10
8
|
addSigningKeys(txHex: string, signingKeys: string[]): string;
|
|
11
|
-
private addUtxosFrom;
|
|
12
|
-
private makePlutusScriptSource;
|
|
13
9
|
protected removeDuplicateInputs: () => void;
|
|
14
|
-
private addAllInputs;
|
|
15
|
-
private addTxIn;
|
|
16
|
-
private addScriptTxIn;
|
|
17
|
-
private addAllOutputs;
|
|
18
|
-
private addOutput;
|
|
19
|
-
private addAllCollaterals;
|
|
20
|
-
private addCollateral;
|
|
21
|
-
private addCollateralReturn;
|
|
22
|
-
private addAllReferenceInputs;
|
|
23
|
-
private addReferenceInput;
|
|
24
|
-
protected addAllMints: (mints: MintItem[]) => void;
|
|
25
|
-
private addPlutusMint;
|
|
26
|
-
private addNativeMint;
|
|
27
|
-
private decimalToFraction;
|
|
28
|
-
private toPoolParams;
|
|
29
|
-
private addCertificate;
|
|
30
|
-
protected addAllCertificates: (allCertificates: Certificate[]) => void;
|
|
31
|
-
protected addCostModels: () => void;
|
|
32
|
-
private addChange;
|
|
33
|
-
private addValidityRange;
|
|
34
|
-
private addAllRequiredSignatures;
|
|
35
|
-
private addAllMetadata;
|
|
36
|
-
protected castRawDataToJsonString: (rawData: object | string) => string;
|
|
37
|
-
protected castDataToPlutusData: ({ type, content }: BuilderData) => csl.PlutusData;
|
|
38
10
|
}
|
package/dist/cjs/core/builder.js
CHANGED
|
@@ -1,11 +1,7 @@
|
|
|
1
1
|
"use strict";
|
|
2
|
-
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
3
|
-
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
4
|
-
};
|
|
5
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
6
3
|
exports.CSLSerializer = exports.emptyTxBuilderBody = void 0;
|
|
7
4
|
const common_1 = require("@meshsdk/common");
|
|
8
|
-
const json_bigint_1 = __importDefault(require("json-bigint"));
|
|
9
5
|
const utils_1 = require("../utils");
|
|
10
6
|
const deser_1 = require("../deser");
|
|
11
7
|
const emptyTxBuilderBody = () => ({
|
|
@@ -27,55 +23,6 @@ exports.emptyTxBuilderBody = emptyTxBuilderBody;
|
|
|
27
23
|
class CSLSerializer {
|
|
28
24
|
constructor(protocolParams) {
|
|
29
25
|
this.meshTxBuilderBody = (0, exports.emptyTxBuilderBody)();
|
|
30
|
-
this.addUtxosFrom = (extraInputs, threshold) => {
|
|
31
|
-
const requiredAssets = this.meshTxBuilderBody.outputs.reduce((map, output) => {
|
|
32
|
-
const outputAmount = output.amount;
|
|
33
|
-
outputAmount.forEach((asset) => {
|
|
34
|
-
const { unit, quantity } = asset;
|
|
35
|
-
const existingQuantity = Number(map.get(unit)) || 0;
|
|
36
|
-
map.set(unit, String(existingQuantity + Number(quantity)));
|
|
37
|
-
});
|
|
38
|
-
return map;
|
|
39
|
-
}, new Map());
|
|
40
|
-
this.meshTxBuilderBody.inputs.reduce((map, input) => {
|
|
41
|
-
const inputAmount = input.txIn.amount;
|
|
42
|
-
inputAmount === null || inputAmount === void 0 ? void 0 : inputAmount.forEach((asset) => {
|
|
43
|
-
const { unit, quantity } = asset;
|
|
44
|
-
const existingQuantity = Number(map.get(unit)) || 0;
|
|
45
|
-
map.set(unit, String(existingQuantity - Number(quantity)));
|
|
46
|
-
});
|
|
47
|
-
return map;
|
|
48
|
-
}, requiredAssets);
|
|
49
|
-
this.meshTxBuilderBody.mints.reduce((map, mint) => {
|
|
50
|
-
const mintAmount = {
|
|
51
|
-
unit: mint.policyId + mint.assetName,
|
|
52
|
-
quantity: String(mint.amount),
|
|
53
|
-
};
|
|
54
|
-
const existingQuantity = Number(map.get(mintAmount.unit)) || 0;
|
|
55
|
-
map.set(mintAmount.unit, String(existingQuantity - Number(mintAmount.quantity)));
|
|
56
|
-
return map;
|
|
57
|
-
}, requiredAssets);
|
|
58
|
-
const selectedInputs = (0, common_1.selectUtxos)(extraInputs, requiredAssets, threshold);
|
|
59
|
-
selectedInputs.forEach((input) => {
|
|
60
|
-
const pubKeyTxIn = {
|
|
61
|
-
type: 'PubKey',
|
|
62
|
-
txIn: {
|
|
63
|
-
txHash: input.input.txHash,
|
|
64
|
-
txIndex: input.input.outputIndex,
|
|
65
|
-
amount: input.output.amount,
|
|
66
|
-
address: input.output.address,
|
|
67
|
-
},
|
|
68
|
-
};
|
|
69
|
-
this.meshTxBuilderBody.inputs.push(pubKeyTxIn);
|
|
70
|
-
this.addTxIn(pubKeyTxIn);
|
|
71
|
-
});
|
|
72
|
-
};
|
|
73
|
-
this.makePlutusScriptSource = (scriptSourceInfo) => {
|
|
74
|
-
const scriptHash = deser_1.csl.ScriptHash.from_hex(scriptSourceInfo.spendingScriptHash);
|
|
75
|
-
const scriptRefInput = deser_1.csl.TransactionInput.new(deser_1.csl.TransactionHash.from_hex(scriptSourceInfo.txHash), scriptSourceInfo.txIndex);
|
|
76
|
-
const scriptSource = deser_1.csl.PlutusScriptSource.new_ref_input_with_lang_ver(scriptHash, scriptRefInput, deser_1.LANGUAGE_VERSIONS[scriptSourceInfo.version]);
|
|
77
|
-
return scriptSource;
|
|
78
|
-
};
|
|
79
26
|
// Below protected functions for completing tx building
|
|
80
27
|
this.removeDuplicateInputs = () => {
|
|
81
28
|
const { inputs } = this.meshTxBuilderBody;
|
|
@@ -92,285 +39,10 @@ class CSLSerializer {
|
|
|
92
39
|
}
|
|
93
40
|
}
|
|
94
41
|
};
|
|
95
|
-
this.addAllInputs = (inputs) => {
|
|
96
|
-
for (let i = 0; i < inputs.length; i += 1) {
|
|
97
|
-
const currentTxIn = inputs[i]; // TODO: add type
|
|
98
|
-
switch (currentTxIn.type) {
|
|
99
|
-
case 'PubKey':
|
|
100
|
-
this.addTxIn(currentTxIn);
|
|
101
|
-
break;
|
|
102
|
-
case 'Script':
|
|
103
|
-
this.addScriptTxIn(currentTxIn);
|
|
104
|
-
break;
|
|
105
|
-
}
|
|
106
|
-
}
|
|
107
|
-
};
|
|
108
|
-
this.addTxIn = (currentTxIn) => {
|
|
109
|
-
this.txBuilder.add_input(deser_1.csl.Address.from_bech32(currentTxIn.txIn.address), deser_1.csl.TransactionInput.new(deser_1.csl.TransactionHash.from_hex(currentTxIn.txIn.txHash), currentTxIn.txIn.txIndex), (0, deser_1.toValue)(currentTxIn.txIn.amount));
|
|
110
|
-
};
|
|
111
|
-
this.addScriptTxIn = ({ scriptTxIn, txIn, }) => {
|
|
112
|
-
let cslDatum;
|
|
113
|
-
const { datumSource, scriptSource, redeemer } = scriptTxIn;
|
|
114
|
-
if (datumSource.type === 'Provided') {
|
|
115
|
-
cslDatum = deser_1.csl.DatumSource.new(this.castDataToPlutusData(datumSource.data));
|
|
116
|
-
}
|
|
117
|
-
else {
|
|
118
|
-
const refTxIn = deser_1.csl.TransactionInput.new(deser_1.csl.TransactionHash.from_hex(datumSource.txHash), datumSource.txIndex);
|
|
119
|
-
cslDatum = deser_1.csl.DatumSource.new_ref_input(refTxIn);
|
|
120
|
-
}
|
|
121
|
-
let cslScript;
|
|
122
|
-
if (scriptSource.type === 'Inline') {
|
|
123
|
-
cslScript = this.makePlutusScriptSource(scriptSource.txInInfo);
|
|
124
|
-
}
|
|
125
|
-
else {
|
|
126
|
-
cslScript = deser_1.csl.PlutusScriptSource.new(deser_1.csl.PlutusScript.from_hex_with_version(scriptSource.script.code, deser_1.LANGUAGE_VERSIONS[scriptSource.script.version]));
|
|
127
|
-
}
|
|
128
|
-
const cslRedeemer = deser_1.csl.Redeemer.new(deser_1.csl.RedeemerTag.new_spend(), deser_1.csl.BigNum.from_str('0'), this.castDataToPlutusData(redeemer.data), deser_1.csl.ExUnits.new(deser_1.csl.BigNum.from_str(String(redeemer.exUnits.mem)), deser_1.csl.BigNum.from_str(String(redeemer.exUnits.steps))));
|
|
129
|
-
this.txBuilder.add_plutus_script_input(deser_1.csl.PlutusWitness.new_with_ref(cslScript, cslDatum, cslRedeemer), deser_1.csl.TransactionInput.new(deser_1.csl.TransactionHash.from_hex(txIn.txHash), txIn.txIndex), (0, deser_1.toValue)(txIn.amount));
|
|
130
|
-
};
|
|
131
|
-
this.addAllOutputs = (outputs) => {
|
|
132
|
-
for (let i = 0; i < outputs.length; i += 1) {
|
|
133
|
-
const currentOutput = outputs[i];
|
|
134
|
-
this.addOutput(currentOutput);
|
|
135
|
-
}
|
|
136
|
-
};
|
|
137
|
-
this.addOutput = ({ amount, address, datum, referenceScript }) => {
|
|
138
|
-
const txValue = (0, deser_1.toValue)(amount);
|
|
139
|
-
const multiAsset = txValue.multiasset();
|
|
140
|
-
if (txValue.is_zero() && multiAsset === undefined)
|
|
141
|
-
throw Error('Invalid output amount');
|
|
142
|
-
let outputBuilder = deser_1.csl.TransactionOutputBuilder.new().with_address((0, deser_1.toAddress)(address));
|
|
143
|
-
if (datum && datum.type === 'Hash') {
|
|
144
|
-
outputBuilder = outputBuilder.with_data_hash(deser_1.csl.hash_plutus_data(this.castDataToPlutusData(datum.data)));
|
|
145
|
-
}
|
|
146
|
-
if (datum && datum.type === 'Inline') {
|
|
147
|
-
outputBuilder = outputBuilder.with_plutus_data(this.castDataToPlutusData(datum.data));
|
|
148
|
-
}
|
|
149
|
-
if (referenceScript) {
|
|
150
|
-
outputBuilder = outputBuilder.with_script_ref(deser_1.csl.ScriptRef.new_plutus_script(deser_1.csl.PlutusScript.from_hex_with_version(referenceScript.code, deser_1.LANGUAGE_VERSIONS[referenceScript.version])));
|
|
151
|
-
}
|
|
152
|
-
const amountBuilder = outputBuilder.next();
|
|
153
|
-
if (multiAsset) {
|
|
154
|
-
const output = txValue.coin().is_zero()
|
|
155
|
-
? amountBuilder
|
|
156
|
-
.with_asset_and_min_required_coin_by_utxo_cost(multiAsset, (0, deser_1.buildDataCost)(this.protocolParams.coinsPerUTxOSize))
|
|
157
|
-
.build()
|
|
158
|
-
: amountBuilder.with_coin_and_asset(txValue.coin(), multiAsset).build();
|
|
159
|
-
this.txBuilder.add_output(output);
|
|
160
|
-
}
|
|
161
|
-
else {
|
|
162
|
-
const output = amountBuilder.with_coin(txValue.coin()).build();
|
|
163
|
-
this.txBuilder.add_output(output);
|
|
164
|
-
}
|
|
165
|
-
};
|
|
166
|
-
this.addAllCollaterals = (collaterals) => {
|
|
167
|
-
const collateralBuilder = deser_1.csl.TxInputsBuilder.new();
|
|
168
|
-
for (let i = 0; i < collaterals.length; i += 1) {
|
|
169
|
-
const currentCollateral = collaterals[i];
|
|
170
|
-
this.addCollateral(collateralBuilder, currentCollateral);
|
|
171
|
-
}
|
|
172
|
-
this.txBuilder.set_collateral(collateralBuilder);
|
|
173
|
-
};
|
|
174
|
-
this.addCollateral = (collateralBuilder, currentCollateral) => {
|
|
175
|
-
collateralBuilder.add_input(deser_1.csl.Address.from_bech32(currentCollateral.txIn.address), deser_1.csl.TransactionInput.new(deser_1.csl.TransactionHash.from_hex(currentCollateral.txIn.txHash), currentCollateral.txIn.txIndex), (0, deser_1.toValue)(currentCollateral.txIn.amount));
|
|
176
|
-
};
|
|
177
|
-
this.addCollateralReturn = (returnAddress) => {
|
|
178
|
-
var _a;
|
|
179
|
-
const currentFee = (_a = this.txBuilder.get_fee_if_set()) === null || _a === void 0 ? void 0 : _a.to_js_value();
|
|
180
|
-
if (currentFee) {
|
|
181
|
-
const collateralAmount = Math.ceil((this.protocolParams.collateralPercent * Number(currentFee)) / 100);
|
|
182
|
-
this.txBuilder.set_total_collateral_and_return(deser_1.csl.BigNum.from_str(String(collateralAmount)), deser_1.csl.Address.from_bech32(returnAddress));
|
|
183
|
-
}
|
|
184
|
-
};
|
|
185
|
-
this.addAllReferenceInputs = (refInputs) => {
|
|
186
|
-
refInputs.forEach((refInput) => {
|
|
187
|
-
this.addReferenceInput(refInput);
|
|
188
|
-
});
|
|
189
|
-
};
|
|
190
|
-
this.addReferenceInput = ({ txHash, txIndex }) => {
|
|
191
|
-
const refInput = deser_1.csl.TransactionInput.new(deser_1.csl.TransactionHash.from_hex(txHash), txIndex);
|
|
192
|
-
this.txBuilder.add_reference_input(refInput);
|
|
193
|
-
};
|
|
194
|
-
this.addAllMints = (mints) => {
|
|
195
|
-
const mintBuilder = deser_1.csl.MintBuilder.new();
|
|
196
|
-
let plutusMintCount = 0;
|
|
197
|
-
for (let i = 0; i < mints.length; i += 1) {
|
|
198
|
-
const mintItem = mints[i];
|
|
199
|
-
if (!mintItem.scriptSource)
|
|
200
|
-
throw Error('Mint script is expected to be provided');
|
|
201
|
-
if (mintItem.type === 'Plutus') {
|
|
202
|
-
if (!mintItem.redeemer)
|
|
203
|
-
throw Error('Missing mint redeemer information');
|
|
204
|
-
this.addPlutusMint(mintBuilder, mintItem, plutusMintCount); // TODO: Update after csl update
|
|
205
|
-
plutusMintCount += 1; // TODO: Remove after csl update
|
|
206
|
-
}
|
|
207
|
-
else if (mintItem.type === 'Native') {
|
|
208
|
-
this.addNativeMint(mintBuilder, mintItem);
|
|
209
|
-
}
|
|
210
|
-
}
|
|
211
|
-
this.txBuilder.set_mint_builder(mintBuilder);
|
|
212
|
-
};
|
|
213
|
-
this.addPlutusMint = (mintBuilder, { redeemer, policyId, scriptSource, assetName, amount }, redeemerIndex) => {
|
|
214
|
-
const newRedeemer = deser_1.csl.Redeemer.new(deser_1.csl.RedeemerTag.new_mint(), deser_1.csl.BigNum.from_str(String(redeemerIndex)), this.castDataToPlutusData(redeemer.data), deser_1.csl.ExUnits.new(deser_1.csl.BigNum.from_str(String(redeemer.exUnits.mem)), deser_1.csl.BigNum.from_str(String(redeemer.exUnits.steps))));
|
|
215
|
-
const script = scriptSource.type === 'Reference Script'
|
|
216
|
-
? deser_1.csl.PlutusScriptSource.new_ref_input_with_lang_ver(deser_1.csl.ScriptHash.from_hex(policyId), deser_1.csl.TransactionInput.new(deser_1.csl.TransactionHash.from_hex(scriptSource.txHash), scriptSource.txIndex), deser_1.LANGUAGE_VERSIONS[scriptSource.version])
|
|
217
|
-
: deser_1.csl.PlutusScriptSource.new(deser_1.csl.PlutusScript.from_hex_with_version(scriptSource.script.code, deser_1.LANGUAGE_VERSIONS[scriptSource.script.version]));
|
|
218
|
-
mintBuilder.add_asset(deser_1.csl.MintWitness.new_plutus_script(script, newRedeemer), deser_1.csl.AssetName.new(Buffer.from(assetName, 'hex')), deser_1.csl.Int.from_str(amount));
|
|
219
|
-
};
|
|
220
|
-
this.addNativeMint = (mintBuilder, { scriptSource, assetName, amount }) => {
|
|
221
|
-
if (scriptSource.type === 'Reference Script')
|
|
222
|
-
throw Error('Native mint cannot have reference script');
|
|
223
|
-
mintBuilder.add_asset(deser_1.csl.MintWitness.new_native_script(deser_1.csl.NativeScript.from_hex(scriptSource.script.code)), deser_1.csl.AssetName.new(Buffer.from(assetName, 'hex')), deser_1.csl.Int.from_str(amount));
|
|
224
|
-
};
|
|
225
|
-
this.toPoolParams = (poolParams) => {
|
|
226
|
-
const marginFraction = this.decimalToFraction(poolParams.margin);
|
|
227
|
-
const relays = deser_1.csl.Relays.new();
|
|
228
|
-
poolParams.relays.forEach((relay) => {
|
|
229
|
-
relays.add((0, deser_1.toRelay)(relay));
|
|
230
|
-
});
|
|
231
|
-
const rewardAddress = deser_1.csl.RewardAddress.from_address(deser_1.csl.Address.from_bech32(poolParams.rewardAddress));
|
|
232
|
-
if (rewardAddress === undefined) {
|
|
233
|
-
throw new Error('Reward address is invalid');
|
|
234
|
-
}
|
|
235
|
-
const poolOwners = deser_1.csl.Ed25519KeyHashes.new();
|
|
236
|
-
poolParams.owners.forEach((owner) => {
|
|
237
|
-
poolOwners.add(deser_1.csl.Ed25519KeyHash.from_hex(owner));
|
|
238
|
-
});
|
|
239
|
-
return deser_1.csl.PoolParams.new(deser_1.csl.Ed25519KeyHash.from_hex(poolParams.operator), deser_1.csl.VRFKeyHash.from_hex(poolParams.VRFKeyHash), deser_1.csl.BigNum.from_str(poolParams.pledge), deser_1.csl.BigNum.from_str(poolParams.cost), deser_1.csl.UnitInterval.new(deser_1.csl.BigNum.from_str(marginFraction[0].toString()), deser_1.csl.BigNum.from_str(marginFraction[1].toString())), rewardAddress, poolOwners, relays, poolParams.metadata
|
|
240
|
-
? deser_1.csl.PoolMetadata.from_json(json_bigint_1.default.stringify(poolParams.metadata))
|
|
241
|
-
: undefined);
|
|
242
|
-
};
|
|
243
|
-
this.addCertificate = (certificates, cert) => {
|
|
244
|
-
switch (cert.type) {
|
|
245
|
-
case 'RegisterPool':
|
|
246
|
-
certificates.add(deser_1.csl.Certificate.new_pool_registration(deser_1.csl.PoolRegistration.new(this.toPoolParams(cert.poolParams))));
|
|
247
|
-
break;
|
|
248
|
-
case 'RegisterStake':
|
|
249
|
-
certificates.add(deser_1.csl.Certificate.new_stake_registration(deser_1.csl.StakeRegistration.new(deser_1.csl.StakeCredential.from_keyhash(deser_1.csl.Ed25519KeyHash.from_hex(cert.stakeKeyHash)))));
|
|
250
|
-
break;
|
|
251
|
-
case 'DelegateStake':
|
|
252
|
-
certificates.add(deser_1.csl.Certificate.new_stake_delegation(deser_1.csl.StakeDelegation.new(deser_1.csl.StakeCredential.from_keyhash(deser_1.csl.Ed25519KeyHash.from_hex(cert.stakeKeyHash)), cert.poolId.startsWith('pool')
|
|
253
|
-
? deser_1.csl.Ed25519KeyHash.from_bech32(cert.poolId)
|
|
254
|
-
: deser_1.csl.Ed25519KeyHash.from_hex(cert.poolId))));
|
|
255
|
-
break;
|
|
256
|
-
case 'DeregisterStake':
|
|
257
|
-
certificates.add(deser_1.csl.Certificate.new_stake_deregistration(deser_1.csl.StakeDeregistration.new(deser_1.csl.StakeCredential.from_keyhash(deser_1.csl.Ed25519KeyHash.from_hex(cert.stakeKeyHash)))));
|
|
258
|
-
break;
|
|
259
|
-
case 'RetirePool':
|
|
260
|
-
certificates.add(deser_1.csl.Certificate.new_pool_retirement(deser_1.csl.PoolRetirement.new(cert.poolId.startsWith('pool')
|
|
261
|
-
? deser_1.csl.Ed25519KeyHash.from_bech32(cert.poolId)
|
|
262
|
-
: deser_1.csl.Ed25519KeyHash.from_hex(cert.poolId), cert.epoch)));
|
|
263
|
-
}
|
|
264
|
-
};
|
|
265
|
-
this.addAllCertificates = (allCertificates) => {
|
|
266
|
-
const certificates = deser_1.csl.Certificates.new();
|
|
267
|
-
allCertificates.forEach((cert) => {
|
|
268
|
-
this.addCertificate(certificates, cert);
|
|
269
|
-
});
|
|
270
|
-
this.txBuilder.set_certs(certificates);
|
|
271
|
-
};
|
|
272
|
-
this.addCostModels = () => {
|
|
273
|
-
this.txBuilder.calc_script_data_hash(deser_1.csl.TxBuilderConstants.plutus_vasil_cost_models());
|
|
274
|
-
};
|
|
275
|
-
this.addChange = (changeAddress) => {
|
|
276
|
-
this.txBuilder.add_change_if_needed(deser_1.csl.Address.from_bech32(changeAddress));
|
|
277
|
-
};
|
|
278
|
-
this.addValidityRange = ({ invalidBefore, invalidHereafter }) => {
|
|
279
|
-
if (invalidBefore) {
|
|
280
|
-
this.txBuilder.set_validity_start_interval_bignum(deser_1.csl.BigNum.from_str(invalidBefore.toString()));
|
|
281
|
-
}
|
|
282
|
-
if (invalidHereafter) {
|
|
283
|
-
this.txBuilder.set_ttl_bignum(deser_1.csl.BigNum.from_str(invalidHereafter.toString()));
|
|
284
|
-
}
|
|
285
|
-
};
|
|
286
|
-
this.addAllRequiredSignatures = (requiredSignatures) => {
|
|
287
|
-
requiredSignatures.forEach((pubKeyHash) => {
|
|
288
|
-
this.txBuilder.add_required_signer(deser_1.csl.Ed25519KeyHash.from_hex(pubKeyHash));
|
|
289
|
-
});
|
|
290
|
-
};
|
|
291
|
-
this.addAllMetadata = (allMetadata) => {
|
|
292
|
-
allMetadata.forEach(({ tag, metadata }) => {
|
|
293
|
-
this.txBuilder.add_json_metadatum(deser_1.csl.BigNum.from_str(tag), json_bigint_1.default.stringify(metadata));
|
|
294
|
-
});
|
|
295
|
-
};
|
|
296
|
-
this.castRawDataToJsonString = (rawData) => {
|
|
297
|
-
if (typeof rawData === 'object') {
|
|
298
|
-
return json_bigint_1.default.stringify(rawData);
|
|
299
|
-
}
|
|
300
|
-
return rawData;
|
|
301
|
-
};
|
|
302
|
-
this.castDataToPlutusData = ({ type, content }) => {
|
|
303
|
-
if (type === 'Mesh') {
|
|
304
|
-
return (0, deser_1.toPlutusData)(content);
|
|
305
|
-
}
|
|
306
|
-
if (type === 'CBOR') {
|
|
307
|
-
return deser_1.csl.PlutusData.from_hex(content);
|
|
308
|
-
}
|
|
309
|
-
return deser_1.csl.PlutusData.from_json(content, deser_1.csl.PlutusDatumSchema.DetailedSchema);
|
|
310
|
-
};
|
|
311
42
|
this.protocolParams = protocolParams || common_1.DEFAULT_PROTOCOL_PARAMETERS;
|
|
312
|
-
this.txBuilder = (0, deser_1.buildTxBuilder)(protocolParams);
|
|
313
43
|
}
|
|
314
44
|
serializeTxBody(txBody, protocolParams) {
|
|
315
|
-
|
|
316
|
-
this.protocolParams = protocolParams;
|
|
317
|
-
}
|
|
318
|
-
this.txBuilder = (0, deser_1.buildTxBuilder)(protocolParams);
|
|
319
|
-
const { inputs, outputs, extraInputs, selectionThreshold, collaterals, referenceInputs, mints, changeAddress, certificates, validityRange, requiredSignatures, metadata, } = txBody;
|
|
320
|
-
if (extraInputs.length > 0) {
|
|
321
|
-
this.addUtxosFrom(extraInputs, String(selectionThreshold));
|
|
322
|
-
}
|
|
323
|
-
this.removeDuplicateInputs();
|
|
324
|
-
mints.sort((a, b) => a.policyId.localeCompare(b.policyId));
|
|
325
|
-
inputs.sort((a, b) => {
|
|
326
|
-
if (a.txIn.txHash === b.txIn.txHash) {
|
|
327
|
-
return a.txIn.txIndex - b.txIn.txIndex;
|
|
328
|
-
}
|
|
329
|
-
return a.txIn.txHash.localeCompare(b.txIn.txHash);
|
|
330
|
-
});
|
|
331
|
-
this.addAllInputs(inputs);
|
|
332
|
-
this.addAllOutputs(outputs);
|
|
333
|
-
this.addAllCollaterals(collaterals);
|
|
334
|
-
this.addAllReferenceInputs(referenceInputs);
|
|
335
|
-
this.addAllMints(mints);
|
|
336
|
-
this.addAllCertificates(certificates);
|
|
337
|
-
this.addValidityRange(validityRange);
|
|
338
|
-
this.addAllRequiredSignatures(requiredSignatures);
|
|
339
|
-
this.addAllMetadata(metadata);
|
|
340
|
-
this.addCostModels();
|
|
341
|
-
if (changeAddress) {
|
|
342
|
-
// Hacky fix to set a dummy collateral return so fees are calculated correctly
|
|
343
|
-
const totalCollateral = collaterals
|
|
344
|
-
.map((collateral) => {
|
|
345
|
-
var _a, _b;
|
|
346
|
-
return ((_b = (_a = collateral.txIn.amount) === null || _a === void 0 ? void 0 : _a.find((asset) => asset.unit === 'lovelace')) === null || _b === void 0 ? void 0 : _b.quantity) || '0';
|
|
347
|
-
})
|
|
348
|
-
.reduce((acc, curr) => acc + parseInt(curr), 0);
|
|
349
|
-
const collateralEstimate = Math.ceil((this.protocolParams.collateralPercent *
|
|
350
|
-
Number(Number(this.txBuilder
|
|
351
|
-
.min_fee()
|
|
352
|
-
.checked_add(deser_1.csl.BigNum.from_str('10000'))
|
|
353
|
-
.to_js_value()))) /
|
|
354
|
-
100);
|
|
355
|
-
let collateralReturnNeeded = false;
|
|
356
|
-
if (totalCollateral - collateralEstimate > 0) {
|
|
357
|
-
const collateralEstimateOutput = deser_1.csl.TransactionOutput.new(deser_1.csl.Address.from_bech32(changeAddress), deser_1.csl.Value.new(deser_1.csl.BigNum.from_str(String(collateralEstimate))));
|
|
358
|
-
if (totalCollateral - collateralEstimate >
|
|
359
|
-
Number(deser_1.csl
|
|
360
|
-
.min_ada_for_output(collateralEstimateOutput, deser_1.csl.DataCost.new_coins_per_byte(deser_1.csl.BigNum.from_str(this.protocolParams.coinsPerUTxOSize)))
|
|
361
|
-
.to_js_value())) {
|
|
362
|
-
this.txBuilder.set_collateral_return(deser_1.csl.TransactionOutput.new(deser_1.csl.Address.from_bech32(changeAddress), deser_1.csl.Value.new(deser_1.csl.BigNum.from_str(String(totalCollateral)))));
|
|
363
|
-
this.txBuilder.set_total_collateral(deser_1.csl.BigNum.from_str(String(totalCollateral)));
|
|
364
|
-
collateralReturnNeeded = true;
|
|
365
|
-
}
|
|
366
|
-
}
|
|
367
|
-
this.addChange(changeAddress);
|
|
368
|
-
if (collateralReturnNeeded)
|
|
369
|
-
this.addCollateralReturn(changeAddress);
|
|
370
|
-
}
|
|
371
|
-
const tx = this.txBuilder.build_tx();
|
|
372
|
-
// const txJson = JSON.parse(tx.to_json());
|
|
373
|
-
const txHex = tx.to_hex();
|
|
45
|
+
const txHex = deser_1.csl.js_serialize_tx_body(JSON.stringify(txBody), JSON.stringify(protocolParams || this.protocolParams));
|
|
374
46
|
return txHex;
|
|
375
47
|
}
|
|
376
48
|
addSigningKeys(txHex, signingKeys) {
|
|
@@ -379,11 +51,5 @@ class CSLSerializer {
|
|
|
379
51
|
}
|
|
380
52
|
return txHex;
|
|
381
53
|
}
|
|
382
|
-
decimalToFraction(decimal) {
|
|
383
|
-
const powerOf10 = Math.pow(10, decimal.toString().split('.')[1].length);
|
|
384
|
-
const numerator = decimal * powerOf10;
|
|
385
|
-
const denominator = powerOf10;
|
|
386
|
-
return [numerator, denominator];
|
|
387
|
-
}
|
|
388
54
|
}
|
|
389
55
|
exports.CSLSerializer = CSLSerializer;
|
|
@@ -1,6 +1,5 @@
|
|
|
1
|
-
import type {
|
|
1
|
+
import type { BuilderData, Data } from '@meshsdk/common';
|
|
2
2
|
import { csl } from './csl';
|
|
3
|
-
import { Value, PlutusData, ScriptRef, TransactionUnspentOutput } from './types';
|
|
4
3
|
export declare const toAddress: (bech32: string) => csl.Address;
|
|
5
4
|
export declare const toBaseAddress: (bech32: string) => csl.BaseAddress | undefined;
|
|
6
5
|
export declare const toEnterpriseAddress: (bech32: string) => csl.EnterpriseAddress | undefined;
|
|
@@ -9,19 +8,6 @@ export declare const fromBytes: (bytes: Uint8Array) => string;
|
|
|
9
8
|
export declare const toBytes: (hex: string) => Uint8Array;
|
|
10
9
|
export declare const fromLovelace: (lovelace: number) => number;
|
|
11
10
|
export declare const toLovelace: (ada: number) => number;
|
|
12
|
-
export declare const fromNativeScript: (script: csl.NativeScript) => NativeScript;
|
|
13
|
-
export declare const toNativeScript: (script: NativeScript) => csl.NativeScript;
|
|
14
|
-
export declare const fromPlutusData: (plutusData: PlutusData) => Data;
|
|
15
11
|
export declare const toPlutusData: (data: Data) => csl.PlutusData;
|
|
16
|
-
export declare const
|
|
17
|
-
export declare const
|
|
18
|
-
export declare const toRelay: (relay: Relay) => csl.Relay;
|
|
19
|
-
export declare const fromScriptRef: (scriptRef: ScriptRef) => NativeScript | PlutusScript;
|
|
20
|
-
export declare const toScriptRef: (script: PlutusScript | NativeScript) => csl.ScriptRef;
|
|
21
|
-
export declare const fromTxUnspentOutput: (txUnspentOutput: TransactionUnspentOutput) => UTxO;
|
|
22
|
-
export declare const toTxUnspentOutput: (utxo: UTxO) => csl.TransactionUnspentOutput;
|
|
23
|
-
export declare const toUnitInterval: (float: string) => csl.UnitInterval;
|
|
24
|
-
export declare const fromUTF8: (utf8: string) => string;
|
|
25
|
-
export declare const toUTF8: (hex: string) => string;
|
|
26
|
-
export declare const fromValue: (value: Value) => Asset[];
|
|
27
|
-
export declare const toValue: (assets: Asset[]) => csl.Value;
|
|
12
|
+
export declare const castRawDataToJsonString: (rawData: object | string) => string;
|
|
13
|
+
export declare const castDataToPlutusData: ({ type, content }: BuilderData) => csl.PlutusData;
|