@bsv/sdk
Version:
BSV Blockchain Software Development Kit
162 lines • 6.71 kB
TypeScript
import BigNumber from './BigNumber.js';
import PublicKey from './PublicKey.js';
/**
* Represents a digital signature.
*
* A digital signature is a mathematical scheme for verifying the authenticity of
* digital messages or documents. In many scenarios, it is equivalent to a handwritten signature or stamped seal.
* The signature pair (R, S) corresponds to the raw ECDSA ([Elliptic Curve Digital Signature Algorithm](https://en.wikipedia.org/wiki/Elliptic_Curve_Digital_Signature_Algorithm)) signature.
* Signatures are often serialized into a format known as '[DER encoding](https://en.wikipedia.org/wiki/X.690#DER_encoding)' for transmission.
*
* @class Signature
*/
export default class Signature {
/**
* @property Represents the "r" component of the digital signature
*/
r: BigNumber;
/**
* @property Represents the "s" component of the digital signature
*/
s: BigNumber;
/**
* Takes an array of numbers or a string and returns a new Signature instance.
* This method will throw an error if the DER encoding is invalid.
* If a string is provided, it is assumed to represent a hexadecimal sequence.
*
* @static
* @method fromDER
* @param data - The sequence to decode from DER encoding.
* @param enc - The encoding of the data string.
* @returns The decoded data in the form of Signature instance.
*
* @example
* const signature = Signature.fromDER('30440220018c1f5502f8...', 'hex');
*/
static fromDER(data: number[] | string, enc?: 'hex' | 'base64'): Signature;
/**
* Takes an array of numbers or a string and returns a new Signature instance.
* This method will throw an error if the Compact encoding is invalid.
* If a string is provided, it is assumed to represent a hexadecimal sequence.
* compactByte value 27-30 means uncompressed public key.
* 31-34 means compressed public key.
* The range represents the recovery param which can be 0,1,2,3.
* We could support recovery functions in future if there's demand.
*
* @static
* @method fromCompact
* @param data - The sequence to decode from Compact encoding.
* @param enc - The encoding of the data string.
* @returns The decoded data in the form of Signature instance.
*
* @example
* const signature = Signature.fromCompact('1b18c1f5502f8...', 'hex');
*/
static fromCompact(data: number[] | string, enc?: 'hex' | 'base64'): Signature;
/**
* Creates an instance of the Signature class.
*
* @constructor
* @param r - The R component of the signature.
* @param s - The S component of the signature.
*
* @example
* const r = new BigNumber('208755674028...');
* const s = new BigNumber('564745627577...');
* const signature = new Signature(r, s);
*/
constructor(r: BigNumber, s: BigNumber);
/**
* Verifies a digital signature.
*
* This method will return true if the signature, key, and message hash match.
* If the data or key do not match the signature, the function returns false.
*
* @method verify
* @param msg - The message to verify.
* @param key - The public key used to sign the original message.
* @param enc - The encoding of the msg string.
* @returns A boolean representing whether the signature is valid.
*
* @example
* const msg = 'The quick brown fox jumps over the lazy dog';
* const publicKey = PublicKey.fromString('04188ca1050...');
* const isVerified = signature.verify(msg, publicKey);
*/
verify(msg: number[] | string, key: PublicKey, enc?: 'hex'): boolean;
/**
* Converts an instance of Signature into DER encoding.
* An alias for the toDER method.
*
* If the encoding parameter is set to 'hex', the function will return a hex string.
* If 'base64', it will return a base64 string.
* Otherwise, it will return an array of numbers.
*
* @method toDER
* @param enc - The encoding to use for the output.
* @returns The current instance in DER encoding.
*
* @example
* const der = signature.toString('base64');
*/
toString(enc?: 'hex' | 'base64'): number[] | string;
/**
* Converts an instance of Signature into DER encoding.
*
* If the encoding parameter is set to 'hex', the function will return a hex string.
* If 'base64', it will return a base64 string.
* Otherwise, it will return an array of numbers.
*
* @method toDER
* @param enc - The encoding to use for the output.
* @returns The current instance in DER encoding.
*
* @example
* const der = signature.toDER('hex');
*/
toDER(enc?: 'hex' | 'base64'): number[] | string;
/**
* Converts an instance of Signature into Compact encoding.
*
* If the encoding parameter is set to 'hex', the function will return a hex string.
* If 'base64', it will return a base64 string.
* Otherwise, it will return an array of numbers.
*
* @method toCompact
* @param enc - The encoding to use for the output.
* @returns The current instance in DER encoding.
*
* @example
* const compact = signature.toCompact(3, true, 'base64');
*/
toCompact(recovery: number, compressed: boolean, enc?: 'hex' | 'base64'): number[] | string;
/**
* Recovers the public key from a signature.
* This method will return the public key if it finds a valid public key.
* If it does not find a valid public key, it will throw an error.
* The recovery factor is a number between 0 and 3.
* @method RecoverPublicKey
* @param recovery - The recovery factor.
* @param e - The message hash.
* @returns The public key associated with the signature.
*
* @example
* const publicKey = signature.RecoverPublicKey(0, msgHash);
*/
RecoverPublicKey(recovery: number, e: BigNumber): PublicKey;
/**
* Calculates the recovery factor which will work for a particular public key and message hash.
* This method will return the recovery factor if it finds a valid recovery factor.
* If it does not find a valid recovery factor, it will throw an error.
* The recovery factor is a number between 0 and 3.
*
* @method CalculateRecoveryFactor
* @param msgHash - The message hash.
* @returns the recovery factor: number
* /
* @example
* const recovery = signature.CalculateRecoveryFactor(publicKey, msgHash);
*/
CalculateRecoveryFactor(pubkey: PublicKey, msgHash: BigNumber): number;
}
//# sourceMappingURL=Signature.d.ts.map