Skip to content

Commit

Permalink
Update serialization and deserialization due to node changes
Browse files Browse the repository at this point in the history
  • Loading branch information
alexmyshchyshyn committed Nov 24, 2024
1 parent 16e07f4 commit a5df4c8
Show file tree
Hide file tree
Showing 4 changed files with 242 additions and 191 deletions.
43 changes: 23 additions & 20 deletions src/types/Transaction.test.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,11 @@
import { BigNumber } from '@ethersproject/bignumber';
import { assert, expect } from 'chai';

import { Duration, Timestamp } from './Time';
import { TransactionV1 } from './Transaction';
import { InitiatorAddr } from './InitiatorAddr';
import { PrivateKey } from './keypair/PrivateKey';
import { FixedMode, PricingMode } from './PricingMode';

import { KeyAlgorithm } from './keypair/Algorithm';

import { KeyAlgorithm, PrivateKey, PublicKey } from './keypair';
import { SessionTarget, TransactionTarget } from './TransactionTarget';
import {
TransactionEntryPoint,
Expand All @@ -21,13 +19,11 @@ import {
CLValueUInt512,
CLValueUInt64
} from './clvalue';
import { PublicKey } from './keypair';
import { TransactionV1Payload } from './TransactionV1Payload';
import { Hash } from './key';
import { assert, expect } from 'chai';

describe('Test Transaction', () => {
it('should create a Transaction from TransactionV1', async () => {
it('should create a TransactionV1 with correct payload instance', async () => {
const keys = await PrivateKey.generate(KeyAlgorithm.ED25519);
const paymentAmount = 20000000000000;

Expand Down Expand Up @@ -57,35 +53,42 @@ describe('Test Transaction', () => {
'8bf9d406ab901428d43ecd3a6f214b864e7ef8316934e5e0f049650a65b40d73'
);

const transactionTarget = new TransactionTarget(
undefined,
undefined,
sessionTarget
);
const scheduling = new TransactionScheduling({});
const entryPoint = new TransactionEntryPoint(
TransactionEntryPointEnum.Call
);

const transactionPayload = TransactionV1Payload.build({
initiatorAddr: new InitiatorAddr(keys.publicKey),
ttl: new Duration(1800000),
args,
timestamp: new Timestamp(new Date()),
category: 2,
entryPoint: new TransactionEntryPoint(TransactionEntryPointEnum.Call),
scheduling: new TransactionScheduling({}),
transactionTarget: new TransactionTarget(
undefined,
undefined,
sessionTarget
),
entryPoint,
scheduling,
transactionTarget,
chainName: 'casper-net-1',
pricingMode
});

const transaction = TransactionV1.makeTransactionV1(transactionPayload);
await transaction.sign(keys);

const toJson = TransactionV1.toJson(transaction);
const parsed = TransactionV1.fromJSON(toJson);

const transactionPaymentAmount = parsed.payload.args.args
const transactionPaymentAmount = transaction.payload.fields.args.args
.get('amount')!
.toString();

assert.deepEqual(parsed.approvals[0].signer, keys.publicKey);
assert.deepEqual(transaction.approvals[0].signer, keys.publicKey);
expect(transaction.payload).to.deep.equal(transactionPayload);
assert.deepEqual(parseInt(transactionPaymentAmount, 10), paymentAmount);
expect(transaction.payload.chainName).to.deep.equal('casper-net-1');
expect(transaction.payload.fields.target).to.deep.equal(transactionTarget);
expect(transaction.payload.fields.args).to.deep.equal(args);
expect(transaction.payload.fields.scheduling).to.deep.equal(scheduling);
expect(transaction.payload.fields.entryPoint).to.deep.equal(entryPoint);
});
});
26 changes: 10 additions & 16 deletions src/types/Transaction.ts
Original file line number Diff line number Diff line change
Expand Up @@ -117,15 +117,12 @@ export class TransactionV1 {
public hash: Hash;

/**
* The header of the transaction.
* The payload of the transaction.
* A merge of header and body concepts from before.
*/
@jsonMember({
name: 'payload',
constructor: TransactionV1Payload,
deserializer: json => {
if (!json) return;
return TransactionV1Payload.fromJSON(json);
}
constructor: TransactionV1Payload
})
public payload: TransactionV1Payload;

Expand All @@ -146,7 +143,7 @@ export class TransactionV1 {
}

/**
* Validates the transaction by checking the body hash and the approval signatures.
* Validates the transaction by checking the transaction hash and the approval signatures.
* @throws {TransactionError} Throws errors if validation fails.
*/
public validate(): boolean {
Expand Down Expand Up @@ -205,8 +202,7 @@ export class TransactionV1 {
/**
* Creates a new `TransactionV1` instance.
* @param hash The hash of the transaction.
* @param header The header of the transaction.
* @param body The body of the transaction.
* @param payload The payload of the transaction. A merge of header and body concepts from before.
* @param approvals The approvals for the transaction.
* @returns A new `TransactionV1` instance.
*/
Expand All @@ -220,8 +216,7 @@ export class TransactionV1 {

/**
* Creates a new `TransactionV1` instance with a header and body.
* @param transactionHeader The header of the transaction.
* @param transactionBody The body of the transaction.
* @param payload The payload of the transaction. A merge of header and body concepts from before.
* @returns A new `TransactionV1` instance.
*/
static makeTransactionV1(payload: TransactionV1Payload): TransactionV1 {
Expand Down Expand Up @@ -531,11 +526,10 @@ export class Transaction {
v1.payload.pricingMode
),
new TransactionBody(
v1.payload.args,
v1.payload.target,
v1.payload.entryPoint,
v1.payload.scheduling,
v1.payload.category
v1.payload.fields.args,
v1.payload.fields.target,
v1.payload.fields.entryPoint,
v1.payload.fields.scheduling
),
v1.approvals,
v1
Expand Down
Loading

0 comments on commit a5df4c8

Please sign in to comment.