Skip to content

feat: batch creation of test data #106

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jan 14, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 16 additions & 0 deletions scripts/create-test-data/example-test-data.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
[
{
"origin": "foo.example",
"records": [
"foo.example,ns,ns1.foo.example",
"ns1.foo.example,a,1.2.3.4"
]
},
{
"origin": "bar.example",
"records": [
"bar.example,ns,ns1.bar.example",
"ns1.bar.example,a,5.6.7.8"
]
}
]
133 changes: 75 additions & 58 deletions scripts/create-test-data/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,23 +14,34 @@ loadSync({ export: true, allowEmptyValues: true });
const generate = new Command()
.description("Generate an unsigned TX with the test domain data")
.env("MAESTRO_API_KEY=<value:string>", "Maestro API key", { required: true })
.option("-D, --domain <domain>", "Domain to create test data for", { required: true })
.option("-n, --nameserver <nameserver>", "Nameserver for domain, specified as: <name>,<ipaddr> (can be specified multiple times)", { collect: true, required: true })
.option("-f, --data-file <file>", "JSON file containing domain data")
.option("-D, --domain <domain>", "Domain to create test data for") //, { required: true })
.option("-n, --nameserver <nameserver>", "Nameserver for domain, specified as: <name>,<ipaddr> (can be specified multiple times)", { collect: true }) //, required: true })
.option("-r, --record <record>", "Record for domain, specified as: <name>[,<ttl>],<type>,<value> (can be specified multiple times)", { collect: true })
.option("-s, --source-address <address>", "Source wallet address to send from (you must be able to sign transactions for this)", { required: true })
.option("-d, --dest-address <address>", "Destination wallet address to send to (this will be read by cdnsd)", { required: true })
.option("-s, --source-address <address>", "Source wallet address to send from (you must be able to sign transactions for this)") //, { required: true })
.option("-d, --dest-address <address>", "Destination wallet address to send to (this will be read by cdnsd)") //, { required: true })
.option("-o, --output <file>", "Output file for generated transaction")
.action(async ({ maestroApiKey, domain, nameserver, record, sourceAddress, destAddress, output }) => {
// Merge --nameserver and --record values
let records = []
for (var tmpNameserver of nameserver) {
const tmpNameserverParts = tmpNameserver.split(",")
// Nameservers for a domain need both a NS record on the domain and an A record for themselves
records.push(`${domain},ns,${tmpNameserverParts[0]}`)
records.push(`${tmpNameserverParts[0]},a,${tmpNameserverParts[1]}`)
}
for (var tmpRecord in record) {
records.push(tmpRecord)
.action(async ({ maestroApiKey, dataFile, domain, nameserver, record, sourceAddress, destAddress, output }) => {
let domains = []
if (dataFile === undefined) {
// TODO: check for required params
let tmpDomain = {
origin: domain,
records: [],
}
// Merge --nameserver and --record values
for (var tmpNameserver of nameserver) {
const tmpNameserverParts = tmpNameserver.split(",")
// Nameservers for a domain need both a NS record on the domain and an A record for themselves
tmpDomain.records.push(`${domain},ns,${tmpNameserverParts[0]}`)
tmpDomain.records.push(`${tmpNameserverParts[0]},a,${tmpNameserverParts[1]}`)
}
for (var tmpRecord in record) {
tmpDomain.records.push(tmpRecord)
}
domains.push(tmpDomain)
} else {
domains = JSON.parse(Deno.readTextFileSync(dataFile));
}

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

lucid.selectWalletFrom({ address: sourceAddress });

let outDatumRecords = []
records.forEach((tmpRecord) => {
const recordParts = tmpRecord.split(",")
if (recordParts.length == 3) {
outDatumRecords.push(new Constr(
1,
[
fromText(recordParts[0]),
fromText(recordParts[1]),
fromText(recordParts[2]),
],
))
} else if (recordParts.length == 4) {
outDatumRecords.push(new Constr(
1,
[
fromText(recordParts[0]),
BigInt(parseInt(recordParts[1])),
fromText(recordParts[2]),
fromText(recordParts[3]),
],
))
} else {
console.log(`Invalid record: ${tmpRecord}`)
Deno.exit(1)
}
})

const outDatum = new Constr(1, [
fromText(domain),
outDatumRecords,
]);
try {
let tx = await lucid
.newTx()

const outDatumEncoded = Data.to(outDatum);
for (var domain of domains) {
let outDatumRecords = []
domain.records.forEach((tmpRecord) => {
const recordParts = tmpRecord.split(",")
if (recordParts.length == 3) {
outDatumRecords.push(new Constr(
1,
[
fromText(recordParts[0]),
fromText(recordParts[1]),
fromText(recordParts[2]),
],
))
} else if (recordParts.length == 4) {
outDatumRecords.push(new Constr(
1,
[
fromText(recordParts[0]),
BigInt(parseInt(recordParts[1])),
fromText(recordParts[2]),
fromText(recordParts[3]),
],
))
} else {
console.log(`Invalid record: ${tmpRecord}`)
Deno.exit(1)
}
})

const outDatum = new Constr(1, [
fromText(domain.origin),
outDatumRecords,
]);

const outDatumEncoded = Data.to(outDatum);

//console.log(`outDatumEncoded = ${outDatumEncoded}`)

//console.log(`outDatumEncoded = ${outDatumEncoded}`)
tx = tx
.payToAddressWithData(
destAddress,
{ inline: outDatumEncoded },
{ lovelace: 2_000_000 },
);
}

try {
const txOut = await lucid
.newTx()
.payToAddressWithData(
destAddress,
{ inline: outDatumEncoded },
{ lovelace: 2_000_000 },
)
const txOut = await tx
// 10 minutes
.validTo(Date.now() + 600_000)
.complete();
Expand All @@ -101,7 +118,7 @@ const generate = new Command()
const txJson = JSON.stringify(txJsonObj)

if (output === undefined) {
output = `./tx-cdnsd-test-data-${domain}-${txOut.toHash()}.json`
output = `./tx-cdnsd-test-data-${txOut.toHash()}.json`
}
Deno.writeTextFileSync(output, txJson)

Expand Down