Skip to content
Open
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
2 changes: 2 additions & 0 deletions cmd/creinit/creinit.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ const (
const (
HelloWorldTemplate string = "HelloWorld"
PoRTemplate string = "PoR"
ConfHTTPTemplate string = "ConfHTTP"
)

type WorkflowTemplate struct {
Expand Down Expand Up @@ -70,6 +71,7 @@ var languageTemplates = []LanguageTemplate{
Workflows: []WorkflowTemplate{
{Folder: "typescriptSimpleExample", Title: "Helloworld: Typescript Hello World example", ID: 3, Name: HelloWorldTemplate},
{Folder: "typescriptPorExampleDev", Title: "Custom data feed: Typescript updating on-chain data periodically using offchain API data", ID: 4, Name: PoRTemplate},
{Folder: "typescriptConfHTTP", Title: "Confidential Http: Typescript example using the confidential http capability", ID: 5, Name: ConfHTTPTemplate},
},
},
}
Expand Down
52 changes: 52 additions & 0 deletions cmd/creinit/template/workflow/typescriptConfHTTP/README.md
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
```
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
{
"schedule": "*/30 * * * * *",
"url": "https://postman-echo.com/headers"
}
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 cmd/creinit/template/workflow/typescriptConfHTTP/main.ts.tpl
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',
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

},
],
})
.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()
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"
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
secretsNames:
SECRET_HEADER:
- SECRET_HEADER_VALUE
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"
]
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
},
"license": "UNLICENSED",
"dependencies": {
"@chainlink/cre-sdk": "^1.0.0",
"@chainlink/cre-sdk": "^1.0.1",
"viem": "2.34.0",
"zod": "3.25.76"
},
Expand Down
Loading