-
Notifications
You must be signed in to change notification settings - Fork 59
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(tx-builder): validate address and enum fields in runtime
- Loading branch information
Showing
6 changed files
with
116 additions
and
95 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,21 +1,32 @@ | ||
import { ArgumentError } from '../../../utils/errors'; | ||
import { isItemOfArray } from '../../../utils/other'; | ||
|
||
export default function genEnumerationField<Enum extends number>(): { | ||
serialize: (value: Enum) => Buffer; | ||
deserialize: (value: Buffer) => Enum; | ||
export default function genEnumerationField< | ||
Enum extends { [key: string]: number | string }, | ||
>(enm: Enum): { | ||
serialize: (value: Enum[keyof Enum]) => Buffer; | ||
deserialize: (value: Buffer) => Enum[keyof Enum]; | ||
} { | ||
const values = Object.values(enm).filter((v) => typeof v === 'number'); | ||
return { | ||
serialize(value) { | ||
if (typeof value !== 'number') throw new ArgumentError('value', 'to be a number', value); | ||
if (value > 0xff) throw new ArgumentError('value', 'to be less than 256', value); | ||
if (!isItemOfArray(value, values)) { | ||
throw new ArgumentError('value', 'to be a value of Enum', value); | ||
} | ||
return Buffer.from([value]); | ||
}, | ||
|
||
deserialize(buffer) { | ||
if (buffer.length !== 1) { | ||
throw new ArgumentError('buffer', 'to have single element', buffer.length); | ||
} | ||
return buffer[0] as Enum; | ||
const value = buffer[0]; | ||
if (!isItemOfArray(value, values)) { | ||
throw new ArgumentError('value', 'to be a value of Enum', value); | ||
} | ||
return value as Enum[keyof Enum]; | ||
}, | ||
}; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.