@ethersphere/bee-js
Version:
Javascript client for Bee
187 lines (186 loc) • 6.88 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.FeedIndex = exports.Topic = exports.Signature = exports.BatchId = exports.PeerAddress = exports.Span = exports.TransactionId = exports.Reference = exports.Identifier = exports.EthAddress = exports.PublicKey = exports.PrivateKey = void 0;
const cafe_utility_1 = require("cafe-utility");
const bytes_1 = require("./bytes");
const cid_1 = require("./cid");
// TODO: add JSdocs for each class
const ENCODER = new TextEncoder();
class PrivateKey extends bytes_1.Bytes {
constructor(bytes) {
super(bytes, 32);
}
publicKey() {
const [x, y] = cafe_utility_1.Elliptic.privateKeyToPublicKey(cafe_utility_1.Binary.uint256ToNumber(this.bytes, 'BE'));
return new PublicKey(cafe_utility_1.Binary.concatBytes(cafe_utility_1.Binary.numberToUint256(x, 'BE'), cafe_utility_1.Binary.numberToUint256(y, 'BE')));
}
sign(data) {
const digest = cafe_utility_1.Binary.concatBytes(ENCODER.encode(`\x19Ethereum Signed Message:\n32`), cafe_utility_1.Binary.keccak256(data instanceof Uint8Array ? data : ENCODER.encode(data)));
const [r, s, v] = cafe_utility_1.Elliptic.signMessage(digest, cafe_utility_1.Binary.uint256ToNumber(this.bytes, 'BE'));
return new Signature(cafe_utility_1.Binary.concatBytes(cafe_utility_1.Binary.numberToUint256(r, 'BE'), cafe_utility_1.Binary.numberToUint256(s, 'BE'), new Uint8Array([Number(v)])));
}
}
exports.PrivateKey = PrivateKey;
PrivateKey.LENGTH = 32;
class PublicKey extends bytes_1.Bytes {
constructor(bytes) {
const b = new bytes_1.Bytes(bytes);
if (b.length === 33) {
const [x, y] = cafe_utility_1.Elliptic.publicKeyFromCompressed(b.toUint8Array());
super(cafe_utility_1.Binary.concatBytes(cafe_utility_1.Binary.numberToUint256(x, 'BE'), cafe_utility_1.Binary.numberToUint256(y, 'BE')), 64);
}
else {
super(bytes, 64);
}
}
address() {
const x = cafe_utility_1.Binary.uint256ToNumber(this.bytes.slice(0, 32), 'BE');
const y = cafe_utility_1.Binary.uint256ToNumber(this.bytes.slice(32, 64), 'BE');
return new EthAddress(cafe_utility_1.Elliptic.publicKeyToAddress([x, y]));
}
toCompressedUint8Array() {
const x = cafe_utility_1.Binary.uint256ToNumber(this.bytes.slice(0, 32), 'BE');
const y = cafe_utility_1.Binary.uint256ToNumber(this.bytes.slice(32, 64), 'BE');
return cafe_utility_1.Elliptic.compressPublicKey([x, y]);
}
toCompressedHex() {
return cafe_utility_1.Binary.uint8ArrayToHex(this.toCompressedUint8Array());
}
}
exports.PublicKey = PublicKey;
PublicKey.LENGTH = 64;
class EthAddress extends bytes_1.Bytes {
constructor(bytes) {
super(bytes, 20);
}
toChecksum() {
return cafe_utility_1.Elliptic.checksumEncode(this.bytes);
}
}
exports.EthAddress = EthAddress;
EthAddress.LENGTH = 20;
class Identifier extends bytes_1.Bytes {
constructor(bytes) {
super(bytes, 32);
}
static fromString(value) {
return new Identifier(cafe_utility_1.Binary.keccak256(ENCODER.encode(value)));
}
}
exports.Identifier = Identifier;
Identifier.LENGTH = 32;
class Reference extends bytes_1.Bytes {
constructor(bytes) {
if (typeof bytes === 'string' && bytes.startsWith('bah5')) {
const decoded = (0, cid_1.convertCidToReference)(bytes);
super(decoded.reference.bytes, 32);
}
else {
super(bytes, [32, 64]);
}
}
toCid(type) {
return (0, cid_1.convertReferenceToCid)(this.bytes, type);
}
static isValid(value) {
try {
new Reference(value);
return true;
}
catch {
return false;
}
}
}
exports.Reference = Reference;
Reference.LENGTH = 32;
class TransactionId extends bytes_1.Bytes {
constructor(bytes) {
super(bytes, 32);
}
}
exports.TransactionId = TransactionId;
TransactionId.LENGTH = 32;
class Span extends bytes_1.Bytes {
constructor(bytes) {
super(bytes, 8);
}
static fromBigInt(number) {
return new Span(cafe_utility_1.Binary.numberToUint64(number, 'LE'));
}
toBigInt() {
return cafe_utility_1.Binary.uint64ToNumber(this.bytes, 'LE');
}
static fromSlice(bytes, start) {
return new Span(bytes.slice(start, start + Span.LENGTH));
}
}
exports.Span = Span;
Span.LENGTH = 8;
class PeerAddress extends bytes_1.Bytes {
constructor(bytes) {
super(bytes, 32);
}
}
exports.PeerAddress = PeerAddress;
PeerAddress.LENGTH = 32;
class BatchId extends bytes_1.Bytes {
constructor(bytes) {
super(bytes, 32);
}
}
exports.BatchId = BatchId;
BatchId.LENGTH = 32;
class Signature extends bytes_1.Bytes {
constructor(bytes) {
super(bytes, 65);
}
static fromSlice(bytes, start) {
return new Signature(bytes.slice(start, start + Signature.LENGTH));
}
recoverPublicKey(digest) {
const r = cafe_utility_1.Binary.uint256ToNumber(this.bytes.slice(0, 32), 'BE');
const s = cafe_utility_1.Binary.uint256ToNumber(this.bytes.slice(32, 64), 'BE');
const v = BigInt(this.bytes[64]);
const [x, y] = cafe_utility_1.Elliptic.recoverPublicKey(cafe_utility_1.Binary.concatBytes(ENCODER.encode(`\x19Ethereum Signed Message:\n32`), cafe_utility_1.Binary.keccak256(digest instanceof Uint8Array ? digest : ENCODER.encode(digest))), r, s, v);
return new PublicKey(cafe_utility_1.Binary.concatBytes(cafe_utility_1.Binary.numberToUint256(x, 'BE'), cafe_utility_1.Binary.numberToUint256(y, 'BE')));
}
isValid(digest, expectedAddress) {
const publicKey = this.recoverPublicKey(digest);
const address = publicKey.address();
return address.equals(expectedAddress);
}
}
exports.Signature = Signature;
Signature.LENGTH = 65;
class Topic extends bytes_1.Bytes {
constructor(bytes) {
super(bytes, 32);
}
static fromString(value) {
return new Topic(cafe_utility_1.Binary.keccak256(ENCODER.encode(value)));
}
}
exports.Topic = Topic;
Topic.LENGTH = 32;
const MAX_UINT64 = new Uint8Array(8).fill(0xff, 0, 8);
class FeedIndex extends bytes_1.Bytes {
constructor(bytes) {
super(bytes, 8);
}
static fromBigInt(number) {
return new FeedIndex(cafe_utility_1.Binary.numberToUint64(number, 'BE'));
}
toBigInt() {
return cafe_utility_1.Binary.uint64ToNumber(this.bytes, 'BE');
}
next() {
if (cafe_utility_1.Binary.equals(this.bytes, MAX_UINT64)) {
return FeedIndex.fromBigInt(0n);
}
return FeedIndex.fromBigInt(this.toBigInt() + 1n);
}
}
exports.FeedIndex = FeedIndex;
FeedIndex.LENGTH = 8;
FeedIndex.MINUS_ONE = new FeedIndex(MAX_UINT64);