Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Run pretty #458

Merged
merged 1 commit into from
Jun 5, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
48 changes: 35 additions & 13 deletions src/proto/serializer.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,14 +23,17 @@ describe("serialize transactions", () => {
sender: wallets.alice.address,
receiver: wallets.bob.address,
gasLimit: 50000,
chainID: "local-testnet"
chainID: "local-testnet",
});

const signer = wallets.alice.signer;
transaction.applySignature(await signer.sign(transaction.serializeForSigning()));

let buffer = serializer.serializeTransaction(transaction);
assert.equal(buffer.toString("hex"), "0859120200001a208049d639e5a6980d1cd2392abcce41029cda74a1563523a202f09641cc2618f82a200139472eff6886771a982f3083da5d421f24c29181e63888228dc81ca60d69e1388094ebdc0340d08603520d6c6f63616c2d746573746e6574580262403f08a1dd64fbb627d10b048e0b45b1390f29bb0e457762a2ccb710b029f299022a67a4b8e45cf62f4314afec2e56b5574c71e38df96cc41fae757b7ee5062503");
assert.equal(
buffer.toString("hex"),
"0859120200001a208049d639e5a6980d1cd2392abcce41029cda74a1563523a202f09641cc2618f82a200139472eff6886771a982f3083da5d421f24c29181e63888228dc81ca60d69e1388094ebdc0340d08603520d6c6f63616c2d746573746e6574580262403f08a1dd64fbb627d10b048e0b45b1390f29bb0e457762a2ccb710b029f299022a67a4b8e45cf62f4314afec2e56b5574c71e38df96cc41fae757b7ee5062503",
);
});

it("with data, no value", async () => {
Expand All @@ -41,14 +44,17 @@ describe("serialize transactions", () => {
receiver: wallets.bob.address,
gasLimit: 80000,
data: new TransactionPayload("hello"),
chainID: "local-testnet"
chainID: "local-testnet",
});

const signer = wallets.alice.signer;
transaction.applySignature(await signer.sign(transaction.serializeForSigning()));

let buffer = serializer.serializeTransaction(transaction);
assert.equal(buffer.toString("hex"), "085a120200001a208049d639e5a6980d1cd2392abcce41029cda74a1563523a202f09641cc2618f82a200139472eff6886771a982f3083da5d421f24c29181e63888228dc81ca60d69e1388094ebdc034080f1044a0568656c6c6f520d6c6f63616c2d746573746e657458026240f9e8c1caf7f36b99e7e76ee1118bf71b55cde11a2356e2b3adf15f4ad711d2e1982469cbba7eb0afbf74e8a8f78e549b9410cd86eeaa88fcba62611ac9f6e30e");
assert.equal(
buffer.toString("hex"),
"085a120200001a208049d639e5a6980d1cd2392abcce41029cda74a1563523a202f09641cc2618f82a200139472eff6886771a982f3083da5d421f24c29181e63888228dc81ca60d69e1388094ebdc034080f1044a0568656c6c6f520d6c6f63616c2d746573746e657458026240f9e8c1caf7f36b99e7e76ee1118bf71b55cde11a2356e2b3adf15f4ad711d2e1982469cbba7eb0afbf74e8a8f78e549b9410cd86eeaa88fcba62611ac9f6e30e",
);
});

it("with data, with value", async () => {
Expand All @@ -59,14 +65,17 @@ describe("serialize transactions", () => {
receiver: wallets.bob.address,
gasLimit: 100000,
data: new TransactionPayload("for the book"),
chainID: "local-testnet"
chainID: "local-testnet",
});

const signer = wallets.alice.signer;
transaction.applySignature(await signer.sign(transaction.serializeForSigning()));

let buffer = serializer.serializeTransaction(transaction);
assert.equal(buffer.toString("hex"), "085b1209008ac7230489e800001a208049d639e5a6980d1cd2392abcce41029cda74a1563523a202f09641cc2618f82a200139472eff6886771a982f3083da5d421f24c29181e63888228dc81ca60d69e1388094ebdc0340a08d064a0c666f722074686520626f6f6b520d6c6f63616c2d746573746e657458026240b45f22e9f57a6df22670fcc3566723a0711a05ac2547456de59fd222a54940e4a1d99bd414897ccbf5c02a842ad86e638989b7f4d30edd26c99a8cd1eb092304");
assert.equal(
buffer.toString("hex"),
"085b1209008ac7230489e800001a208049d639e5a6980d1cd2392abcce41029cda74a1563523a202f09641cc2618f82a200139472eff6886771a982f3083da5d421f24c29181e63888228dc81ca60d69e1388094ebdc0340a08d064a0c666f722074686520626f6f6b520d6c6f63616c2d746573746e657458026240b45f22e9f57a6df22670fcc3566723a0711a05ac2547456de59fd222a54940e4a1d99bd414897ccbf5c02a842ad86e638989b7f4d30edd26c99a8cd1eb092304",
);
});

