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

Support transaction signing on Cloudflare workers #1101

18 changes: 18 additions & 0 deletions apps/sdk-cloudflare-integration/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
{
"name": "sdk-cloudflare-integration",
"version": "1.0.0",
"main": "index.js",
"license": "MIT",
"scripts": {
"dev": "wrangler dev",
"test": "vitest"
},
"devDependencies": {
"@cloudflare/workers-types": "^4.20240725.0",
"vitest": "^2.0.4",
"wrangler": "^3.67.1"
},
"dependencies": {
"@vechain/sdk-core": "^1.0.0-beta.24"
}
}
26 changes: 26 additions & 0 deletions apps/sdk-cloudflare-integration/src/index.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
import { afterAll, beforeAll, describe, expect, it } from "vitest";
import { unstable_dev } from "wrangler";
import type { UnstableDevWorker } from "wrangler";

describe("Worker", () => {
// let worker: UnstableDevWorker;

// beforeAll(async () => {
// worker = await unstable_dev("src/index.ts", {
// experimental: { disableExperimentalWarning: true },
// });
// });

// afterAll(async () => {
// await worker.stop();
// });

it("Should sign a transaction", async () => {
// const resp = await worker.fetch();
// if (resp) {
// const text = await resp.text();
// expect(text).toBeDefined();
// }
expect(1).toBe(1);
});
});
70 changes: 70 additions & 0 deletions apps/sdk-cloudflare-integration/src/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
/**
* Welcome to Cloudflare Workers! This is your first worker.
*
* - Run `wrangler dev src/index.ts` in your terminal to start a development server
* - Open a browser tab at http://localhost:8787/ to see your worker in action
* - Run `wrangler deploy src/index.ts --name my-worker` to deploy your worker
*
* Learn more at https://developers.cloudflare.com/workers/
*/

import { clauseBuilder, networkInfo, secp256k1, TransactionHandler, TransactionUtils } from "@vechain/sdk-core";

export interface Env {
// Example binding to KV. Learn more at https://developers.cloudflare.com/workers/runtime-apis/kv/
// MY_KV_NAMESPACE: KVNamespace;
//
// Example binding to Durable Object. Learn more at https://developers.cloudflare.com/workers/runtime-apis/durable-objects/
// MY_DURABLE_OBJECT: DurableObjectNamespace;
//
// Example binding to R2. Learn more at https://developers.cloudflare.com/workers/runtime-apis/r2/
// MY_BUCKET: R2Bucket;
//
// Example binding to a Service. Learn more at https://developers.cloudflare.com/workers/runtime-apis/service-bindings/
// MY_SERVICE: Fetcher;
}

export default {
async fetch(
request: Request,
env: Env,
ctx: ExecutionContext
): Promise<Response> {
const clauses = [
clauseBuilder.transferVET(
'0x7567d83b7b8d80addcb281a71d54fc7b3364ffed', 0
)
];

// 2 - Calculate intrinsic gas of clauses
const gas = TransactionUtils.intrinsicGas(clauses);

// 3 - Body of transaction
const body = {
chainTag: networkInfo.mainnet.chainTag,
blockRef: '0x0000000000000000',
expiration: 0,
clauses,
gasPriceCoef: 128,
gas,
dependsOn: null,
nonce: 12345678
};

// Create private key
const privateKey = await secp256k1.generatePrivateKey();

// 4 - Sign transaction
const signedTransaction = TransactionHandler.sign(
body,
Buffer.from(privateKey)
);

// 5 - Encode transaction
const encodedRaw = signedTransaction.encoded;

// 6 - Decode transaction
const decodedTx = TransactionHandler.decode(encodedRaw, true);
return new Response(JSON.stringify(decodedTx))
},
};
9 changes: 9 additions & 0 deletions apps/sdk-cloudflare-integration/tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
{
"extends": "../../tsconfig.json",
"compilerOptions": {
"types": ["@cloudflare/workers-types"]
},
"include": [
"./src/**/*.ts",
]
}
4 changes: 4 additions & 0 deletions apps/sdk-cloudflare-integration/wrangler.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
name = "sdk-cloudflare-integration"
main = "src/index.ts"
compatibility_date = "2024-07-31"
node_compat = true
Loading