1- import { decodePrivateKeyWif , hexToBin , isHex , secp256k1 , SigningSerializationFlag } from '@bitauth/libauth' ;
1+ import { decodePrivateKeyWif , hexToBin , isHex , secp256k1 } from '@bitauth/libauth' ;
22import { hash256 , scriptToBytecode } from '@cashscript/utils' ;
33import {
44 GenerateUnlockingBytecodeOptions ,
5- HashType ,
5+ SighashType ,
66 SignatureAlgorithm ,
77 P2PKHUnlocker ,
88} from './interfaces.js' ;
9- import { createSighashPreimage , publicKeyToP2PKHLockingBytecode } from './utils.js' ;
9+ import { createSighashPreimage , publicKeyToP2PKHLockingBytecode , toSigningSerializationType } from './utils.js' ;
1010
1111/**
1212 * A signature template used to sign CashScript transactions. Wraps a private key together with
13- * the desired `HashType ` and `SignatureAlgorithm`, and is consumed by the `TransactionBuilder`
13+ * the desired `SighashType ` and `SignatureAlgorithm`, and is consumed by the `TransactionBuilder`
1414 * whenever a `sig` argument is required or when unlocking a P2PKH input.
1515 */
1616export default class SignatureTemplate {
1717 /** The raw private key bytes used for signing. */
1818 public privateKey : Uint8Array ;
1919
20+ /** The 33-byte compressed public key that corresponds to the template's private key. */
21+ get publicKey ( ) : Uint8Array {
22+ return secp256k1 . derivePublicKeyCompressed ( this . privateKey ) as Uint8Array ;
23+ }
24+
2025 /**
2126 * Create a new SignatureTemplate.
2227 *
2328 * @param signer - A 32-byte private key (Uint8Array), a WIF or hex-encoded private key string,
2429 * or any object exposing a `toWIF()` method (e.g. bitcore-lib or bitcoincashjs `ECPair`).
25- * @param hashtype - Sighash flags to use when signing. Defaults to `SIGHASH_ALL | SIGHASH_UTXOS`.
30+ * @param sighashType - Sighash flags to use when signing. Defaults to `SIGHASH_ALL | SIGHASH_UTXOS`.
2631 * @param signatureAlgorithm - The signature algorithm to use. Defaults to
2732 * `SignatureAlgorithm.SCHNORR`.
2833 */
2934 constructor (
3035 signer : Keypair | Uint8Array | string ,
31- private hashtype : HashType = HashType . SIGHASH_ALL | HashType . SIGHASH_UTXOS ,
32- private signatureAlgorithm : SignatureAlgorithm = SignatureAlgorithm . SCHNORR ,
36+ public readonly sighashType : SighashType = SighashType . SIGHASH_ALL | SighashType . SIGHASH_UTXOS ,
37+ public readonly signatureAlgorithm : SignatureAlgorithm = SignatureAlgorithm . SCHNORR ,
3338 ) {
3439 if ( isKeypair ( signer ) ) {
3540 const wif = signer . toWIF ( ) ;
@@ -47,24 +52,22 @@ export default class SignatureTemplate {
4752 }
4853
4954 /**
50- * Sign the provided sighash payload and return the signature concatenated with the hashtype
55+ * Sign the provided sighash payload and return the signature concatenated with the sighash type
5156 * byte, ready to be used as a transaction signature.
5257 *
5358 * @param payload - The 32-byte sighash to sign.
54- * @param bchForkId - Whether to include the BCH fork id flag in the appended hashtype byte.
55- * Defaults to `true`.
56- * @returns The signature bytes followed by the hashtype byte.
59+ * @returns The signature bytes followed by the sighash type byte.
5760 */
58- generateSignature ( payload : Uint8Array , bchForkId ?: boolean ) : Uint8Array {
61+ generateSignature ( payload : Uint8Array ) : Uint8Array {
5962 const signature = this . signMessageHash ( payload ) ;
60- return Uint8Array . from ( [ ...signature , this . getHashType ( bchForkId ) ] ) ;
63+ return Uint8Array . from ( [ ...signature , toSigningSerializationType ( this . sighashType ) ] ) ;
6164 }
6265
6366 /**
6467 * Sign a raw 32-byte message hash using the template's private key and signature algorithm.
6568 *
6669 * @param payload - The 32-byte hash to sign.
67- * @returns The raw signature bytes (without an appended hashtype byte).
70+ * @returns The raw signature bytes (without an appended sighash type byte).
6871 */
6972 signMessageHash ( payload : Uint8Array ) : Uint8Array {
7073 const signature = this . signatureAlgorithm === SignatureAlgorithm . SCHNORR
@@ -74,50 +77,22 @@ export default class SignatureTemplate {
7477 return signature ;
7578 }
7679
77- /**
78- * Get the sighash flags used by this template.
79- *
80- * @param bchForkId - Whether to OR in the BCH fork id flag. Defaults to `true`.
81- * @returns The combined hashtype byte.
82- */
83- getHashType ( bchForkId : boolean = true ) : number {
84- return bchForkId ? ( this . hashtype | SigningSerializationFlag . forkId ) : this . hashtype ;
85- }
86-
87- /**
88- * @returns The signature algorithm (ECDSA or Schnorr) used by this template.
89- */
90- getSignatureAlgorithm ( ) : SignatureAlgorithm {
91- return this . signatureAlgorithm ;
92- }
93-
94- /**
95- * Derive the compressed public key that corresponds to the template's private key.
96- *
97- * @returns The 33-byte compressed public key.
98- */
99- getPublicKey ( ) : Uint8Array {
100- return secp256k1 . derivePublicKeyCompressed ( this . privateKey ) as Uint8Array ;
101- }
102-
10380 /**
10481 * Build a P2PKH `Unlocker` for the address derived from this template's private key. The
10582 * returned unlocker can be passed directly to `TransactionBuilder.addInput`.
10683 *
10784 * @returns An unlocker that signs the corresponding P2PKH UTXO.
10885 */
10986 unlockP2PKH ( ) : P2PKHUnlocker {
110- const publicKey = this . getPublicKey ( ) ;
111- const prevOutScript = publicKeyToP2PKHLockingBytecode ( publicKey ) ;
112- const hashtype = this . getHashType ( ) ;
87+ const prevOutScript = publicKeyToP2PKHLockingBytecode ( this . publicKey ) ;
11388
11489 return {
11590 generateLockingBytecode : ( ) => prevOutScript ,
11691 generateUnlockingBytecode : ( { transaction, sourceOutputs, inputIndex } : GenerateUnlockingBytecodeOptions ) => {
117- const preimage = createSighashPreimage ( transaction , sourceOutputs , inputIndex , prevOutScript , hashtype ) ;
92+ const preimage = createSighashPreimage ( transaction , sourceOutputs , inputIndex , prevOutScript , this . sighashType ) ;
11893 const sighash = hash256 ( preimage ) ;
11994 const signature = this . generateSignature ( sighash ) ;
120- const unlockingBytecode = scriptToBytecode ( [ signature , publicKey ] ) ;
95+ const unlockingBytecode = scriptToBytecode ( [ signature , this . publicKey ] ) ;
12196 return unlockingBytecode ;
12297 } ,
12398 template : this ,
0 commit comments