Skip to content

Commit

Permalink
chore: allow odd-length hex strings that certain libs output
Browse files Browse the repository at this point in the history
  • Loading branch information
zone117x authored and reedrosenbluth committed Jul 26, 2021
1 parent a59579a commit 912bc2f
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 2 deletions.
8 changes: 6 additions & 2 deletions packages/transactions/src/clarity/types/intCV.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,13 @@ function valueToBN(value: unknown, signed: boolean): BigNum {
return new BigNum(value);
}
if (typeof value === 'string') {
// If hex string then convert to buffer then fall through to the buffer condition
if (value.toLowerCase().startsWith('0x')) {
// Convert to buffer then fall through to the buffer condition
value = Buffer.from(value.slice(2), 'hex');
// Trim '0x' hex-prefix
let hex = value.slice(2);
// Allow odd-length strings like `0xf` -- some libs output these, or even just `0x${num.toString(16)}`
hex = hex.padStart(hex.length + (hex.length % 2), '0');
value = Buffer.from(hex, 'hex');
} else {
return new BigNum(value);
}
Expand Down
2 changes: 2 additions & 0 deletions packages/transactions/tests/clarity.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -236,6 +236,8 @@ describe('Clarity Types', () => {
['10', '10', '0x0000000000000000000000000000000a'],
['0x0a', '10', '0x0000000000000000000000000000000a'],
['0x000a', '10', '0x0000000000000000000000000000000a'],
['0xa', '10', '0x0000000000000000000000000000000a'], // hex string with odd padding
['0x00a', '10', '0x0000000000000000000000000000000a'], // hex string with odd padding
[new BN(10), '10', '0x0000000000000000000000000000000a'],
[Buffer.from([0x0a]), '10', '0x0000000000000000000000000000000a'],
[Buffer.from([0x00, 0x0a]), '10', '0x0000000000000000000000000000000a'],
Expand Down

0 comments on commit 912bc2f

Please sign in to comment.