|
| 1 | +import { Address, getAddressFromPublicKey } from '@solana/addresses'; |
| 2 | +import { Decoder } from '@solana/codecs-core'; |
| 3 | +import { getBase58Decoder } from '@solana/codecs-strings'; |
| 4 | +import { |
| 5 | + SOLANA_ERROR__TRANSACTION__ADDRESSES_CANNOT_SIGN_TRANSACTION, |
| 6 | + SOLANA_ERROR__TRANSACTION__FEE_PAYER_SIGNATURE_MISSING, |
| 7 | + SOLANA_ERROR__TRANSACTION__SIGNATURES_MISSING, |
| 8 | + SolanaError, |
| 9 | +} from '@solana/errors'; |
| 10 | +import { Signature, SignatureBytes, signBytes } from '@solana/keys'; |
| 11 | + |
| 12 | +import { NewTransaction } from './new-compile-transaction'; |
| 13 | + |
| 14 | +export interface FullySignedTransaction extends NewTransaction { |
| 15 | + readonly __brand: unique symbol; |
| 16 | +} |
| 17 | + |
| 18 | +let base58Decoder: Decoder<string> | undefined; |
| 19 | + |
| 20 | +export function newGetSignatureFromTransaction(transaction: NewTransaction): Signature { |
| 21 | + if (!base58Decoder) base58Decoder = getBase58Decoder(); |
| 22 | + |
| 23 | + // We have ordered signatures from the compiled message accounts |
| 24 | + // first signature is the fee payer |
| 25 | + const signatureBytes = Object.values(transaction.signatures)[0]; |
| 26 | + if (!signatureBytes) { |
| 27 | + throw new SolanaError(SOLANA_ERROR__TRANSACTION__FEE_PAYER_SIGNATURE_MISSING); |
| 28 | + } |
| 29 | + const transactionSignature = base58Decoder.decode(signatureBytes); |
| 30 | + return transactionSignature as Signature; |
| 31 | +} |
| 32 | + |
| 33 | +function uint8ArraysEqual(arr1: Uint8Array, arr2: Uint8Array) { |
| 34 | + return arr1.length === arr2.length && arr1.every((value, index) => value === arr2[index]); |
| 35 | +} |
| 36 | + |
| 37 | +export async function newPartiallySignTransaction( |
| 38 | + keyPairs: CryptoKeyPair[], |
| 39 | + transaction: NewTransaction, |
| 40 | +): Promise<NewTransaction> { |
| 41 | + let newSignatures: Record<Address, SignatureBytes> | undefined; |
| 42 | + let unexpectedSigners: Set<Address> | undefined; |
| 43 | + |
| 44 | + await Promise.all( |
| 45 | + keyPairs.map(async keyPair => { |
| 46 | + const address = await getAddressFromPublicKey(keyPair.publicKey); |
| 47 | + const existingSignature = transaction.signatures[address]; |
| 48 | + |
| 49 | + // Check if the address is expected to sign the transaction |
| 50 | + if (existingSignature === undefined) { |
| 51 | + // address is not an expected signer for this transaction |
| 52 | + unexpectedSigners ||= new Set(); |
| 53 | + unexpectedSigners.add(address); |
| 54 | + return; |
| 55 | + } |
| 56 | + |
| 57 | + // Return if there are any unexpected signers already since we won't be using signatures |
| 58 | + if (unexpectedSigners) { |
| 59 | + return; |
| 60 | + } |
| 61 | + |
| 62 | + const newSignature = await signBytes(keyPair.privateKey, transaction.messageBytes); |
| 63 | + |
| 64 | + if (existingSignature !== null && uint8ArraysEqual(newSignature, existingSignature)) { |
| 65 | + // already have the same signature set |
| 66 | + return; |
| 67 | + } |
| 68 | + |
| 69 | + newSignatures ||= {}; |
| 70 | + newSignatures[address] = newSignature; |
| 71 | + }), |
| 72 | + ); |
| 73 | + |
| 74 | + if (unexpectedSigners && unexpectedSigners.size > 0) { |
| 75 | + const expectedSigners = Object.keys(transaction.signatures); |
| 76 | + throw new SolanaError(SOLANA_ERROR__TRANSACTION__ADDRESSES_CANNOT_SIGN_TRANSACTION, { |
| 77 | + expectedAddresses: expectedSigners, |
| 78 | + unexpectedAddresses: [...unexpectedSigners], |
| 79 | + }); |
| 80 | + } |
| 81 | + |
| 82 | + if (!newSignatures) { |
| 83 | + return transaction; |
| 84 | + } |
| 85 | + |
| 86 | + return Object.freeze({ |
| 87 | + ...transaction, |
| 88 | + signatures: Object.freeze({ |
| 89 | + ...transaction.signatures, |
| 90 | + ...newSignatures, |
| 91 | + }), |
| 92 | + }); |
| 93 | +} |
| 94 | + |
| 95 | +export async function newSignTransaction( |
| 96 | + keyPairs: CryptoKeyPair[], |
| 97 | + transaction: NewTransaction, |
| 98 | +): Promise<FullySignedTransaction> { |
| 99 | + const out = await newPartiallySignTransaction(keyPairs, transaction); |
| 100 | + newAssertTransactionIsFullySigned(out); |
| 101 | + Object.freeze(out); |
| 102 | + return out; |
| 103 | +} |
| 104 | + |
| 105 | +export function newAssertTransactionIsFullySigned( |
| 106 | + transaction: NewTransaction, |
| 107 | +): asserts transaction is FullySignedTransaction { |
| 108 | + const missingSigs: Address[] = []; |
| 109 | + Object.entries(transaction.signatures).forEach(([address, signatureBytes]) => { |
| 110 | + if (!signatureBytes) { |
| 111 | + missingSigs.push(address as Address); |
| 112 | + } |
| 113 | + }); |
| 114 | + |
| 115 | + if (missingSigs.length > 0) { |
| 116 | + throw new SolanaError(SOLANA_ERROR__TRANSACTION__SIGNATURES_MISSING, { |
| 117 | + addresses: missingSigs, |
| 118 | + }); |
| 119 | + } |
| 120 | +} |
0 commit comments