Skip to content

Commit

Permalink
Gateway client script: added readme and json pretty print (#11361)
Browse files Browse the repository at this point in the history
* Gateway client script: added readme and json pretty print

* Reading S4 payload from the file
  • Loading branch information
Andrei Smirnov authored and Borja Aranda committed Dec 14, 2023
1 parent d6465f0 commit 7f1a336
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 4 deletions.
20 changes: 20 additions & 0 deletions core/scripts/gateway/client/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
# A gateway client script

This script is used to connect to a gateway server and send commands to it.

## Usage

All requests have to be signed on behalf of a user, you need to provide your private key in .env file, e.g.

```
PRIVATE_KEY=1a2b3c...
```

The script will automatically sign the message using the provided private key.
Run the script without arguments to get the list of available commands.

## Example

```
go run . -gateway_url https://01.functions-gateway.chain.link -don_id fun-avalanche-mainnet-2 -method secrets_list -message_id 123
```
23 changes: 19 additions & 4 deletions core/scripts/gateway/client/send_request.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ func main() {
s4SetSlotId := flag.Uint("s4_set_slot_id", 0, "S4 set slot ID")
s4SetVersion := flag.Uint64("s4_set_version", 0, "S4 set version")
s4SetExpirationPeriod := flag.Int64("s4_set_expiration_period", 60*60*1000, "S4 how long until the entry expires from now (in milliseconds)")
s4SetPayload := flag.String("s4_set_payload", "", "S4 set payload")
s4SetPayloadFile := flag.String("s4_set_payload_file", "", "S4 payload file to set secret")
repeat := flag.Bool("repeat", false, "Repeat sending the request every 10 seconds")
flag.Parse()

Expand All @@ -50,14 +50,24 @@ func main() {
}
address := crypto.PubkeyToAddress(key.PublicKey)

var s4SetPayload []byte
if *methodName == functions.MethodSecretsSet {
var err error
s4SetPayload, err = os.ReadFile(*s4SetPayloadFile)
if err != nil {
fmt.Println("error reading S4 payload file", err)
return
}
}

// build payload (if relevant)
var payloadJSON []byte
if *methodName == functions.MethodSecretsSet {
envelope := s4.Envelope{
Address: address.Bytes(),
SlotID: *s4SetSlotId,
Version: *s4SetVersion,
Payload: []byte(*s4SetPayload),
Payload: s4SetPayload,
Expiration: time.Now().UnixMilli() + *s4SetExpirationPeriod,
}
signature, err := envelope.Sign(key)
Expand All @@ -70,7 +80,7 @@ func main() {
SlotID: envelope.SlotID,
Version: envelope.Version,
Expiration: envelope.Expiration,
Payload: []byte(*s4SetPayload),
Payload: s4SetPayload,
Signature: signature,
}

Expand Down Expand Up @@ -131,7 +141,12 @@ func main() {
return
}

fmt.Println(string(body))
var prettyJSON bytes.Buffer
if err := json.Indent(&prettyJSON, body, "", " "); err != nil {
fmt.Println(string(body))
} else {
fmt.Println(prettyJSON.String())
}
}

sendRequest()
Expand Down

0 comments on commit 7f1a336

Please sign in to comment.