feat: add API for decoding stake instructions

This commit is contained in:
Justin Starry
2020-03-02 23:58:10 +08:00
committed by Michael Vines
parent 01e65d2070
commit aba7e14f3a
6 changed files with 660 additions and 426 deletions

View File

@@ -25,3 +25,23 @@ export function encodeData(type: InstructionType, fields: Object): Buffer {
type.layout.encode(layoutFields, data);
return data;
}
/**
* Decode instruction data buffer using an InstructionType
*/
export function decodeData(type: InstructionType, buffer: Buffer): Object {
let data;
try {
data = type.layout.decode(buffer);
} catch (err) {
throw new Error('invalid instruction; ' + err);
}
if (data.instruction !== type.index) {
throw new Error(
`invalid instruction; instruction index mismatch ${data.instruction} != ${type.index}`,
);
}
return data;
}