Skip to content

Commit

Permalink
[xp] refactor signature class
Browse files Browse the repository at this point in the history
  • Loading branch information
peterjah committed Apr 24, 2024
1 parent d64c835 commit 140bf97
Show file tree
Hide file tree
Showing 4 changed files with 40 additions and 46 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ export class Address {
/**
* Get the address version from bytes.
* *
* @returns the address type enum.
* @returns the address version.
*/
private getVersion(): Version {
if (!this.bytes) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ export function extractData(
data: string,
expectedVersion: Version
): Uint8Array {
const raw: Uint8Array = serializer.deserialize(data)
const raw = serializer.deserialize(data)
const { data: extractedData, version } = versioner.extract(raw)
if (version !== expectedVersion) {
throw new Error(
Expand Down
10 changes: 7 additions & 3 deletions packages/massa-web3/src/experimental/basicElements/keys.ts
Original file line number Diff line number Diff line change
Expand Up @@ -149,9 +149,12 @@ export class PrivateKey {
* @returns The signature byte array.
*/
public async sign(message: Uint8Array): Promise<Signature> {
const signatureRawBytes = await this.signer.sign(
this.bytes,
this.hasher.hash(message)
)
return Signature.fromBytes(
await this.signer.sign(this.bytes, this.hasher.hash(message)),
this.version
this.versioner.attach(this.version, signatureRawBytes)
)
}

Expand Down Expand Up @@ -310,10 +313,11 @@ export class PublicKey {
data: Uint8Array,
signature: Signature
): Promise<boolean> {
const { data: rawSignature } = this.versioner.extract(signature.toBytes())
return await this.signer.verify(
this.bytes,
this.hasher.hash(data),
signature.bytes
rawSignature
)
}

Expand Down
72 changes: 31 additions & 41 deletions packages/massa-web3/src/experimental/basicElements/signature.ts
Original file line number Diff line number Diff line change
@@ -1,57 +1,35 @@
import Base58 from '../crypto/base58'
import Serializer from '../crypto/interfaces/serializer'
import { Version, Versioner } from '../crypto/interfaces/versioner'
import VarintVersioner from '../crypto/varintVersioner'
import { extractData } from './internal'
import { Version } from '../crypto/interfaces/versioner'
import varint from 'varint'

const DEFAULT_VERSION = Version.V0

/**
* A class representing a signature.
*/
export class Signature {
public bytes: Uint8Array
private bytes: Uint8Array

protected constructor(
public serializer: Serializer,
public versioner: Versioner,
public version: Version
) {}
public version: Version = DEFAULT_VERSION

/**
* Initializes a new signature object from a version.
*
* @param version - The version of the signature.
*
* @returns A new signature instance.
*/
protected static initFromVersion(version: Version = Version.V0): Signature {
switch (version) {
case Version.V0:
return new Signature(new Base58(), new VarintVersioner(), version)
default:
throw new Error(`unsupported version: ${version}`)
}
}
protected constructor(public serializer: Serializer = new Base58()) {}

/**
* Initializes a new signature object from a serialized string.
*
* @param str - The serialized signature string.
* @param version - The version of the signature. If not defined, the last version will be used.
*
* @returns A new signature instance.
*
* @throws If the signature string is invalid.
*/
public static fromString(str: string, version?: Version): Signature {
const signature = Signature.initFromVersion(version)
public static fromString(str: string): Signature {

Check warning on line 27 in packages/massa-web3/src/experimental/basicElements/signature.ts

View workflow job for this annotation

GitHub Actions / Coverage annotations (🧪 jest-coverage-report-action)

🕹️ Function is not covered

Warning! Not covered function
const signature = new Signature()

Check warning on line 28 in packages/massa-web3/src/experimental/basicElements/signature.ts

View workflow job for this annotation

GitHub Actions / Coverage annotations (🧪 jest-coverage-report-action)

🧾 Statement is not covered

Warning! Not covered statement

try {
signature.bytes = extractData(
signature.serializer,
signature.versioner,
str,
signature.version
)
signature.bytes = signature.serializer.deserialize(str)

Check warning on line 31 in packages/massa-web3/src/experimental/basicElements/signature.ts

View workflow job for this annotation

GitHub Actions / Coverage annotations (🧪 jest-coverage-report-action)

🧾 Statement is not covered

Warning! Not covered statement
signature.version = signature.getVersion()

Check warning on line 32 in packages/massa-web3/src/experimental/basicElements/signature.ts

View workflow job for this annotation

GitHub Actions / Coverage annotations (🧪 jest-coverage-report-action)

🧾 Statement is not covered

Warning! Not covered statement
} catch (e) {
throw new Error(`invalid signature string: ${e.message}`)
}

Check warning on line 35 in packages/massa-web3/src/experimental/basicElements/signature.ts

View workflow job for this annotation

GitHub Actions / Coverage annotations (🧪 jest-coverage-report-action)

🧾 Statement is not covered

Warning! Not covered statement
Expand All @@ -60,26 +38,38 @@ export class Signature {
}

/**
* Initializes a signature object from a raw byte array.
* Get the signature version from bytes.
* *
* @returns the signature version.
*/
private getVersion(): Version {
if (!this.bytes) {
throw new Error('signature bytes is not initialized')

Check warning on line 47 in packages/massa-web3/src/experimental/basicElements/signature.ts

View workflow job for this annotation

GitHub Actions / Coverage annotations (🧪 jest-coverage-report-action)

🧾 Statement is not covered

Warning! Not covered statement
}

Check warning on line 48 in packages/massa-web3/src/experimental/basicElements/signature.ts

View workflow job for this annotation

GitHub Actions / Coverage annotations (🧪 jest-coverage-report-action)

🌿 Branch is not covered

Warning! Not covered branch
return varint.decode(this.bytes)
}

/**
* Initializes a signature object from a byte array.
*
* @param bytes - The signature raw bytes.
* @param version - The version of the signature. If not defined, the last version will be used.
* @param bytes - The signature bytes.
*
* @returns A signature object.
*/
public static fromBytes(bytes: Uint8Array, version?: Version): Signature {
const signature = Signature.initFromVersion(version)
public static fromBytes(bytes: Uint8Array): Signature {
const signature = new Signature()
signature.bytes = bytes
signature.version = signature.getVersion()
return signature
}

/**
* Versions the Signature key bytes.
* Get signature in bytes format.
*
* @returns The versioned signature key bytes.
*/
public toBytes(): Uint8Array {
return this.versioner.attach(this.version, this.bytes)
return this.bytes
}

/**
Expand All @@ -88,6 +78,6 @@ export class Signature {
* @returns The serialized signature string.
*/
public toString(): string {
return this.serializer.serialize(this.toBytes())
return this.serializer.serialize(this.bytes)

Check warning on line 81 in packages/massa-web3/src/experimental/basicElements/signature.ts

View workflow job for this annotation

GitHub Actions / Coverage annotations (🧪 jest-coverage-report-action)

🧾 Statement is not covered

Warning! Not covered statement
}
}

0 comments on commit 140bf97

Please sign in to comment.