solana-web3.js icon indicating copy to clipboard operation
solana-web3.js copied to clipboard

[experimental] Figure out how to write an assertion function for fully signed transactions while TypeScript does not support async assertions and typeguards

Open steveluscher opened this issue 2 years ago • 3 comments

See https://github.com/microsoft/TypeScript/issues/37681#issuecomment-1650605236.

Essentially you can write an assertion function (playground)…

type NumberLessThanTen = number & { readonly brand: unique symbol }; 

function assertIsLessThan10(num: number): asserts num is NumberLessThanTen {
  if (num >= 10) {
    throw new Error('Too big');
  }
}
const num = 9;
assertIsLessThan10(num);
num satisfies NumberLessThanTen;

…but as soon as you make that assertion function async it stops working.

Options:

  1. We could write an assertIsFullySignedTransaction() function that just checks to see if there is a signature for each signer, without actually performing any verification
  2. We could break from the existing pattern of assertFoo/isFoo and make a function that both asserts and returns the transaction with the IFullySignedTransaction type added.

Neither of these are great, and both are really hard to take back (ie. deprecate) once we've released them.

steveluscher avatar Oct 24 '23 00:10 steveluscher

I'd argue we don't need this assert method to be async in the first place. Since the goal is the ensure it is "fully signed", we just need to go through the account metas that are required to sign, deduplicate the addresses and ensure the amount of unique signers required matches the amount of signatures in the transaction.

I'm assuming the async part would be to also verify each of these signatures match the expected set of signers but IMO that's a bit overkill when all we want is ensure we've not missed one signature.

lorisleiva avatar Oct 24 '23 08:10 lorisleiva

Agreed. I think it'd be okay to have the type assertion just assert there's a signature for each expected signer, and then optionally to have a separate function(s) that asserts/checks signature validity without changing the type.

Arguably those are separate operations anyway, eg you might want to check the validity of the current signatures on a partially signed transaction.

mcintyre94 avatar Oct 24 '23 12:10 mcintyre94

assertIsFullyButPossiblyBadlySignedTransaction()

steveluscher avatar Oct 25 '23 04:10 steveluscher

Because there has been no activity on this issue for 7 days since it was closed, it has been automatically locked. Please open a new issue if it requires a follow up.

github-actions[bot] avatar Nov 07 '24 08:11 github-actions[bot]