All files / src/utils typed-bytes.ts

100% Statements 86/86
100% Branches 11/11
100% Functions 31/31
100% Lines 86/86

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 22633x 33x 33x       33x   33x 33x   513x       521x   521x       15x       15x   15x           33x 33x   549x   549x 18x 18x   531x         529x 529x   529x       14x 14x   14x       14x       33x 33x   3686x       1x       33x 33x   56x       1x       33x 33x   1061x 8x 8x   1053x         8x       16x 16x   8x   8x         33x 33x   27x       33x 33x   36x       16x       3x       20x       33x 33x   12x       33x 33x   2167x       33x 33x   25x       10x       8x 8x 8x 8x                   8x       3x 3x   3x       33x 33x   62x       3x       33x   33x 33x 33x     104x       51x       24x       12x 2x     10x      
import { Binary, Elliptic } from 'cafe-utility'
import { Bytes } from './bytes'
import { convertCidToReference, convertReferenceToCid } from './cid'
 
// TODO: add JSdocs for each class
 
const ENCODER = new TextEncoder()
 
export class PrivateKey extends Bytes {
  static readonly LENGTH = 32
  constructor(bytes: Uint8Array | string | Bytes) {
    super(bytes, 32)
  }
 
  publicKey(): PublicKey {
    const [x, y] = Elliptic.privateKeyToPublicKey(Binary.uint256ToNumber(this.bytes, 'BE'))
 
    return new PublicKey(Binary.concatBytes(Binary.numberToUint256(x, 'BE'), Binary.numberToUint256(y, 'BE')))
  }
 
  sign(data: Uint8Array | string): Signature {
    const digest = Binary.concatBytes(
      ENCODER.encode(`\x19Ethereum Signed Message:\n32`),
      Binary.keccak256(data instanceof Uint8Array ? data : ENCODER.encode(data)),
    )
    const [r, s, v] = Elliptic.signMessage(digest, Binary.uint256ToNumber(this.bytes, 'BE'))
 
    return new Signature(
      Binary.concatBytes(Binary.numberToUint256(r, 'BE'), Binary.numberToUint256(s, 'BE'), new Uint8Array([Number(v)])),
    )
  }
}
 
export class PublicKey extends Bytes {
  static readonly LENGTH = 64
  constructor(bytes: Uint8Array | string | Bytes) {
    const b = new Bytes(bytes)
 
    if (b.length === 33) {
      const [x, y] = Elliptic.publicKeyFromCompressed(b.toUint8Array())
      super(Binary.concatBytes(Binary.numberToUint256(x, 'BE'), Binary.numberToUint256(y, 'BE')), 64)
    } else {
      super(bytes, 64)
    }
  }
 
  address(): EthAddress {
    const x = Binary.uint256ToNumber(this.bytes.slice(0, 32), 'BE')
    const y = Binary.uint256ToNumber(this.bytes.slice(32, 64), 'BE')
 
    return new EthAddress(Elliptic.publicKeyToAddress([x, y]))
  }
 
  toCompressedUint8Array(): Uint8Array {
    const x = Binary.uint256ToNumber(this.bytes.slice(0, 32), 'BE')
    const y = Binary.uint256ToNumber(this.bytes.slice(32, 64), 'BE')
 
    return Elliptic.compressPublicKey([x, y])
  }
 
  toCompressedHex(): string {
    return Binary.uint8ArrayToHex(this.toCompressedUint8Array())
  }
}
 
export class EthAddress extends Bytes {
  static readonly LENGTH = 20
  constructor(bytes: Uint8Array | string | Bytes) {
    super(bytes, 20)
  }
 
  public toChecksum(): string {
    return Elliptic.checksumEncode(this.bytes)
  }
}
 
export class Identifier extends Bytes {
  static readonly LENGTH = 32
  constructor(bytes: Uint8Array | string | Bytes) {
    super(bytes, 32)
  }
 
  static fromString(value: string): Identifier {
    return new Identifier(Binary.keccak256(ENCODER.encode(value)))
  }
}
 
