feat: add API for decoding stake instructions
This commit is contained in:
committed by
Michael Vines
parent
01e65d2070
commit
aba7e14f3a
@@ -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;
|
||||
}
|
||||
|
Reference in New Issue
Block a user