Skip to content

Commit

Permalink
verify legacy second signature
Browse files Browse the repository at this point in the history
  • Loading branch information
oXtxNt9U committed Dec 11, 2024
1 parent 1ffb716 commit 5802b8f
Show file tree
Hide file tree
Showing 5 changed files with 61 additions and 5 deletions.
4 changes: 3 additions & 1 deletion packages/contracts/source/contracts/crypto/transactions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ export interface TransactionData {
timestamp: number;

signature?: string;
secondSignature?: string;
legacySecondSignature?: string;

sequence?: number;
gasUsed?: number;
Expand Down Expand Up @@ -103,6 +103,8 @@ export interface TransactionVerifier {
verifyHash(data: TransactionData): Promise<boolean>;

verifySchema(data: Omit<TransactionData, "id">, strict?: boolean): Promise<SchemaValidationResult>;

verifyLegacySecondSignature(data: TransactionData, legacySecondPublicKey: string): Promise<boolean>;
}

export interface TransactionSigner {
Expand Down
18 changes: 18 additions & 0 deletions packages/contracts/source/exceptions/crypto.ts
Original file line number Diff line number Diff line change
Expand Up @@ -242,6 +242,24 @@ export class SenderWalletMismatchError extends Exception {
}
}

export class UnexpectedLegacySecondSignatureError extends Exception {
public constructor() {
super(`Failed to apply transaction, because wallet does not allow legacy second signatures.`);
}
}

export class InvalidLegacySecondSignatureError extends Exception {
public constructor() {
super(`Failed to apply transaction, because the legacy second signature could not be verified.`);
}
}

export class MissingLegacySecondSignatureError extends Exception {
public constructor() {
super(`Failed to apply transaction, because the legacy second signature is missing.`);
}
}

export class MissingMultiSignatureOnSenderError extends Exception {
public constructor() {
super(`Failed to apply transaction, because sender does not have a multi signature.`);
Expand Down
7 changes: 7 additions & 0 deletions packages/crypto-transaction/source/validation/schemas.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,13 @@ export const transactionBaseSchema: SchemaObject = {
senderPublicKey: { $ref: "publicKey" },
signature: { allOf: [{ maxLength: 130, minLength: 130 }, { $ref: "alphanumeric" }], type: "string" },
value: { bignumber: { maximum: undefined, minimum: 0 } },

// Legacy
legacySecondSignature: {
// TODO: double check format
allOf: [{ maxLength: 130, minLength: 130 }, { $ref: "alphanumeric" }],
type: "string",
},
// signatures: {
// items: { allOf: [{ maxLength: 130, minLength: 130 }, { $ref: "alphanumeric" }], type: "string" },
// maxItems: 16,
Expand Down
21 changes: 21 additions & 0 deletions packages/crypto-transaction/source/verifier.ts
Original file line number Diff line number Diff line change
Expand Up @@ -105,4 +105,25 @@ export class Verifier implements Contracts.Crypto.TransactionVerifier {

return this.validator.validate(strict ? `${$id}Strict` : `${$id}`, data);
}

public async verifyLegacySecondSignature(
data: Contracts.Crypto.TransactionData,
legacySecondPublicKey: string,
): Promise<boolean> {
const { legacySecondSignature } = data;

if (!legacySecondSignature) {
return false;
}

const hash: Buffer = await this.utils.toHash(data, {
excludeSignature: true,
});

return this.signatureFactory.verify(
Buffer.from(legacySecondSignature, "hex"),
hash,
Buffer.from(legacySecondPublicKey, "hex"),
);
}
}
16 changes: 12 additions & 4 deletions packages/transactions/source/handlers/transaction.ts
Original file line number Diff line number Diff line change
Expand Up @@ -46,11 +46,19 @@ export abstract class TransactionHandler implements Contracts.Transactions.Trans
throw new Exceptions.InsufficientBalanceError();
}

// Legacy
if (sender.hasLegacySecondPublicKey()) {
// TODO: enable
// if (!(await this.verifier.verifyHash(transaction.data, sender.legacySecondPublicKey()))) {
// throw new Exceptions.InvalidSignatureError();
// }
if (!transaction.data.legacySecondSignature) {
throw new Exceptions.MissingLegacySecondSignatureError();
}

if (!(await this.verifier.verifyLegacySecondSignature(transaction.data, sender.legacySecondPublicKey()))) {
throw new Exceptions.InvalidLegacySecondSignatureError();
}
} else {
if (transaction.data.legacySecondSignature) {
throw new Exceptions.UnexpectedLegacySecondSignatureError();
}
}
}

Expand Down

0 comments on commit 5802b8f

Please sign in to comment.