Skip to content

Commit b7a6419

Browse files
committed
feat: make tx ids idempotent, check gobject before submit
1 parent 287abfd commit b7a6419

File tree

1 file changed

+34
-25
lines changed

1 file changed

+34
-25
lines changed

bin/gobject-prepare.js

Lines changed: 34 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -296,7 +296,8 @@ async function main() {
296296
* @param {Uint8Array} txHashBytes
297297
*/
298298
sign: async function (privKeyBytes, txHashBytes) {
299-
let sigOpts = { canonical: true, extraEntropy: true };
299+
// extraEntropy set to null to make gobject transactions idempotent
300+
let sigOpts = { canonical: true, extraEntropy: null };
300301
let sigBytes = await Secp256k1.sign(txHashBytes, privKeyBytes, sigOpts);
301302

302303
return sigBytes;
@@ -323,11 +324,9 @@ async function main() {
323324
console.log("Collateral Address (source of 1 DASH network fee):");
324325
console.log(collateralAddr);
325326

326-
// we can set txid to short circuit for testing
327327
let txid = "";
328-
// ./bin/gobject-prepare.js 1 3 100 https://example.com/proposal-00 proposal-00 yPPy7Z5RQj46SnFtuFXyT6DFAygxESPR7K ./yjZxu7SJAwgSm1JtWybuQRYQDx34z8P2Z7.wif
329-
// txid = "10d9862feb6eac6cf6fa653589e39b60a0ed640bae4140c51c35401ffe019479";
330-
if (!txid) {
328+
let txInfoSigned;
329+
{
331330
let utxosResult = await rpc.getaddressutxos({
332331
addresses: [collateralAddr],
333332
});
@@ -338,7 +337,7 @@ async function main() {
338337
// @type {Array<DashTx.TxOutput>} */
339338
let outputs = [{ memo: gobjIdForward, satoshis: 100000000 }];
340339
let txInfo = { inputs, outputs };
341-
let txInfoSigned = await dashTx.hashAndSignAll(txInfo);
340+
txInfoSigned = await dashTx.hashAndSignAll(txInfo);
342341
console.log(utxosResult);
343342
//
344343

@@ -350,7 +349,36 @@ async function main() {
350349
console.log("Signed Collateral Transaction ID:");
351350
txid = await DashTx.getId(txInfoSigned.transaction);
352351
console.log(txid);
352+
}
353353

354+
async function check() {
355+
let gobjResult = await rpc
356+
.request("/", {
357+
method: "gobject",
358+
params: ["check", gobj.dataHex],
359+
})
360+
.catch(
361+
/** @param {any} err */ function (err) {
362+
console.error(err.message);
363+
console.error(err.code);
364+
console.error(err);
365+
// invalid burn hash
366+
return null;
367+
},
368+
);
369+
370+
// { result: { 'Object status': 'OK' }, error: null, id: 5542 }
371+
if (gobjResult?.result?.["Object status"] !== "OK") {
372+
throw new Error(`gobject failed: ${gobjResult.result.error}`);
373+
}
374+
return gobjResult;
375+
}
376+
377+
await check();
378+
379+
// ./bin/gobject-prepare.js 1 3 100 https://example.com/proposal-00 proposal-00 yPPy7Z5RQj46SnFtuFXyT6DFAygxESPR7K ./yjZxu7SJAwgSm1JtWybuQRYQDx34z8P2Z7.wif
380+
// set to false to short circuit for testing
381+
if (true) {
354382
let txResult = await rpc.request("/", {
355383
method: "sendrawtransaction",
356384
params: [txInfoSigned.transaction],
@@ -395,25 +423,6 @@ async function main() {
395423
await DashGov.utils.sleep(5000);
396424
}
397425

398-
// async function check() {
399-
// let gobjResult = await rpc
400-
// .request("/", {
401-
// method: "gobject",
402-
// params: ["check", gobj.dataHex],
403-
// })
404-
// .catch(
405-
// /** @param {any} err */ function (err) {
406-
// console.error(err.message);
407-
// console.error(err.code);
408-
// console.error(err);
409-
// // invalid collateral hash
410-
// return null;
411-
// },
412-
// );
413-
414-
// return gobjResult;
415-
// }
416-
417426
async function submit() {
418427
let req = {
419428
method: "gobject",

0 commit comments

Comments
 (0)