export class Reference extends Bytes {
  static readonly LENGTH = 32
  constructor(bytes: Uint8Array | string | Bytes) {
    if (typeof bytes === 'string' && bytes.startsWith('bah5')) {
      const decoded = convertCidToReference(bytes)
      super(decoded.reference.bytes, 32)
    } else {
      super(bytes, [32, 64])
    }
  }
 
  toCid(type: 'feed' | 'manifest'): string {
    return convertReferenceToCid(this.bytes, type)
  }
 
  static isValid(value: string): boolean {
    try {
      new Reference(value)
 
      return true
    } catch {
      return false
    }
  }
}
 
export class TransactionId extends Bytes {
  static readonly LENGTH = 32
  constructor(bytes: Uint8Array | string | Bytes) {
    super(bytes, 32)
  }
}
 
export class Span extends Bytes {
  static readonly LENGTH = 8
  constructor(bytes: Uint8Array | string | Bytes) {
    super(bytes, 8)
  }
 
  static fromBigInt(number: bigint): Span {
    return new Span(Binary.numberToUint64(number, 'LE'))
  }
 
  toBigInt(): bigint {
    return Binary.uint64ToNumber(this.bytes, 'LE')
  }
 
  static fromSlice(bytes: Uint8Array, start: number): Span {
    return new Span(bytes.slice(start, start + Span.LENGTH))
  }
}
 
export class PeerAddress extends Bytes {
  static readonly LENGTH = 32
  constructor(bytes: Uint8Array | string | Bytes) {
    super(bytes, 32)
  }
}
 
export class BatchId extends Bytes {
  static readonly LENGTH = 32
  constructor(bytes: Uint8Array | string | Bytes) {
    super(bytes, 32)
  }
}
 
export class Signature extends Bytes {
  static readonly LENGTH = 65
  constructor(bytes: Uint8Array | string | Bytes) {
    super(bytes, 65)
  }
 
  static fromSlice(bytes: Uint8Array, start: number): Signature {
    return new Signature(bytes.slice(start, start + Signature.LENGTH))
  }
 
  recoverPublicKey(digest: Uint8Array | string): PublicKey {
    const r = Binary.uint256ToNumber(this.bytes.slice(0, 32), 'BE')
    const s = Binary.uint256ToNumber(this.bytes.slice(32, 64), 'BE')
    const v = BigInt(this.bytes[64]) as 27n | 28n
    const [x, y] = Elliptic.recoverPublicKey(
      Binary.concatBytes(
        ENCODER.encode(`\x19Ethereum Signed Message:\n32`),
        Binary.keccak256(digest instanceof Uint8Array ? digest : ENCODER.encode(digest)),
      ),
      r,
      s,
      v,
    )
 
    return new PublicKey(Binary.concatBytes(Binary.numberToUint256(x, 'BE'), Binary.numberToUint256(y, 'BE')))
  }
 
  isValid(digest: Uint8Array | string, expectedAddress: EthAddress | Uint8Array | string): boolean {
    const publicKey = this.recoverPublicKey(digest)
    const address = publicKey.address()
 
    return address.equals(expectedAddress)
  }
}
 
export class Topic extends Bytes {
  static readonly LENGTH = 32
  constructor(bytes: Uint8Array | string | Bytes) {
    super(bytes, 32)
  }
 
  static fromString(value: string): Topic {
    return new Topic(Binary.keccak256(ENCODER.encode(value)))
  }
}
 
const MAX_UINT64 = new Uint8Array(8).fill(0xff, 0, 8)
 
export class FeedIndex extends Bytes {
  static readonly LENGTH = 8
  static readonly MINUS_ONE = new FeedIndex(MAX_UINT64)
 
  constructor(bytes: Uint8Array | string | Bytes) {
    super(bytes, 8)
  }
 
  static fromBigInt(number: bigint): FeedIndex {
    return new FeedIndex(Binary.numberToUint64(number, 'BE'))
  }
 
  toBigInt(): bigint {
    return Binary.uint64ToNumber(this.bytes, 'BE')
  }
 
  next(): FeedIndex {
    if (Binary.equals(this.bytes, MAX_UINT64)) {
      return FeedIndex.fromBigInt(0n)
    }
 
    return FeedIndex.fromBigInt(this.toBigInt() + 1n)
  }
}