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 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 | 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 75104x 75104x 75104x 1x 1x 1x 1x 1x 1x 1x 1x 217013x 217013x 217013x 217013x 217013x 217013x 217013x 217013x 217013x 217013x 217013x 217013x 217013x 217013x 217013x 217013x 217013x 217013x 166341x 166341x 166341x 217013x 217013x 217013x 217013x 217013x 217013x 217013x 217013x 217013x 217013x 217013x 217013x 217013x 217013x 217013x 217013x 77578x 77578x 77578x 217013x 217013x 217013x 217013x 217013x 217013x 217013x 217013x 217013x 217013x 217013x 217013x 217013x 217013x 217013x 217013x 217013x 217013x 217013x 217013x 217013x 217013x 217013x 217013x 217013x 56448x 56448x 11648x 11648x 217013x 217013x 217013x 217013x 217013x 217013x 217013x 203974x 203974x 217013x 217013x 217013x 217013x 217013x 217013x 217013x 217013x 217013x 217013x 217013x 217013x 217013x 217013x 217013x 217013x 1x 1x 217013x 217013x 217013x 217013x 217013x 217013x 217013x 217013x 217013x 217013x 217013x 217013x 217013x 217013x 217013x 217013x 217013x 217013x 217013x 217013x 77578x 217013x 217013x 217013x 217013x 217013x 179462x 179462x 179462x 179462x 217013x 217013x 217013x 217013x 217013x 356x 356x 356x 356x 356x 356x 356x 217013x 217013x 217013x 217013x 217013x 217013x 217013x 217013x 217013x 217013x 217013x 217013x 217013x 217013x 217013x 217013x 11648x 11648x 11648x 11648x 11648x 11648x 11648x 11648x 11648x 11648x 217013x 217013x 217013x 217013x 217013x 217013x 217013x 217013x 217013x 217013x 217013x 217013x 217013x 217013x 217013x 217013x 13312x 13312x 217013x 217013x 217013x 217013x 217013x 217013x 217013x 217013x 217013x 217013x 217013x 217013x 217013x 217013x 217013x 217013x 217013x 217013x 217013x 217013x 217013x 217013x 217013x 29468x 29468x 29468x 29468x 3352x 3352x 3352x 3352x 29468x 29468x 217013x 217013x 217013x 217013x 217013x 217013x 217013x 217013x 217013x 217013x 217014x 112655x 217014x 217014x 217014x 217014x 217014x 217014x 217014x 37552x 37552x 37552x 37552x 37552x 37552x 37552x 37552x 37552x 37552x 37552x 37552x 37552x 37552x 29468x 29468x 29468x 29468x 29468x 29468x 8084x 37552x 8084x 37552x 8083x 8083x 8083x 8083x 1x 1x 37552x 37552x 37552x 217014x 37551x 37551x 217014x 217014x 217014x 217014x 217014x 217013x |
import { ZeroHash } from "../constants/index.js";
import {
concat, dataLength, getBigInt, getBytes, getNumber, hexlify,
toBeArray, isHexString, zeroPadValue,
assertArgument, assertPrivate
} from "../utils/index.js";
import type {
BigNumberish, BytesLike, Numeric
} from "../utils/index.js";
// Constants
const BN_0 = BigInt(0);
const BN_1 = BigInt(1);
const BN_2 = BigInt(2);
const BN_27 = BigInt(27);
const BN_28 = BigInt(28);
const BN_35 = BigInt(35);
const BN_N = BigInt("0xfffffffffffffffffffffffffffffffebaaedce6af48a03bbfd25e8cd0364141");
const BN_N_2 = BN_N / BN_2; // Must be integer (floor) division; do NOT shifts
const inspect = Symbol.for("nodejs.util.inspect.custom");
const _guard = { };
// @TODO: Allow Uint8Array
/**
* A SignatureLike
*
* @_docloc: api/crypto:Signing
*/
export type SignatureLike = Signature | string | {
r: string;
s: string;
v: BigNumberish;
yParity?: 0 | 1;
yParityAndS?: string;
} | {
r: string;
yParityAndS: string;
yParity?: 0 | 1;
s?: string;
v?: number;
} | {
r: string;
s: string;
yParity: 0 | 1;
v?: BigNumberish;
yParityAndS?: string;
};
function toUint256(value: BigNumberish): string {
return zeroPadValue(toBeArray(value), 32);
}
/**
* A Signature @TODO
*
*
* @_docloc: api/crypto:Signing
*/
export class Signature {
#r: string;
#s: string;
#v: 27 | 28;
#networkV: null | bigint;
/**
* The ``r`` value for a signature.
*
* This represents the ``x`` coordinate of a "reference" or
* challenge point, from which the ``y`` can be computed.
*/
get r(): string { return this.#r; }
set r(value: BytesLike) {
assertArgument(dataLength(value) === 32, "invalid r", "value", value);
this.#r = hexlify(value);
}
/**
* The ``s`` value for a signature.
*/
get s(): string {
assertArgument(parseInt(this.#s.substring(0, 3)) < 8, "non-canonical s; use ._s", "s", this.#s);
return this.#s;
}
set s(_value: BytesLike) {
assertArgument(dataLength(_value) === 32, "invalid s", "value", _value);
this.#s = hexlify(_value);
}
/**
* Return the s value, unchecked for EIP-2 compliance.
*
* This should generally not be used and is for situations where
* a non-canonical S value might be relevant, such as Frontier blocks
* that were mined prior to EIP-2 or invalid Authorization List
* signatures.
*/
get _s(): string { return this.#s; }
/**
* Returns true if the Signature is valid for [[link-eip-2]] signatures.
*/
isValid(): boolean {
const s = BigInt(this.#s);
return (s <= BN_N_2);
}
/**
* The ``v`` value for a signature.
*
* Since a given ``x`` value for ``r`` has two possible values for
* its correspondin ``y``, the ``v`` indicates which of the two ``y``
* values to use.
*
* It is normalized to the values ``27`` or ``28`` for legacy
* purposes.
*/
get v(): 27 | 28 { return this.#v; }
set v(value: BigNumberish) {
const v = getNumber(value, "value");
assertArgument(v === 27 || v === 28, "invalid v", "v", value);
this.#v = v;
}
/**
* The EIP-155 ``v`` for legacy transactions. For non-legacy
* transactions, this value is ``null``.
*/
get networkV(): null | bigint { return this.#networkV; }
/**
* The chain ID for EIP-155 legacy transactions. For non-legacy
* transactions, this value is ``null``.
*/
get legacyChainId(): null | bigint {
const v = this.networkV;
if (v == null) { return null; }
return Signature.getChainId(v);
}
/**
* The ``yParity`` for the signature.
*
* See ``v`` for more details on how this value is used.
*/
get yParity(): 0 | 1 {
return (this.v === 27) ? 0: 1;
}
/**
* The [[link-eip-2098]] compact representation of the ``yParity``
* and ``s`` compacted into a single ``bytes32``.
*/
get yParityAndS(): string {
// The EIP-2098 compact representation
const yParityAndS = getBytes(this.s);
if (this.yParity) { yParityAndS[0] |= 0x80; }
return hexlify(yParityAndS);
}
/**
* The [[link-eip-2098]] compact representation.
*/
get compactSerialized(): string {
return concat([ this.r, this.yParityAndS ]);
}
/**
* The serialized representation.
*/
get serialized(): string {
return concat([ this.r, this.s, (this.yParity ? "0x1c": "0x1b") ]);
}
/**
* @private
*/
constructor(guard: any, r: string, s: string, v: 27 | 28) {
assertPrivate(guard, _guard, "Signature");
this.#r = r;
this.#s = s;
this.#v = v;
this.#networkV = null;
}
/**
* Returns the canonical signature.
*
* This is only necessary when dealing with legacy transaction which
* did not enforce canonical S values (i.e. [[link-eip-2]]. Most
* developers should never require this.
*/
getCanonical(): Signature {
if (this.isValid()) { return this; }
// Compute the canonical signature; s' = N - s, v = !v
const s = BN_N - BigInt(this._s);
const v = <27 | 28>(55 - this.v);
const result = new Signature(_guard, this.r, toUint256(s), v);
// Populate the networkV if necessary
if (this.networkV) { result.#networkV = this.networkV; }
return result;
}
/**
* Returns a new identical [[Signature]].
*/
clone(): Signature {
const clone = new Signature(_guard, this.r, this._s, this.v);
if (this.networkV) { clone.#networkV = this.networkV; }
return clone;
}
/**
* Returns a representation that is compatible with ``JSON.stringify``.
*/
toJSON(): any {
const networkV = this.networkV;
return {
_type: "signature",
networkV: ((networkV != null) ? networkV.toString(): null),
r: this.r, s: this._s, v: this.v,
};
}
[inspect](): string {
return this.toString();
}
toString(): string {
if (this.isValid()) {
return `Signature { r: ${ this.r }, s: ${ this._s }, v: ${ this.v } }`;
}
return `Signature { r: ${ this.r }, s: ${ this._s }, v: ${ this.v }, valid: false }`;
}
/**
* Compute the chain ID from the ``v`` in a legacy EIP-155 transactions.
*
* @example:
* Signature.getChainId(45)
* //_result:
*
* Signature.getChainId(46)
* //_result:
*/
static getChainId(v: BigNumberish): bigint {
const bv = getBigInt(v, "v");
// The v is not an EIP-155 v, so it is the unspecified chain ID
if ((bv == BN_27) || (bv == BN_28)) { return BN_0; }
// Bad value for an EIP-155 v
assertArgument(bv >= BN_35, "invalid EIP-155 v", "v", v);
return (bv - BN_35) / BN_2;
}
/**
* Compute the ``v`` for a chain ID for a legacy EIP-155 transactions.
*
* Legacy transactions which use [[link-eip-155]] hijack the ``v``
* property to include the chain ID.
*
* @example:
* Signature.getChainIdV(5, 27)
* //_result:
*
* Signature.getChainIdV(5, 28)
* //_result:
*
*/
static getChainIdV(chainId: BigNumberish, v: 27 | 28): bigint {
return (getBigInt(chainId) * BN_2) + BigInt(35 + v - 27);
}
/**
* Compute the normalized legacy transaction ``v`` from a ``yParirty``,
* a legacy transaction ``v`` or a legacy [[link-eip-155]] transaction.
*
* @example:
* // The values 0 and 1 imply v is actually yParity
* Signature.getNormalizedV(0)
* //_result:
*
* // Legacy non-EIP-1559 transaction (i.e. 27 or 28)
* Signature.getNormalizedV(27)
* //_result:
*
* // Legacy EIP-155 transaction (i.e. >= 35)
* Signature.getNormalizedV(46)
* //_result:
*
* // Invalid values throw
* Signature.getNormalizedV(5)
* //_error:
*/
static getNormalizedV(v: BigNumberish): 27 | 28 {
const bv = getBigInt(v);
if (bv === BN_0 || bv === BN_27) { return 27; }
if (bv === BN_1 || bv === BN_28) { return 28; }
assertArgument(bv >= BN_35, "invalid v", "v", v);
// Otherwise, EIP-155 v means odd is 27 and even is 28
return (bv & BN_1) ? 27: 28;
}
/**
* Creates a new [[Signature]].
*
* If no %%sig%% is provided, a new [[Signature]] is created
* with default values.
*
* If %%sig%% is a string, it is parsed.
*/
static from(sig?: SignatureLike): Signature {
function assertError(check: unknown, message: string): asserts check {
assertArgument(check, message, "signature", sig);
};
if (sig == null) {
return new Signature(_guard, ZeroHash, ZeroHash, 27);
}
if (typeof(sig) === "string") {
const bytes = getBytes(sig, "signature");
if (bytes.length === 64) {
const r = hexlify(bytes.slice(0, 32));
const s = bytes.slice(32, 64);
const v = (s[0] & 0x80) ? 28: 27;
s[0] &= 0x7f;
return new Signature(_guard, r, hexlify(s), v);
}
if (bytes.length === 65) {
const r = hexlify(bytes.slice(0, 32));
const s = hexlify(bytes.slice(32, 64));
const v = Signature.getNormalizedV(bytes[64]);
return new Signature(_guard, r, s, v);
}
assertError(false, "invalid raw signature length");
}
if (sig instanceof Signature) { return sig.clone(); }
// Get r
const _r = sig.r;
assertError(_r != null, "missing r");
const r = toUint256(_r);
// Get s; by any means necessary (we check consistency below)
const s = (function(s?: string, yParityAndS?: string) {
if (s != null) { return toUint256(s); }
if (yParityAndS != null) {
assertError(isHexString(yParityAndS, 32), "invalid yParityAndS");
const bytes = getBytes(yParityAndS);
bytes[0] &= 0x7f;
return hexlify(bytes);
}
assertError(false, "missing s");
})(sig.s, sig.yParityAndS);
// Get v; by any means necessary (we check consistency below)
const { networkV, v } = (function(_v?: BigNumberish, yParityAndS?: string, yParity?: Numeric): { networkV?: bigint, v: 27 | 28 } {
if (_v != null) {
const v = getBigInt(_v);
return {
networkV: ((v >= BN_35) ? v: undefined),
v: Signature.getNormalizedV(v)
};
}
if (yParityAndS != null) {
assertError(isHexString(yParityAndS, 32), "invalid yParityAndS");
return { v: ((getBytes(yParityAndS)[0] & 0x80) ? 28: 27) };
}
if (yParity != null) {
switch (getNumber(yParity, "sig.yParity")) {
case 0: return { v: 27 };
case 1: return { v: 28 };
}
assertError(false, "invalid yParity");
}
assertError(false, "missing v");
})(sig.v, sig.yParityAndS, sig.yParity);
const result = new Signature(_guard, r, s, v);
if (networkV) { result.#networkV = networkV; }
// If multiple of v, yParity, yParityAndS we given, check they match
assertError(sig.yParity == null || getNumber(sig.yParity, "sig.yParity") === result.yParity, "yParity mismatch");
assertError(sig.yParityAndS == null || sig.yParityAndS === result.yParityAndS, "yParityAndS mismatch");
return result;
}
}
|