-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathvalidator.js
70 lines (58 loc) · 2.08 KB
/
validator.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
/*
* This script reads Helios code from validator.hl,
* populates Datum values for TIME_LOCK, OWNER and BENEFICIARY,
* and generates final artifacts which can be used to interact with
* the smart contract.
*
* See dist folder for artifacts, which include:
* - validator.addr (testnet address)
* - validator.json
* - datum.json
*/
import { Program, Address, hexToBytes } from "@hyperionbt/helios"
import fs from "fs";
// Replace the following pubkeyhash values with your own values.
// These values can be generated using the following command:
// $ cardano-cli address key-hash --payment-verification-key-file <payment.vkey>
const ownerPKH = ""
const beneficiaryPKH = ""
// Used to set TIME_LOCK
const fiveMinsFromNow = (new Date()).getTime() + 1000 * 60 * 5;
// Reads the Helios source code
const src = fs.readFileSync('./validator.hl').toString();
const program = Program.new(src);
// Sets values for the Datum
program.changeParam("TIME_LOCK", JSON.stringify(fiveMinsFromNow));
program.changeParam("OWNER", JSON.stringify(hexToBytes(ownerPKH)));
program.changeParam("BENEFICIARY", JSON.stringify(hexToBytes(beneficiaryPKH)));
// Print the final Helios source code:
// console.log("\n" + program.toString() + "\n");
// must be set to false in order to print to log
const uplc = program.compile(false);
// set to false for mainnet address
const IS_TESTNET = true;
const scriptAddr = Address.
fromValidatorHash(uplc.validatorHash, null, IS_TESTNET).
toBech32();
console.log("\nGenerating Artifacts\n");
fs.writeFile('dist/validator.addr', scriptAddr, err => {
if (err) {
console.error(err);
}
console.log("Wrote to dist/validator.addr");
});
const validatorJSON = uplc.serialize();
fs.writeFile('dist/validator.json', validatorJSON, err => {
if (err) {
console.error(err);
}
console.log("Wrote to dist/validator.json");
});
const datum = program.evalParam("DATUM");
const datumJSON = datum.data.toSchemaJson();
fs.writeFile('dist/datum.json', datumJSON, err => {
if (err) {
console.error(err);
}
console.log("Wrote to dist/datum.json");
});