All files / src/data-package signed-package-deserializing.ts

90% Statements 18/20
0% Branches 0/1
75% Functions 3/4
90% Lines 18/20

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  4x             4x               4x     2x 2x     2x 2x   2x 2x   2x     4x     2x 2x   2x     4x 1x   1x     4x          
import { Signature } from "ethers";
import {
  arrayify,
  base64,
  computeAddress,
  recoverPublicKey,
  splitSignature,
} from "ethers/lib/utils";
import { DataPackage } from "./DataPackage";
import { SignedDataPackagePlainObj } from "./SignedDataPackage";
 
export interface SignedDataPackageLike {
  signature: Signature;
  dataPackage: DataPackage;
}
 
export function deserializeSignedPackage(
  plainObject: SignedDataPackagePlainObj
): SignedDataPackageLike {
  const signatureBase64 = plainObject.signature;
  Iif (!signatureBase64) {
    throw new Error("Signature can not be empty");
  }
  const signatureBytes: Uint8Array = base64.decode(signatureBase64);
  const parsedSignature = splitSignature(signatureBytes);
 
  const { signature: _, ...unsignedDataPackagePlainObj } = plainObject;
  const unsignedDataPackage = DataPackage.fromObj(unsignedDataPackagePlainObj);
 
  return { signature: parsedSignature, dataPackage: unsignedDataPackage };
}
 
export function recoverSignerPublicKey(
  object: SignedDataPackageLike
): Uint8Array {
  const digest = object.dataPackage.getSignableHash();
  const publicKeyHex = recoverPublicKey(digest, object.signature);
 
  return arrayify(publicKeyHex);
}
 
export function recoverSignerAddress(object: SignedDataPackageLike): string {
  const signerPublicKeyBytes = recoverSignerPublicKey(object);
 
  return computeAddress(signerPublicKeyBytes);
}
 
export function recoverDeserializedSignerAddress(
  plainObj: SignedDataPackagePlainObj
): string {
  return recoverSignerAddress(deserializeSignedPackage(plainObj));
}