Skip to content

Commit

Permalink
Support compressed contract code in tx builder
Browse files Browse the repository at this point in the history
  • Loading branch information
samuelmanzanera committed Aug 30, 2024
1 parent 88481bd commit 7874b1e
Show file tree
Hide file tree
Showing 4 changed files with 41 additions and 11 deletions.
6 changes: 3 additions & 3 deletions src/api/node_rpc.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ export class NodeRPCClient {
* @returns {Promise<TransactionFee>} The transaction fee
*/
async getTransactionFee(tx: TransactionBuilder): Promise<TransactionFee> {
return this.client.request("estimate_transaction_fee", { transaction: tx.toNodeRPC() });
return this.client.request("estimate_transaction_fee", { transaction: await tx.toNodeRPC() });
}

/**
Expand All @@ -48,7 +48,7 @@ export class NodeRPCClient {
* @returns {Promise<SendTransactionResponse>} The transaction response
*/
async sendTransaction(tx: TransactionBuilder): Promise<SendTransactionResponse> {
return this.client.request("send_transaction", { transaction: tx.toNodeRPC() });
return this.client.request("send_transaction", { transaction: await tx.toNodeRPC() });
}

/**
Expand All @@ -66,7 +66,7 @@ export class NodeRPCClient {
* @returns {Promise<SimulateContractExecutionResponse[]>} The simulation response per recipient
*/
async simulateContractExecution(tx: TransactionBuilder): Promise<SimulateContractExecutionResponse[]> {
return this.client.request("simulate_contract_execution", { transaction: tx.toNodeRPC() });
return this.client.request("simulate_contract_execution", { transaction: await tx.toNodeRPC() });
}

async handleRequest(jsonRPCRequest: any): Promise<any> {
Expand Down
29 changes: 26 additions & 3 deletions src/transaction_builder.ts
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,7 @@ export default class TransactionBuilder {
* @param {string} code Smart contract code
*/
setCode(code: string) {
this.data.code = new TextEncoder().encode(code);
this.data.code = maybeStringToUint8Array(code);
return this;
}

Expand Down Expand Up @@ -432,14 +432,37 @@ export default class TransactionBuilder {
/**
* JSON RPC API SEND_TRANSACTION
*/
toNodeRPC(): TransactionRPC {
async toNodeRPC(): Promise<TransactionRPC> {
async function asyncConcatUint8Arrays(uint8arrays: Uint8Array[]) {
const blob = new Blob(uint8arrays);
const buffer = await blob.arrayBuffer();
return new Uint8Array(buffer);
}

async function compress(str: Uint8Array): Promise<Uint8Array> {
// Convert the string to a byte stream.
const stream = new Blob([str]).stream();

// Create a compressed stream.
const compressedStream = stream.pipeThrough(new CompressionStream("deflate-raw"));

// Read all the bytes from this stream.
const chunks: Uint8Array[] = [];
//@ts-ignore
for await (const chunk of compressedStream) {
chunks.push(chunk);
}

return await asyncConcatUint8Arrays(chunks);
}

return {
version: this.version,
address: uint8ArrayToHex(this.address),
type: this.type,
data: {
content: new TextDecoder().decode(this.data.content),
code: new TextDecoder().decode(this.data.code),
code: uint8ArrayToHex(await compress(this.data.code)),
ownerships: this.data.ownerships.map(({ secret, authorizedPublicKeys }) => {
return {
secret: uint8ArrayToHex(secret),
Expand Down
6 changes: 3 additions & 3 deletions tests/network.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,7 @@ describe("Network", () => {
id: 1,
method: "estimate_transaction_fee",
params: {
transaction: tx.toNodeRPC()
transaction: await tx.toNodeRPC()
}
})
// @ts-ignore
Expand Down Expand Up @@ -155,7 +155,7 @@ describe("Network", () => {
id: 1,
method: "send_transaction",
params: {
transaction: tx.toNodeRPC()
transaction: await tx.toNodeRPC()
}
})
// @ts-ignore
Expand Down Expand Up @@ -191,7 +191,7 @@ describe("Network", () => {
id: 1,
method: "simulate_contract_execution",
params: {
transaction: tx.toNodeRPC()
transaction: await tx.toNodeRPC()
}
})
// @ts-ignore
Expand Down
11 changes: 9 additions & 2 deletions tests/transaction_builder.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -445,10 +445,10 @@ describe("Transaction builder", () => {

expect(txRPC).toHaveProperty("generateEncryptedSeedSC", true);
});
it("should not affect the NodeTransactionRPC", () => {
it("should not affect the NodeTransactionRPC", async () => {
const generateEncryptedSeedSC = true;
const tx = new TransactionBuilder("transfer").setGenerateEncryptedSeedSC(generateEncryptedSeedSC);
const txRPC = tx.toNodeRPC();
const txRPC = await tx.toNodeRPC();

expect(txRPC).not.toHaveProperty("generateEncryptedSeedSC", true);
});
Expand Down Expand Up @@ -603,4 +603,11 @@ describe("Transaction builder", () => {
expect(txRPC.data.content).toStrictEqual("Hello world");
});
});

describe("toNodeRPC", () => {
it("should compress using zlib contract's code", async () => {
const tx = new TransactionBuilder("code").setCode("0061736d01000000015e1160017f017f60067f7f7f7f7f7f0060037f7f7f");
console.log(await tx.toNodeRPC());
});
});
});

0 comments on commit 7874b1e

Please sign in to comment.