Skip to content

Commit b4490f4

Browse files
committed
feat: batch creation of test data
1 parent 39cf206 commit b4490f4

File tree

2 files changed

+91
-58
lines changed

2 files changed

+91
-58
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
[
2+
{
3+
"origin": "foo.example",
4+
"records": [
5+
"foo.example,ns,ns1.foo.example",
6+
"ns1.foo.example,a,1.2.3.4"
7+
]
8+
},
9+
{
10+
"origin": "bar.example",
11+
"records": [
12+
"bar.example,ns,ns1.bar.example",
13+
"ns1.bar.example,a,5.6.7.8"
14+
]
15+
}
16+
]

scripts/create-test-data/main.ts

+75-58
Original file line numberDiff line numberDiff line change
@@ -14,23 +14,34 @@ loadSync({ export: true, allowEmptyValues: true });
1414
const generate = new Command()
1515
.description("Generate an unsigned TX with the test domain data")
1616
.env("MAESTRO_API_KEY=<value:string>", "Maestro API key", { required: true })
17-
.option("-D, --domain <domain>", "Domain to create test data for", { required: true })
18-
.option("-n, --nameserver <nameserver>", "Nameserver for domain, specified as: <name>,<ipaddr> (can be specified multiple times)", { collect: true, required: true })
17+
.option("-f, --data-file <file>", "JSON file containing domain data")
18+
.option("-D, --domain <domain>", "Domain to create test data for") //, { required: true })
19+
.option("-n, --nameserver <nameserver>", "Nameserver for domain, specified as: <name>,<ipaddr> (can be specified multiple times)", { collect: true }) //, required: true })
1920
.option("-r, --record <record>", "Record for domain, specified as: <name>[,<ttl>],<type>,<value> (can be specified multiple times)", { collect: true })
20-
.option("-s, --source-address <address>", "Source wallet address to send from (you must be able to sign transactions for this)", { required: true })
21-
.option("-d, --dest-address <address>", "Destination wallet address to send to (this will be read by cdnsd)", { required: true })
21+
.option("-s, --source-address <address>", "Source wallet address to send from (you must be able to sign transactions for this)") //, { required: true })
22+
.option("-d, --dest-address <address>", "Destination wallet address to send to (this will be read by cdnsd)") //, { required: true })
2223
.option("-o, --output <file>", "Output file for generated transaction")
23-
.action(async ({ maestroApiKey, domain, nameserver, record, sourceAddress, destAddress, output }) => {
24-
// Merge --nameserver and --record values
25-
let records = []
26-
for (var tmpNameserver of nameserver) {
27-
const tmpNameserverParts = tmpNameserver.split(",")
28-
// Nameservers for a domain need both a NS record on the domain and an A record for themselves
29-
records.push(`${domain},ns,${tmpNameserverParts[0]}`)
30-
records.push(`${tmpNameserverParts[0]},a,${tmpNameserverParts[1]}`)
31-
}
32-
for (var tmpRecord in record) {
33-
records.push(tmpRecord)
24+
.action(async ({ maestroApiKey, dataFile, domain, nameserver, record, sourceAddress, destAddress, output }) => {
25+
let domains = []
26+
if (dataFile === undefined) {
27+
// TODO: check for required params
28+
let tmpDomain = {
29+
origin: domain,
30+
records: [],
31+
}
32+
// Merge --nameserver and --record values
33+
for (var tmpNameserver of nameserver) {
34+
const tmpNameserverParts = tmpNameserver.split(",")
35+
// Nameservers for a domain need both a NS record on the domain and an A record for themselves
36+
tmpDomain.records.push(`${domain},ns,${tmpNameserverParts[0]}`)
37+
tmpDomain.records.push(`${tmpNameserverParts[0]},a,${tmpNameserverParts[1]}`)
38+
}
39+
for (var tmpRecord in record) {
40+
tmpDomain.records.push(tmpRecord)
41+
}
42+
domains.push(tmpDomain)
43+
} else {
44+
domains = JSON.parse(Deno.readTextFileSync(dataFile));
3445
}
3546

3647
console.log(`Building transaction...`);
@@ -44,51 +55,57 @@ const generate = new Command()
4455

4556
lucid.selectWalletFrom({ address: sourceAddress });
4657

47-
let outDatumRecords = []
48-
records.forEach((tmpRecord) => {
49-
const recordParts = tmpRecord.split(",")
50-
if (recordParts.length == 3) {
51-
outDatumRecords.push(new Constr(
52-
1,
53-
[
54-
fromText(recordParts[0]),
55-
fromText(recordParts[1]),
56-
fromText(recordParts[2]),
57-
],
58-
))
59-
} else if (recordParts.length == 4) {
60-
outDatumRecords.push(new Constr(
61-
1,
62-
[
63-
fromText(recordParts[0]),
64-
BigInt(parseInt(recordParts[1])),
65-
fromText(recordParts[2]),
66-
fromText(recordParts[3]),
67-
],
68-
))
69-
} else {
70-
console.log(`Invalid record: ${tmpRecord}`)
71-
Deno.exit(1)
72-
}
73-
})
74-
75-
const outDatum = new Constr(1, [
76-
fromText(domain),
77-
outDatumRecords,
78-
]);
58+
try {
59+
let tx = await lucid
60+
.newTx()
7961

80-
const outDatumEncoded = Data.to(outDatum);
62+
for (var domain of domains) {
63+
let outDatumRecords = []
64+
domain.records.forEach((tmpRecord) => {
65+
const recordParts = tmpRecord.split(",")
66+
if (recordParts.length == 3) {
67+
outDatumRecords.push(new Constr(
68+
1,
69+
[
70+
fromText(recordParts[0]),
71+
fromText(recordParts[1]),
72+
fromText(recordParts[2]),
73+
],
74+
))
75+
} else if (recordParts.length == 4) {
76+
outDatumRecords.push(new Constr(
77+
1,
78+
[
79+
fromText(recordParts[0]),
80+
BigInt(parseInt(recordParts[1])),
81+
fromText(recordParts[2]),
82+
fromText(recordParts[3]),
83+
],
84+
))
85+
} else {
86+
console.log(`Invalid record: ${tmpRecord}`)
87+
Deno.exit(1)
88+
}
89+
})
90+
91+
const outDatum = new Constr(1, [
92+
fromText(domain.origin),
93+
outDatumRecords,
94+
]);
95+
96+
const outDatumEncoded = Data.to(outDatum);
97+
98+
//console.log(`outDatumEncoded = ${outDatumEncoded}`)
8199

82-
//console.log(`outDatumEncoded = ${outDatumEncoded}`)
100+
tx = tx
101+
.payToAddressWithData(
102+
destAddress,
103+
{ inline: outDatumEncoded },
104+
{ lovelace: 2_000_000 },
105+
);
106+
}
83107

84-
try {
85-
const txOut = await lucid
86-
.newTx()
87-
.payToAddressWithData(
88-
destAddress,
89-
{ inline: outDatumEncoded },
90-
{ lovelace: 2_000_000 },
91-
)
108+
const txOut = await tx
92109
// 10 minutes
93110
.validTo(Date.now() + 600_000)
94111
.complete();
@@ -101,7 +118,7 @@ const generate = new Command()
101118
const txJson = JSON.stringify(txJsonObj)
102119

103120
if (output === undefined) {
104-
output = `./tx-cdnsd-test-data-${domain}-${txOut.toHash()}.json`
121+
output = `./tx-cdnsd-test-data-${txOut.toHash()}.json`
105122
}
106123
Deno.writeTextFileSync(output, txJson)
107124

0 commit comments

Comments
 (0)