mirror of
https://github.com/pezkuwichain/pezkuwi-api.git
synced 2026-04-22 03:17:56 +00:00
31467f90d4
- @pezkuwi/papi-utils (rebrand of @polkadot-api/utils) - @pezkuwi/bizinikiwi-bindings (rebrand of @polkadot-api/substrate-bindings) - @pezkuwi/metadata-builders (rebrand of @polkadot-api/metadata-builders) - @pezkuwi/merkleize-metadata (rebrand of @polkadot-api/merkleize-metadata) All @polkadot-api references replaced with @pezkuwi equivalents.
38 lines
1.2 KiB
JavaScript
38 lines
1.2 KiB
JavaScript
import { createCodec, createDecoder, Bytes } from 'scale-ts';
|
|
import { compactNumber } from './compact.mjs';
|
|
import { mergeUint8 } from '@pezkuwi/papi-utils';
|
|
|
|
const dec = (isLsb = true) => createDecoder((data) => {
|
|
const bitsLen = compactNumber.dec(data);
|
|
const bytesLen = Math.ceil(bitsLen / 8);
|
|
const bytes = Bytes(bytesLen).dec(data);
|
|
const result = new Array(bitsLen);
|
|
let resultIdx = 0;
|
|
bytes.forEach((val) => {
|
|
for (let i = 0; i < 8 && resultIdx < bitsLen; i++) {
|
|
const actualIdx = isLsb ? i : 7 - i;
|
|
result[resultIdx++] = val >> actualIdx & 1;
|
|
}
|
|
});
|
|
return result;
|
|
});
|
|
const enc = (isLsb = true) => (input) => {
|
|
const lenEncoded = compactNumber.enc(input.length);
|
|
const nBytes = Math.ceil(input.length / 8);
|
|
const bytes = new Uint8Array(nBytes);
|
|
for (let byteIdx = 0; byteIdx < nBytes; byteIdx++) {
|
|
let inputIdx = byteIdx * 8;
|
|
let byte = 0;
|
|
for (let i = 0; i < 8 && inputIdx < input.length; i++, inputIdx++)
|
|
byte |= input[inputIdx] << (isLsb ? i : 7 - i);
|
|
bytes[byteIdx] = byte;
|
|
}
|
|
return mergeUint8([lenEncoded, bytes]);
|
|
};
|
|
const BitSeq = (isLsb) => createCodec(enc(isLsb), dec(isLsb));
|
|
BitSeq.enc = enc;
|
|
BitSeq.dec = dec;
|
|
|
|
export { BitSeq };
|
|
//# sourceMappingURL=BitSeq.mjs.map
|