import * as nearAPI from "near-api-js"; // Refuses what `Buffer.from(value, "hex")` would quietly truncate: it stops at the first invalid // character and drops a trailing half-byte, which would attach a wrong-length signature that only fails at broadcast. function decodeSignature(signature: string): Buffer { const decoded = Buffer.from(signature, "hex"); if (signature.length % 2 !== 0 || decoded.length !== signature.length / 2) { throw new Error("Near: signature is not valid hex"); } if (decoded.length === 0) { throw new Error("Near: signature is empty"); } return decoded; } // Attaches a device signature and returns the signed, base64'd transaction ready to broadcast. The // key type is read back from the transaction's own public key, so it always matches the crafting key. export function combine(tx: string, signature: string[], _pubkey?: string): string { if (signature.length !== 1) { throw new Error(`NEAR combine expects exactly one signature, got ${signature.length}`); } const transaction = nearAPI.Transaction.decode(Buffer.from(tx, "base64")); const signedTransaction = new nearAPI.SignedTransaction({ transaction, signature: new nearAPI.Signature({ keyType: transaction.publicKey.keyType, data: decodeSignature(signature[0]), }), }); return Buffer.from(signedTransaction.encode()).toString("base64"); }