Rename encodeInt to encodeUvarint
This commit is contained in:
parent
1e50f89225
commit
c6112913b9
@ -1,6 +1,13 @@
|
||||
import { ReadonlyDate } from "readonly-date";
|
||||
|
||||
import { encodeBlockId, encodeBytes, encodeInt, encodeString, encodeTime, encodeVersion } from "./encodings";
|
||||
import {
|
||||
encodeBlockId,
|
||||
encodeBytes,
|
||||
encodeString,
|
||||
encodeTime,
|
||||
encodeUvarint,
|
||||
encodeVersion,
|
||||
} from "./encodings";
|
||||
|
||||
describe("encodings", () => {
|
||||
describe("encodeString", () => {
|
||||
@ -13,14 +20,14 @@ describe("encodings", () => {
|
||||
});
|
||||
});
|
||||
|
||||
describe("encodeInt", () => {
|
||||
describe("encodeUvarint", () => {
|
||||
it("works", () => {
|
||||
expect(encodeInt(0)).toEqual(Uint8Array.from([0]));
|
||||
expect(encodeInt(1)).toEqual(Uint8Array.from([1]));
|
||||
expect(encodeInt(127)).toEqual(Uint8Array.from([127]));
|
||||
expect(encodeInt(128)).toEqual(Uint8Array.from([128, 1]));
|
||||
expect(encodeInt(255)).toEqual(Uint8Array.from([255, 1]));
|
||||
expect(encodeInt(256)).toEqual(Uint8Array.from([128, 2]));
|
||||
expect(encodeUvarint(0)).toEqual(Uint8Array.from([0]));
|
||||
expect(encodeUvarint(1)).toEqual(Uint8Array.from([1]));
|
||||
expect(encodeUvarint(127)).toEqual(Uint8Array.from([127]));
|
||||
expect(encodeUvarint(128)).toEqual(Uint8Array.from([128, 1]));
|
||||
expect(encodeUvarint(255)).toEqual(Uint8Array.from([255, 1]));
|
||||
expect(encodeUvarint(256)).toEqual(Uint8Array.from([128, 2]));
|
||||
});
|
||||
});
|
||||
|
||||
|
||||
@ -161,18 +161,21 @@ export function encodeString(s: string): Uint8Array {
|
||||
}
|
||||
|
||||
// See https://github.com/tendermint/go-amino/blob/v0.15.0/encoder.go#L79-L87
|
||||
export function encodeInt(n: number): Uint8Array {
|
||||
// eslint-disable-next-line no-bitwise
|
||||
return n >= 0x80 ? Uint8Array.from([(n & 0xff) | 0x80, ...encodeInt(n >> 7)]) : Uint8Array.from([n & 0xff]);
|
||||
export function encodeUvarint(n: number): Uint8Array {
|
||||
return n >= 0x80
|
||||
? // eslint-disable-next-line no-bitwise
|
||||
Uint8Array.from([(n & 0xff) | 0x80, ...encodeUvarint(n >> 7)])
|
||||
: // eslint-disable-next-line no-bitwise
|
||||
Uint8Array.from([n & 0xff]);
|
||||
}
|
||||
|
||||
// See https://github.com/tendermint/go-amino/blob/v0.15.0/encoder.go#L134-L178
|
||||
export function encodeTime(time: ReadonlyDateWithNanoseconds): Uint8Array {
|
||||
const milliseconds = time.getTime();
|
||||
const seconds = Math.floor(milliseconds / 1000);
|
||||
const secondsArray = seconds ? [0x08, ...encodeInt(seconds)] : new Uint8Array();
|
||||
const secondsArray = seconds ? [0x08, ...encodeUvarint(seconds)] : new Uint8Array();
|
||||
const nanoseconds = (time.nanoseconds || 0) + (milliseconds % 1000) * 1e6;
|
||||
const nanosecondsArray = nanoseconds ? [0x10, ...encodeInt(nanoseconds)] : new Uint8Array();
|
||||
const nanosecondsArray = nanoseconds ? [0x10, ...encodeUvarint(nanoseconds)] : new Uint8Array();
|
||||
return Uint8Array.from([...secondsArray, ...nanosecondsArray]);
|
||||
}
|
||||
|
||||
@ -184,8 +187,10 @@ export function encodeBytes(bytes: Uint8Array): Uint8Array {
|
||||
}
|
||||
|
||||
export function encodeVersion(version: Version): Uint8Array {
|
||||
const blockArray = version.block ? Uint8Array.from([0x08, ...encodeInt(version.block)]) : new Uint8Array();
|
||||
const appArray = version.app ? Uint8Array.from([0x10, ...encodeInt(version.app)]) : new Uint8Array();
|
||||
const blockArray = version.block
|
||||
? Uint8Array.from([0x08, ...encodeUvarint(version.block)])
|
||||
: new Uint8Array();
|
||||
const appArray = version.app ? Uint8Array.from([0x10, ...encodeUvarint(version.app)]) : new Uint8Array();
|
||||
return Uint8Array.from([...blockArray, ...appArray]);
|
||||
}
|
||||
|
||||
|
||||
@ -1,6 +1,13 @@
|
||||
import { Sha256, sha256 } from "@cosmjs/crypto";
|
||||
|
||||
import { encodeBlockId, encodeBytes, encodeInt, encodeString, encodeTime, encodeVersion } from "./encodings";
|
||||
import {
|
||||
encodeBlockId,
|
||||
encodeBytes,
|
||||
encodeString,
|
||||
encodeTime,
|
||||
encodeUvarint,
|
||||
encodeVersion,
|
||||
} from "./encodings";
|
||||
import { Header } from "./responses";
|
||||
|
||||
// hash is sha256
|
||||
@ -55,7 +62,7 @@ export function hashBlock(header: Header): Uint8Array {
|
||||
const encodedFields: readonly Uint8Array[] = [
|
||||
encodeVersion(header.version),
|
||||
encodeString(header.chainId),
|
||||
encodeInt(header.height),
|
||||
encodeUvarint(header.height),
|
||||
encodeTime(header.time),
|
||||
encodeBlockId(header.lastBlockId),
|
||||
|
||||
|
||||
@ -1,6 +1,13 @@
|
||||
import { ReadonlyDate } from "readonly-date";
|
||||
|
||||
import { encodeBlockId, encodeBytes, encodeInt, encodeString, encodeTime, encodeVersion } from "./encodings";
|
||||
import {
|
||||
encodeBlockId,
|
||||
encodeBytes,
|
||||
encodeString,
|
||||
encodeTime,
|
||||
encodeUvarint,
|
||||
encodeVersion,
|
||||
} from "./encodings";
|
||||
|
||||
describe("encodings", () => {
|
||||
describe("encodeString", () => {
|
||||
@ -13,14 +20,14 @@ describe("encodings", () => {
|
||||
});
|
||||
});
|
||||
|
||||
describe("encodeInt", () => {
|
||||
describe("encodeUvarint", () => {
|
||||
it("works", () => {
|
||||
expect(encodeInt(0)).toEqual(Uint8Array.from([0]));
|
||||
expect(encodeInt(1)).toEqual(Uint8Array.from([1]));
|
||||
expect(encodeInt(127)).toEqual(Uint8Array.from([127]));
|
||||
expect(encodeInt(128)).toEqual(Uint8Array.from([128, 1]));
|
||||
expect(encodeInt(255)).toEqual(Uint8Array.from([255, 1]));
|
||||
expect(encodeInt(256)).toEqual(Uint8Array.from([128, 2]));
|
||||
expect(encodeUvarint(0)).toEqual(Uint8Array.from([0]));
|
||||
expect(encodeUvarint(1)).toEqual(Uint8Array.from([1]));
|
||||
expect(encodeUvarint(127)).toEqual(Uint8Array.from([127]));
|
||||
expect(encodeUvarint(128)).toEqual(Uint8Array.from([128, 1]));
|
||||
expect(encodeUvarint(255)).toEqual(Uint8Array.from([255, 1]));
|
||||
expect(encodeUvarint(256)).toEqual(Uint8Array.from([128, 2]));
|
||||
});
|
||||
});
|
||||
|
||||
|
||||
@ -161,18 +161,21 @@ export function encodeString(s: string): Uint8Array {
|
||||
}
|
||||
|
||||
// See https://github.com/tendermint/go-amino/blob/v0.15.0/encoder.go#L79-L87
|
||||
export function encodeInt(n: number): Uint8Array {
|
||||
// eslint-disable-next-line no-bitwise
|
||||
return n >= 0x80 ? Uint8Array.from([(n & 0xff) | 0x80, ...encodeInt(n >> 7)]) : Uint8Array.from([n & 0xff]);
|
||||
export function encodeUvarint(n: number): Uint8Array {
|
||||
return n >= 0x80
|
||||
? // eslint-disable-next-line no-bitwise
|
||||
Uint8Array.from([(n & 0xff) | 0x80, ...encodeUvarint(n >> 7)])
|
||||
: // eslint-disable-next-line no-bitwise
|
||||
Uint8Array.from([n & 0xff]);
|
||||
}
|
||||
|
||||
// See https://github.com/tendermint/go-amino/blob/v0.15.0/encoder.go#L134-L178
|
||||
export function encodeTime(time: ReadonlyDateWithNanoseconds): Uint8Array {
|
||||
const milliseconds = time.getTime();
|
||||
const seconds = Math.floor(milliseconds / 1000);
|
||||
const secondsArray = seconds ? [0x08, ...encodeInt(seconds)] : new Uint8Array();
|
||||
const secondsArray = seconds ? [0x08, ...encodeUvarint(seconds)] : new Uint8Array();
|
||||
const nanoseconds = (time.nanoseconds || 0) + (milliseconds % 1000) * 1e6;
|
||||
const nanosecondsArray = nanoseconds ? [0x10, ...encodeInt(nanoseconds)] : new Uint8Array();
|
||||
const nanosecondsArray = nanoseconds ? [0x10, ...encodeUvarint(nanoseconds)] : new Uint8Array();
|
||||
return Uint8Array.from([...secondsArray, ...nanosecondsArray]);
|
||||
}
|
||||
|
||||
@ -184,8 +187,10 @@ export function encodeBytes(bytes: Uint8Array): Uint8Array {
|
||||
}
|
||||
|
||||
export function encodeVersion(version: Version): Uint8Array {
|
||||
const blockArray = version.block ? Uint8Array.from([0x08, ...encodeInt(version.block)]) : new Uint8Array();
|
||||
const appArray = version.app ? Uint8Array.from([0x10, ...encodeInt(version.app)]) : new Uint8Array();
|
||||
const blockArray = version.block
|
||||
? Uint8Array.from([0x08, ...encodeUvarint(version.block)])
|
||||
: new Uint8Array();
|
||||
const appArray = version.app ? Uint8Array.from([0x10, ...encodeUvarint(version.app)]) : new Uint8Array();
|
||||
return Uint8Array.from([...blockArray, ...appArray]);
|
||||
}
|
||||
|
||||
|
||||
@ -1,6 +1,13 @@
|
||||
import { Sha256, sha256 } from "@cosmjs/crypto";
|
||||
|
||||
import { encodeBlockId, encodeBytes, encodeInt, encodeString, encodeTime, encodeVersion } from "./encodings";
|
||||
import {
|
||||
encodeBlockId,
|
||||
encodeBytes,
|
||||
encodeString,
|
||||
encodeTime,
|
||||
encodeUvarint,
|
||||
encodeVersion,
|
||||
} from "./encodings";
|
||||
import { Header } from "./responses";
|
||||
|
||||
// hash is sha256
|
||||
@ -55,7 +62,7 @@ export function hashBlock(header: Header): Uint8Array {
|
||||
const encodedFields: readonly Uint8Array[] = [
|
||||
encodeVersion(header.version),
|
||||
encodeString(header.chainId),
|
||||
encodeInt(header.height),
|
||||
encodeUvarint(header.height),
|
||||
encodeTime(header.time),
|
||||
encodeBlockId(header.lastBlockId),
|
||||
|
||||
|
||||
Loading…
Reference in New Issue
Block a user