@flarenetwork/flarejs
Version:
Flare Platform JS Library
39 lines (33 loc) • 921 B
text/typescript
import { concatBytes, hexToBuffer, padLeft } from '../../utils/buffer';
import { serializable } from '../common/types';
import { Primitives } from './primatives';
import { Short } from './short';
import { TypeSymbols } from '../constants';
()
export class Stringpr extends Primitives {
_type = TypeSymbols.StringPr;
constructor(private readonly string: string) {
super();
}
static fromBytes(buf: Uint8Array): [Stringpr, Uint8Array] {
const [length, remaining] = Short.fromBytes(buf);
return [
new Stringpr(
new TextDecoder().decode(remaining.slice(0, length.value())),
),
remaining.slice(length.value()),
];
}
toJSON() {
return this.string;
}
toBytes() {
return concatBytes(
padLeft(hexToBuffer(this.string.length.toString(16)), 2),
new TextEncoder().encode(this.string),
);
}
value() {
return this.string;
}
}