Skip to content

Commit

Permalink
refactor: change transaction layout and use RLP encoding for signing …
Browse files Browse the repository at this point in the history
…/ hashing (#725)

* tx refactor

* use RLP encoding for signature / hash calculation

* regenerate genesis block

* style: resolve style guide violations

* fix some tests

* style: resolve style guide violations

* update imports

* set default network

* fix crypto-block fixtures

* style: resolve style guide violations

* unit test fixes

* update e2e configs

* remove obsolete deps

* style: resolve style guide violations

* remove obsolete type field

* regenerate genesis

* update e2e

* style: resolve style guide violations

---------

Co-authored-by: oXtxNt9U <[email protected]>
  • Loading branch information
oXtxNt9U and oXtxNt9U authored Oct 28, 2024
1 parent 957164c commit 96bce8b
Show file tree
Hide file tree
Showing 112 changed files with 3,013 additions and 5,025 deletions.
40 changes: 16 additions & 24 deletions packages/api-development/source/resources/transaction.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import { injectable } from "@mainsail/container";
import { Contracts } from "@mainsail/contracts";
import { Utils as AppUtils } from "@mainsail/kernel";

@injectable()
export class TransactionResource implements Contracts.Api.Resource {
Expand All @@ -9,31 +8,24 @@ export class TransactionResource implements Contracts.Api.Resource {
}

public async transform(resource: Contracts.Crypto.TransactionData): Promise<object> {
AppUtils.assert.defined<string>(resource.senderPublicKey);
// AppUtils.assert.defined<string>(resource.senderPublicKey);

return {
amount: resource.amount.toFixed(),
asset: resource.asset,
blockId: resource.blockId,
fee: resource.fee.toFixed(),
id: resource.id,
nonce: resource.nonce?.toFixed(),
recipient: resource.recipientId,
senderPublicKey: resource.senderPublicKey,

signature: resource.signature,

signatures: resource.signatures,

timestamp: resource.timestamp,

type: resource.type,

typeGroup: resource.typeGroup,

vendorField: resource.vendorField,

version: resource.version,
// amount: resource.amount.toFixed(),
// asset: resource.asset,
// blockId: resource.blockId,
// fee: resource.fee.toFixed(),
// id: resource.id,
// nonce: resource.nonce?.toFixed(),
// recipient: resource.recipientId,
// senderPublicKey: resource.senderPublicKey,
// signature: resource.signature,
// signatures: resource.signatures,
// timestamp: resource.timestamp,
// type: resource.type,
// typeGroup: resource.typeGroup,
// vendorField: resource.vendorField,
// version: resource.version,
};
}
}
39 changes: 20 additions & 19 deletions packages/api-sync/source/service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -261,25 +261,26 @@ export class Sync implements Contracts.ApiSync.Service {

receipts: transactionReceipts,

transactions: transactions.map(({ data }) => ({
amount: data.amount.toFixed(),
asset: data.asset,
blockHeight: header.height.toFixed(),
blockId: header.id,
fee: data.fee.toFixed(),
id: data.id as unknown as string,
nonce: data.nonce.toFixed(),
recipientId: data.recipientId,
senderPublicKey: data.senderPublicKey,
sequence: data.sequence as unknown as number,
signature: data.signature,
signatures: data.signatures,
timestamp: header.timestamp.toFixed(),
type: data.type,
typeGroup: data.typeGroup,
vendorField: data.vendorField,
version: data.version,
})),
transactions: [], // TODO
// transactions: transactions.map(({ data }) => ({
// amount: data.amount.toFixed(),
// asset: {}, // TODO
// blockHeight: header.height.toFixed(),
// blockId: header.id,
// fee: data.fee.toFixed(),
// id: data.id as unknown as string,
// nonce: data.nonce.toFixed(),
// recipientId: data.recipientId,
// senderPublicKey: data.senderPublicKey,
// sequence: data.sequence as unknown as number,
// signature: data.signature,
// signatures: data.signatures,
// timestamp: header.timestamp.toFixed(),
// type: data.type,
// typeGroup: data.typeGroup,
// vendorField: data.vendorField,
// version: data.version,
// })),

wallets,

Expand Down
44 changes: 17 additions & 27 deletions packages/api-transaction-pool/source/resources/transaction.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import { injectable } from "@mainsail/container";
import { Contracts } from "@mainsail/contracts";
import { Utils as AppUtils } from "@mainsail/kernel";

@injectable()
export class TransactionResource implements Contracts.Api.Resource {
Expand All @@ -9,37 +8,28 @@ export class TransactionResource implements Contracts.Api.Resource {
}

public async transform(resource: Contracts.Crypto.TransactionData): Promise<object> {
AppUtils.assert.defined<string>(resource.senderPublicKey);
//AppUtils.assert.defined<string>(resource.senderPublicKey);

// const wallet = await this.stateService.getStore().walletRepository.findByPublicKey(resource.senderPublicKey);
// const sender: string = wallet.getAddress();

return {
amount: resource.amount.toFixed(),
asset: resource.asset,
blockId: resource.blockId,
fee: resource.fee.toFixed(),
id: resource.id,
nonce: resource.nonce?.toFixed(),
recipient: resource.recipientId,

// sender,

// senderPublicKey: resource.senderPublicKey,

signature: resource.signature,

signatures: resource.signatures,

timestamp: resource.timestamp,

type: resource.type,

typeGroup: resource.typeGroup,

vendorField: resource.vendorField,

version: resource.version,
// amount: resource.amount.toFixed(),
// asset: resource.asset,
// blockId: resource.blockId,
// fee: resource.fee.toFixed(),
// id: resource.id,
// nonce: resource.nonce?.toFixed(),
// recipient: resource.recipientId,
// // sender,
// // senderPublicKey: resource.senderPublicKey,
// signature: resource.signature,
// signatures: resource.signatures,
// timestamp: resource.timestamp,
// type: resource.type,
// typeGroup: resource.typeGroup,
// vendorField: resource.vendorField,
// version: resource.version,
};
}
}
2 changes: 1 addition & 1 deletion packages/configuration-generator/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@
"@mainsail/crypto-hash-bcrypto": "workspace:*",
"@mainsail/crypto-key-pair-ecdsa": "workspace:*",
"@mainsail/crypto-messages": "workspace:*",
"@mainsail/crypto-signature-schnorr": "workspace:*",
"@mainsail/crypto-signature-ecdsa": "workspace:*",
"@mainsail/crypto-transaction": "workspace:*",
"@mainsail/crypto-transaction-evm-call": "workspace:*",
"@mainsail/crypto-validation": "workspace:*",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import { ServiceProvider as CoreCryptoConsensus } from "@mainsail/crypto-consens
import { ServiceProvider as CoreCryptoHashBcrypto } from "@mainsail/crypto-hash-bcrypto";
import { ServiceProvider as CoreCryptoKeyPairEcdsa } from "@mainsail/crypto-key-pair-ecdsa";
import { ServiceProvider as CryptoMessages } from "@mainsail/crypto-messages";
import { ServiceProvider as CoreCryptoSignatureSchnorr } from "@mainsail/crypto-signature-schnorr";
import { ServiceProvider as CoreCryptoSignatureEcdsa } from "@mainsail/crypto-signature-ecdsa";
import { ServiceProvider as CoreCryptoTransaction } from "@mainsail/crypto-transaction";
import { ServiceProvider as CoreCryptoTransactionEvmCall } from "@mainsail/crypto-transaction-evm-call";
import { ServiceProvider as CoreCryptoValidation } from "@mainsail/crypto-validation";
Expand Down Expand Up @@ -45,7 +45,7 @@ export const makeApplication = async (configurationPath: string, options: Record
await app.resolve(CoreCryptoConfig).register();
await app.resolve(CoreCryptoValidation).register();
await app.resolve(CoreCryptoHashBcrypto).register();
await app.resolve(CoreCryptoSignatureSchnorr).register();
await app.resolve(CoreCryptoSignatureEcdsa).register();
await app.resolve(CoreCryptoKeyPairEcdsa).register();
await app.resolve(CoreCryptoAddressKeccak256).register();
await app.resolve(CryptoMessages).register();
Expand Down
30 changes: 15 additions & 15 deletions packages/configuration-generator/source/generators/genesis-block.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,6 @@ export class GenesisBlockGenerator extends Generator {
@inject(Identifiers.Cryptography.Transaction.Verifier)
private readonly transactionVerifier!: Contracts.Crypto.TransactionVerifier;

@inject(Identifiers.Evm.Gas.Limits)
private readonly gasLimits!: Contracts.Evm.GasLimits;

async generate(
genesisMnemonic: string,
validatorsMnemonics: string[],
Expand Down Expand Up @@ -85,10 +82,11 @@ export class GenesisBlockGenerator extends Generator {
await this.app
.resolve(EvmCallBuilder)
.network(pubKeyHash)
.recipientId(recipient.address)
.recipientAddress(recipient.address)
.nonce(nonce.toFixed(0))
.amount(amount)
.value(amount)
.payload("")
.gasPrice(0)
.gasLimit(21_000)
.sign(sender.passphrase)
).build();
Expand Down Expand Up @@ -128,9 +126,10 @@ export class GenesisBlockGenerator extends Generator {
await this.app
.resolve(EvmCallBuilder)
.network(pubKeyHash)
.recipientId(consensusContractAddress)
.recipientAddress(consensusContractAddress)
.nonce("0") // validator registration tx is always the first one from sender
.payload(data)
.gasPrice(0)
.gasLimit(500_000)
.sign(sender.passphrase)
).build();
Expand All @@ -154,9 +153,10 @@ export class GenesisBlockGenerator extends Generator {
await this.app
.resolve(EvmCallBuilder)
.network(pubKeyHash)
.recipientId(consensusContractAddress)
.recipientAddress(consensusContractAddress)
.nonce("1") // vote transaction is always the 3rd tx from sender (1st one is validator registration)
.payload(data)
.gasPrice(0)
.gasLimit(200_000)
.sign(sender.passphrase)
).build();
Expand Down Expand Up @@ -212,9 +212,9 @@ export class GenesisBlockGenerator extends Generator {

Utils.assert.defined<string>(data.id);

totals.amount = totals.amount.plus(data.amount);
totals.fee = totals.fee.plus(data.fee);
totals.gasUsed += this.gasLimits.of(transaction);
totals.amount = totals.amount.plus(data.value);
totals.fee = totals.fee.plus(data.gasPrice);
totals.gasUsed += data.gasLimit;

payloadBuffers.push(Buffer.from(data.id, "hex"));
transactionData.push(data);
Expand Down Expand Up @@ -257,11 +257,11 @@ export class GenesisBlockGenerator extends Generator {
}

async #ensureValidGenesisBlock(genesis: Contracts.Crypto.Commit): Promise<void> {
if (
!(await Promise.all(
genesis.block.transactions.map((transaction) => this.transactionVerifier.verifyHash(transaction.data)),
))
) {
const verifiedTransactions = await Promise.all(
genesis.block.transactions.map((transaction) => this.transactionVerifier.verifyHash(transaction.data)),
);

if (verifiedTransactions.includes(false)) {
throw new Error("genesis block contains invalid transactions");
}

Expand Down
6 changes: 6 additions & 0 deletions packages/contracts/source/contracts/crypto/identities.ts
Original file line number Diff line number Diff line change
Expand Up @@ -80,4 +80,10 @@ export interface Signature {
deserialize(buffer: ByteBuffer): Buffer;

aggregate(signatures: Buffer[]): Promise<string>;

signRecoverable(message: Buffer, privateKey: Buffer): Promise<string>;

verifyRecoverable(signature: Buffer, message: Buffer, publicKey: Buffer): Promise<boolean>;

recoverPublicKey(message: Buffer, signature: Buffer): string;
}
Loading

0 comments on commit 96bce8b

Please sign in to comment.