diff --git a/scripts/create-test-data/example-test-data.json b/scripts/create-test-data/example-test-data.json new file mode 100644 index 0000000..98aa612 --- /dev/null +++ b/scripts/create-test-data/example-test-data.json @@ -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" + ] + } +] diff --git a/scripts/create-test-data/main.ts b/scripts/create-test-data/main.ts index 56304bc..26f018a 100644 --- a/scripts/create-test-data/main.ts +++ b/scripts/create-test-data/main.ts @@ -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=", "Maestro API key", { required: true }) - .option("-D, --domain ", "Domain to create test data for", { required: true }) - .option("-n, --nameserver ", "Nameserver for domain, specified as: , (can be specified multiple times)", { collect: true, required: true }) + .option("-f, --data-file ", "JSON file containing domain data") + .option("-D, --domain ", "Domain to create test data for") //, { required: true }) + .option("-n, --nameserver ", "Nameserver for domain, specified as: , (can be specified multiple times)", { collect: true }) //, required: true }) .option("-r, --record ", "Record for domain, specified as: [,],, (can be specified multiple times)", { collect: true }) - .option("-s, --source-address
", "Source wallet address to send from (you must be able to sign transactions for this)", { required: true }) - .option("-d, --dest-address
", "Destination wallet address to send to (this will be read by cdnsd)", { required: true }) + .option("-s, --source-address
", "Source wallet address to send from (you must be able to sign transactions for this)") //, { required: true }) + .option("-d, --dest-address
", "Destination wallet address to send to (this will be read by cdnsd)") //, { required: true }) .option("-o, --output ", "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...`); @@ -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(); @@ -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)