improve script hex parsing validation

This commit is contained in:
Mononaut 2023-07-23 13:55:52 +09:00
parent 0ce043cca9
commit 48b55eed46
No known key found for this signature in database
GPG Key ID: A3F058E41374C04E

View File

@ -283,7 +283,10 @@ export function isFeatureActive(network: string, height: number, feature: 'rbf'
}
export async function calcScriptHash$(script: string): Promise<string> {
const buf = Uint8Array.from(script.match(/.{1,2}/g).map((byte) => parseInt(byte, 16)));
if (!/^[0-9a-fA-F]*$/.test(script) || script.length % 2 !== 0) {
throw new Error('script is not a valid hex string');
}
const buf = Uint8Array.from(script.match(/.{2}/g).map((byte) => parseInt(byte, 16)));
const hashBuffer = await crypto.subtle.digest('SHA-256', buf);
const hashArray = Array.from(new Uint8Array(hashBuffer));
return hashArray