it("with data, with large value", async () => {
Expand All @@ -77,14 +86,17 @@ describe("serialize transactions", () => {
receiver: wallets.bob.address,
gasLimit: 100000,
data: new TransactionPayload("for the spaceship"),
chainID: "local-testnet"
chainID: "local-testnet",
});

const signer = wallets.alice.signer;
transaction.applySignature(await signer.sign(transaction.serializeForSigning()));

let buffer = serializer.serializeTransaction(transaction);
assert.equal(buffer.toString("hex"), "085c120e00018ee90ff6181f3761632000001a208049d639e5a6980d1cd2392abcce41029cda74a1563523a202f09641cc2618f82a200139472eff6886771a982f3083da5d421f24c29181e63888228dc81ca60d69e1388094ebdc0340a08d064a11666f722074686520737061636573686970520d6c6f63616c2d746573746e65745802624001f05aa8cb0614e12a94ab9dcbde5e78370a4e05d23ef25a1fb9d5fcf1cb3b1f33b919cd8dafb1704efb18fa233a8aa0d3344fb6ee9b613a7d7a403786ffbd0a");
assert.equal(
buffer.toString("hex"),
"085c120e00018ee90ff6181f3761632000001a208049d639e5a6980d1cd2392abcce41029cda74a1563523a202f09641cc2618f82a200139472eff6886771a982f3083da5d421f24c29181e63888228dc81ca60d69e1388094ebdc0340a08d064a11666f722074686520737061636573686970520d6c6f63616c2d746573746e65745802624001f05aa8cb0614e12a94ab9dcbde5e78370a4e05d23ef25a1fb9d5fcf1cb3b1f33b919cd8dafb1704efb18fa233a8aa0d3344fb6ee9b613a7d7a403786ffbd0a",
);
});

it("with nonce = 0", async () => {
Expand All @@ -96,13 +108,20 @@ describe("serialize transactions", () => {
gasLimit: 80000,
data: new TransactionPayload("hello"),
chainID: "local-testnet",
version: new TransactionVersion(1)
version: new TransactionVersion(1),
});

transaction.applySignature(new Signature("dfa3e9f2fdec60dcb353bac3b3435b4a2ff251e7e98eaf8620f46c731fc70c8ba5615fd4e208b05e75fe0f7dc44b7a99567e29f94fcd91efac7e67b182cd2a04"))
transaction.applySignature(
new Signature(
"dfa3e9f2fdec60dcb353bac3b3435b4a2ff251e7e98eaf8620f46c731fc70c8ba5615fd4e208b05e75fe0f7dc44b7a99567e29f94fcd91efac7e67b182cd2a04",
),
);

let buffer = serializer.serializeTransaction(transaction);
assert.equal(buffer.toString("hex"), "120200001a208049d639e5a6980d1cd2392abcce41029cda74a1563523a202f09641cc2618f82a200139472eff6886771a982f3083da5d421f24c29181e63888228dc81ca60d69e1388094ebdc034080f1044a0568656c6c6f520d6c6f63616c2d746573746e657458016240dfa3e9f2fdec60dcb353bac3b3435b4a2ff251e7e98eaf8620f46c731fc70c8ba5615fd4e208b05e75fe0f7dc44b7a99567e29f94fcd91efac7e67b182cd2a04");
assert.equal(
buffer.toString("hex"),
"120200001a208049d639e5a6980d1cd2392abcce41029cda74a1563523a202f09641cc2618f82a200139472eff6886771a982f3083da5d421f24c29181e63888228dc81ca60d69e1388094ebdc034080f1044a0568656c6c6f520d6c6f63616c2d746573746e657458016240dfa3e9f2fdec60dcb353bac3b3435b4a2ff251e7e98eaf8620f46c731fc70c8ba5615fd4e208b05e75fe0f7dc44b7a99567e29f94fcd91efac7e67b182cd2a04",
);
});

