Skip to content

Commit

Permalink
fix(parseBytesAsScriptNumber): accept options as an object
Browse files Browse the repository at this point in the history
  • Loading branch information
bitjson committed Mar 31, 2020
1 parent 1532785 commit 8a73752
Show file tree
Hide file tree
Showing 4 changed files with 60 additions and 17 deletions.
7 changes: 3 additions & 4 deletions src/lib/auth/instruction-sets/common/combinators.ts
Original file line number Diff line number Diff line change
Expand Up @@ -166,11 +166,10 @@ export const useOneScriptNumber = <
}: { requireMinimalEncoding: boolean; maximumScriptNumberByteLength?: number }
) =>
useOneStackItem(state, (nextState, [item]) => {
const value = parseBytesAsScriptNumber(
item,
const value = parseBytesAsScriptNumber(item, {
maximumScriptNumberByteLength,
requireMinimalEncoding,
maximumScriptNumberByteLength
);
});
if (isScriptNumberError(value)) {
return applyError<State, Errors>(
AuthenticationErrorCommon.invalidScriptNumber,
Expand Down
9 changes: 4 additions & 5 deletions src/lib/auth/instruction-sets/common/time.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,11 +43,10 @@ export const readLocktime = <
state
);
}
const parsedLocktime = parseBytesAsScriptNumber(
item,
flags.requireMinimalEncoding,
Constants.LocktimeScriptNumberByteLength
);
const parsedLocktime = parseBytesAsScriptNumber(item, {
maximumScriptNumberByteLength: Constants.LocktimeScriptNumberByteLength,
requireMinimalEncoding: flags.requireMinimalEncoding,
});
if (isScriptNumberError(parsedLocktime)) {
return applyError<State, Errors>(
AuthenticationErrorCommon.invalidScriptNumber,
Expand Down
49 changes: 43 additions & 6 deletions src/lib/auth/instruction-sets/common/types.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -83,35 +83,72 @@ const equivalentScriptNumbers: readonly [string, string][] = [
];

test('parseBytesAsScriptNumber', (t) => {
minimallyEncodedScriptNumbers.map((pair) => {
t.deepEqual(parseBytesAsScriptNumber(hexToBin(pair[0])), pair[1]);
t.deepEqual(
parseBytesAsScriptNumber(hexToBin(pair[0]), {
requireMinimalEncoding: true,
}),
pair[1]
);
t.deepEqual(
parseBytesAsScriptNumber(hexToBin(pair[0]), {
maximumScriptNumberByteLength: 4,
requireMinimalEncoding: true,
}),
pair[1]
);
return undefined;
});
[...minimallyEncodedScriptNumbers, ...nonMinimallyEncodedScriptNumbers].map(
(pair) => {
t.deepEqual(
parseBytesAsScriptNumber(hexToBin(pair[0]), false, 5),
parseBytesAsScriptNumber(hexToBin(pair[0]), {
maximumScriptNumberByteLength: 5,
requireMinimalEncoding: false,
}),
pair[1]
);
return undefined;
}
);
nonMinimallyEncodedScriptNumbers.map((pair) => {
t.deepEqual(
parseBytesAsScriptNumber(hexToBin(pair[0]), true, 5),
parseBytesAsScriptNumber(hexToBin(pair[0]), {
maximumScriptNumberByteLength: 5,
}),
ScriptNumberError.requiresMinimal
);
t.deepEqual(
parseBytesAsScriptNumber(hexToBin(pair[0]), {
maximumScriptNumberByteLength: 5,
requireMinimalEncoding: true,
}),
ScriptNumberError.requiresMinimal
);
return undefined;
});
equivalentScriptNumbers.map((pair) => {
t.deepEqual(
parseBytesAsScriptNumber(hexToBin(pair[0]), false, 5),
parseBytesAsScriptNumber(hexToBin(pair[1]), true, 5)
parseBytesAsScriptNumber(hexToBin(pair[0]), {
maximumScriptNumberByteLength: 5,
requireMinimalEncoding: false,
}),
parseBytesAsScriptNumber(hexToBin(pair[1]), {
maximumScriptNumberByteLength: 5,
requireMinimalEncoding: true,
})
);
return undefined;
});
t.deepEqual(
parseBytesAsScriptNumber(hexToBin('abcdef1234'), true, 4),
parseBytesAsScriptNumber(hexToBin('abcdef1234')),
ScriptNumberError.outOfRange
);
t.deepEqual(
parseBytesAsScriptNumber(hexToBin('abcdef1234'), true, 5),
parseBytesAsScriptNumber(hexToBin('abcdef1234'), {
maximumScriptNumberByteLength: 5,
}),
BigInt(223656005035)
);
});
Expand Down
12 changes: 10 additions & 2 deletions src/lib/auth/instruction-sets/common/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -54,8 +54,16 @@ const normalMaximumScriptNumberByteLength = 4;
// eslint-disable-next-line complexity
export const parseBytesAsScriptNumber = (
bytes: Uint8Array,
requireMinimalEncoding = true,
maximumScriptNumberByteLength = normalMaximumScriptNumberByteLength
{
maximumScriptNumberByteLength = normalMaximumScriptNumberByteLength,
requireMinimalEncoding = true,
}: {
maximumScriptNumberByteLength?: number;
requireMinimalEncoding?: boolean;
} = {
maximumScriptNumberByteLength: normalMaximumScriptNumberByteLength,
requireMinimalEncoding: true,
}
): bigint | ScriptNumberError => {
if (bytes.length === 0) {
return BigInt(0);
Expand Down

0 comments on commit 8a73752

Please sign in to comment.