Skip to content

Commit

Permalink
Merge pull request #106 from blinklabs-io/feat/test-data-script-batch
Browse files Browse the repository at this point in the history
feat: batch creation of test data
  • Loading branch information
agaffney authored Jan 14, 2024
2 parents 4b936c7 + b4490f4 commit 273fbb7
Show file tree
Hide file tree
Showing 2 changed files with 91 additions and 58 deletions.
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

0 comments on commit 273fbb7

Please sign in to comment.