-
Notifications
You must be signed in to change notification settings - Fork 6
feat(DAPP-7970): add typescript conf http example #199
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
Open
jeffrifwaldsmartcontract
wants to merge
1
commit into
main
Choose a base branch
from
DAPP-7970-http-confidential-example
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+184
−1
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
52 changes: 52 additions & 0 deletions
52
cmd/creinit/template/workflow/typescriptConfHTTP/README.md
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,52 @@ | ||
| # Typescript Confidential HTTP Example | ||
|
|
||
| This template provides a Typescript Confidential HTTP workflow example. It shows how to set a secret header and send it via the ConfidentialHTTP capability. | ||
|
|
||
| Steps to run the example | ||
|
|
||
| ## 1. Update .env file | ||
|
|
||
| You'll need to add a secret value to the .env file for requests to read. This is the value that will be set as a header when sending requests via the ConfidentialHTTP capability. | ||
|
|
||
| ``` | ||
| SECRET_HEADER_VALUE=abcd1234 | ||
| ``` | ||
|
|
||
| Note: Make sure your `workflow.yaml` file is pointing to the config.json, example: | ||
|
|
||
| ```yaml | ||
| staging-settings: | ||
| user-workflow: | ||
| workflow-name: "conf-http" | ||
| workflow-artifacts: | ||
| workflow-path: "./main.ts" | ||
| config-path: "./config.json" | ||
| ``` | ||
|
|
||
| ## 2. Install dependencies | ||
|
|
||
| If `bun` is not already installed, see https://bun.com/docs/installation for installing in your environment. | ||
|
|
||
| ```bash | ||
| cd <workflow-name> && bun install | ||
| ``` | ||
|
|
||
| Example: For a workflow directory named `conf-http` the command would be: | ||
|
|
||
| ```bash | ||
| cd conf-http && bun install | ||
| ``` | ||
|
|
||
| ## 3. Simulate the workflow | ||
|
|
||
| Run the command from <b>project root directory</b> | ||
|
|
||
| ```bash | ||
| cre workflow simulate <path-to-workflow-directory> --target=staging-settings | ||
| ``` | ||
|
|
||
| Example: For workflow named `conf-http` the command would be: | ||
|
|
||
| ```bash | ||
| cre workflow simulate ./conf-http --target=staging-settings | ||
| ``` |
4 changes: 4 additions & 0 deletions
4
cmd/creinit/template/workflow/typescriptConfHTTP/config.production.json
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,4 @@ | ||
| { | ||
| "schedule": "*/30 * * * * *", | ||
| "url": "https://postman-echo.com/headers" | ||
| } |
4 changes: 4 additions & 0 deletions
4
cmd/creinit/template/workflow/typescriptConfHTTP/config.staging.json
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,4 @@ | ||
| { | ||
| "schedule": "*/30 * * * * *", | ||
| "url": "https://postman-echo.com/headers" | ||
| } |
86 changes: 86 additions & 0 deletions
86
cmd/creinit/template/workflow/typescriptConfHTTP/main.ts.tpl
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,86 @@ | ||
| import { | ||
| type ConfidentialHTTPSendRequester, | ||
| consensusIdenticalAggregation, | ||
| cre, | ||
| json, | ||
| ok, | ||
| Runner, | ||
| type Runtime, | ||
| } from '@chainlink/cre-sdk' | ||
| import { z } from 'zod' | ||
|
|
||
| const configSchema = z.object({ | ||
| schedule: z.string(), | ||
| url: z.string(), | ||
| }) | ||
|
|
||
| type Config = z.infer<typeof configSchema> | ||
|
|
||
| type ResponseValues = { | ||
| result: { | ||
| headers: { | ||
| 'secret-header': string | ||
| } | ||
| } | ||
| } | ||
|
|
||
| const fetchResult = (sendRequester: ConfidentialHTTPSendRequester, config: Config) => { | ||
| const { responses } = sendRequester | ||
| .sendRequests({ | ||
| input: { | ||
| requests: [ | ||
| { | ||
| url: config.url, | ||
| method: 'GET', | ||
| headers: ['secret-header: {{.SECRET_HEADER}}'], | ||
| }, | ||
| ], | ||
| }, | ||
| vaultDonSecrets: [ | ||
| { | ||
| key: 'SECRET_HEADER', | ||
| }, | ||
| ], | ||
| }) | ||
| .result() | ||
| const response = responses[0] | ||
|
|
||
| if (!ok(response)) { | ||
| throw new Error(`HTTP request failed with status: ${response.statusCode}`) | ||
| } | ||
|
|
||
| return json(response) as ResponseValues | ||
| } | ||
|
|
||
| const onCronTrigger = (runtime: Runtime<Config>) => { | ||
| runtime.log('Confidential HTTP workflow triggered.') | ||
|
|
||
| const confHTTPClient = new cre.capabilities.ConfidentialHTTPClient() | ||
| const result = confHTTPClient | ||
| .sendRequests( | ||
| runtime, | ||
| fetchResult, | ||
| consensusIdenticalAggregation(), | ||
| )(runtime.config) | ||
| .result() | ||
|
|
||
| runtime.log(`Successfully fetched result: ${result}`) | ||
|
|
||
| return { | ||
| result, | ||
| } | ||
| } | ||
|
|
||
| const initWorkflow = (config: Config) => { | ||
| const cron = new cre.capabilities.CronCapability() | ||
|
|
||
| return [cre.handler(cron.trigger({ schedule: config.schedule }), onCronTrigger)] | ||
| } | ||
|
|
||
| export async function main() { | ||
| const runner = await Runner.newRunner<Config>({ configSchema }) | ||
|
|
||
| await runner.run(initWorkflow) | ||
| } | ||
|
|
||
| main() | ||
16 changes: 16 additions & 0 deletions
16
cmd/creinit/template/workflow/typescriptConfHTTP/package.json.tpl
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,16 @@ | ||
| { | ||
| "name": "typescript-simple-template", | ||
| "version": "1.0.0", | ||
| "main": "dist/main.js", | ||
| "private": true, | ||
| "scripts": { | ||
| "postinstall": "bunx cre-setup" | ||
| }, | ||
| "license": "UNLICENSED", | ||
| "dependencies": { | ||
| "@chainlink/cre-sdk": "^1.0.0" | ||
| }, | ||
| "devDependencies": { | ||
| "@types/bun": "1.2.21" | ||
| } | ||
| } |
3 changes: 3 additions & 0 deletions
3
cmd/creinit/template/workflow/typescriptConfHTTP/secrets.yaml
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,3 @@ | ||
| secretsNames: | ||
| SECRET_HEADER: | ||
| - SECRET_HEADER_VALUE |
16 changes: 16 additions & 0 deletions
16
cmd/creinit/template/workflow/typescriptConfHTTP/tsconfig.json.tpl
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,16 @@ | ||
| { | ||
| "compilerOptions": { | ||
| "target": "esnext", | ||
| "module": "ESNext", | ||
| "moduleResolution": "bundler", | ||
| "lib": ["ESNext"], | ||
| "outDir": "./dist", | ||
| "strict": true, | ||
| "esModuleInterop": true, | ||
| "skipLibCheck": true, | ||
| "forceConsistentCasingInFileNames": true | ||
| }, | ||
| "include": [ | ||
| "main.ts" | ||
| ] | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
it would be good to have an "Owner" field here. Example: https://github.com/smartcontractkit/confidential-compute/blob/e9dacce67251bbb21c9e2320bca4ff221f9bae9b/tests/e2e/confidentialhttp.go#L42C4-L42C9