it("with usernames", async () => {
Expand All @@ -114,13 +133,16 @@ describe("serialize transactions", () => {
senderUsername: "carol",
receiverUsername: "alice",
gasLimit: 50000,
chainID: "T"
chainID: "T",
});

const signer = wallets.carol.signer;
transaction.applySignature(await signer.sign(transaction.serializeForSigning()));

const buffer = serializer.serializeTransaction(transaction);
assert.equal(buffer.toString("hex"), "08cc011209000de0b6b3a76400001a200139472eff6886771a982f3083da5d421f24c29181e63888228dc81ca60d69e12205616c6963652a20b2a11555ce521e4944e09ab17549d85b487dcd26c84b5017a39e31a3670889ba32056361726f6c388094ebdc0340d086035201545802624051e6cd78fb3ab4b53ff7ad6864df27cb4a56d70603332869d47a5cf6ea977c30e696103e41e8dddf2582996ad335229fdf4acb726564dbc1a0bc9e705b511f06");
assert.equal(
buffer.toString("hex"),
"08cc011209000de0b6b3a76400001a200139472eff6886771a982f3083da5d421f24c29181e63888228dc81ca60d69e12205616c6963652a20b2a11555ce521e4944e09ab17549d85b487dcd26c84b5017a39e31a3670889ba32056361726f6c388094ebdc0340d086035201545802624051e6cd78fb3ab4b53ff7ad6864df27cb4a56d70603332869d47a5cf6ea977c30e696103e41e8dddf2582996ad335229fdf4acb726564dbc1a0bc9e705b511f06",
);
});
});
10 changes: 7 additions & 3 deletions src/proto/serializer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
* Serializes a Transaction object to a Buffer. Handles low-level conversion logic and field-mappings as well.
*/
serializeTransaction(transaction: Transaction): Buffer {
const proto = require("./compiled").proto;

Check warning on line 19 in src/proto/serializer.ts

View workflow job for this annotation

GitHub Actions / build (16.x)

Require statement not part of import statement
const receiverPubkey = new Address(transaction.getReceiver().bech32()).pubkey();
const senderPubkey = new Address(transaction.getSender().bech32()).pubkey();

Expand All @@ -25,15 +25,19 @@
Nonce: transaction.getNonce().valueOf() ? transaction.getNonce().valueOf() : undefined,
Value: this.serializeTransactionValue(transaction.getValue()),
RcvAddr: receiverPubkey,
RcvUserName: transaction.getReceiverUsername() ? Buffer.from(transaction.getReceiverUsername()).toString("base64") : undefined,
RcvUserName: transaction.getReceiverUsername()
? Buffer.from(transaction.getReceiverUsername()).toString("base64")
: undefined,
SndAddr: senderPubkey,
SndUserName: transaction.getSenderUsername() ? Buffer.from(transaction.getSenderUsername()).toString("base64") : undefined,
SndUserName: transaction.getSenderUsername()
? Buffer.from(transaction.getSenderUsername()).toString("base64")
: undefined,
GasPrice: transaction.getGasPrice().valueOf(),
GasLimit: transaction.getGasLimit().valueOf(),
Data: transaction.getData().length() == 0 ? null : transaction.getData().valueOf(),
ChainID: Buffer.from(transaction.getChainID().valueOf()),
Version: transaction.getVersion().valueOf(),
Signature: transaction.getSignature()
Signature: transaction.getSignature(),
});

if (transaction.getOptions().valueOf() !== TRANSACTION_OPTIONS_DEFAULT) {
Expand Down
Loading
Loading