refactor(state): extract state management to dedicated package
This commit is contained in:
@@ -1,2 +1,113 @@
|
||||
// Package state contains the state of the enclave by leveraging the extism.PDK config/context variables.
|
||||
// Package state contains the state of the enclave.
|
||||
package state
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
|
||||
"github.com/extism/go-pdk"
|
||||
)
|
||||
|
||||
const (
|
||||
keyInitialized = "enclave:initialized"
|
||||
keyDID = "enclave:did"
|
||||
keyDIDID = "enclave:did_id"
|
||||
)
|
||||
|
||||
func Default() {
|
||||
if pdk.GetVarInt(keyInitialized) == 0 {
|
||||
pdk.SetVarInt(keyInitialized, 0)
|
||||
pdk.SetVar(keyDID, nil)
|
||||
pdk.SetVarInt(keyDIDID, 0)
|
||||
}
|
||||
pdk.Log(pdk.LogDebug, "state: initialized default state")
|
||||
}
|
||||
|
||||
func IsInitialized() bool {
|
||||
return pdk.GetVarInt(keyInitialized) == 1
|
||||
}
|
||||
|
||||
func SetInitialized(v bool) {
|
||||
if v {
|
||||
pdk.SetVarInt(keyInitialized, 1)
|
||||
} else {
|
||||
pdk.SetVarInt(keyInitialized, 0)
|
||||
}
|
||||
}
|
||||
|
||||
func GetDID() string {
|
||||
data := pdk.GetVar(keyDID)
|
||||
if data == nil {
|
||||
return ""
|
||||
}
|
||||
return string(data)
|
||||
}
|
||||
|
||||
func SetDID(did string) {
|
||||
pdk.SetVar(keyDID, []byte(did))
|
||||
}
|
||||
|
||||
func GetDIDID() int64 {
|
||||
return int64(pdk.GetVarInt(keyDIDID))
|
||||
}
|
||||
|
||||
func SetDIDID(id int64) {
|
||||
pdk.SetVarInt(keyDIDID, int(id))
|
||||
}
|
||||
|
||||
func GetString(key string) string {
|
||||
data := pdk.GetVar(key)
|
||||
if data == nil {
|
||||
return ""
|
||||
}
|
||||
return string(data)
|
||||
}
|
||||
|
||||
func SetString(key, value string) {
|
||||
pdk.SetVar(key, []byte(value))
|
||||
}
|
||||
|
||||
func GetBytes(key string) []byte {
|
||||
return pdk.GetVar(key)
|
||||
}
|
||||
|
||||
func SetBytes(key string, value []byte) {
|
||||
pdk.SetVar(key, value)
|
||||
}
|
||||
|
||||
func GetInt(key string) int {
|
||||
return pdk.GetVarInt(key)
|
||||
}
|
||||
|
||||
func SetInt(key string, value int) {
|
||||
pdk.SetVarInt(key, value)
|
||||
}
|
||||
|
||||
func GetJSON(key string, v any) error {
|
||||
data := pdk.GetVar(key)
|
||||
if data == nil {
|
||||
return nil
|
||||
}
|
||||
return json.Unmarshal(data, v)
|
||||
}
|
||||
|
||||
func SetJSON(key string, v any) error {
|
||||
data, err := json.Marshal(v)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
pdk.SetVar(key, data)
|
||||
return nil
|
||||
}
|
||||
|
||||
func GetConfig(key string) (string, bool) {
|
||||
return pdk.GetConfig(key)
|
||||
}
|
||||
|
||||
func MustGetConfig(key string) string {
|
||||
val, ok := pdk.GetConfig(key)
|
||||
if !ok {
|
||||
pdk.SetErrorString("config key required: " + key)
|
||||
return ""
|
||||
}
|
||||
return val
|
||||
}
|
||||
|
||||
22
main.go
22
main.go
@@ -9,17 +9,13 @@ import (
|
||||
"strings"
|
||||
|
||||
"enclave/internal/keybase"
|
||||
"enclave/internal/state"
|
||||
"enclave/internal/types"
|
||||
|
||||
"github.com/extism/go-pdk"
|
||||
)
|
||||
|
||||
var enclave = &struct {
|
||||
initialized bool
|
||||
did string
|
||||
}{}
|
||||
|
||||
func main() {}
|
||||
func main() { state.Default() }
|
||||
|
||||
//go:wasmexport ping
|
||||
func ping() int32 {
|
||||
@@ -77,8 +73,8 @@ func generate() int32 {
|
||||
return 1
|
||||
}
|
||||
|
||||
enclave.initialized = true
|
||||
enclave.did = did
|
||||
state.SetInitialized(true)
|
||||
state.SetDID(did)
|
||||
|
||||
dbBytes, err := serializeDatabase()
|
||||
if err != nil {
|
||||
@@ -125,8 +121,8 @@ func load() int32 {
|
||||
return 1
|
||||
}
|
||||
|
||||
enclave.initialized = true
|
||||
enclave.did = did
|
||||
state.SetInitialized(true)
|
||||
state.SetDID(did)
|
||||
|
||||
output := types.LoadOutput{
|
||||
Success: true,
|
||||
@@ -146,7 +142,7 @@ func load() int32 {
|
||||
func exec() int32 {
|
||||
pdk.Log(pdk.LogInfo, "exec: executing action")
|
||||
|
||||
if !enclave.initialized {
|
||||
if !state.IsInitialized() {
|
||||
output := types.ExecOutput{Success: false, Error: "database not initialized, call generate or load first"}
|
||||
pdk.OutputJSON(output)
|
||||
return 0
|
||||
@@ -207,7 +203,7 @@ func exec() int32 {
|
||||
func query() int32 {
|
||||
pdk.Log(pdk.LogInfo, "query: resolving DID document")
|
||||
|
||||
if !enclave.initialized {
|
||||
if !state.IsInitialized() {
|
||||
pdk.SetError(errors.New("database not initialized, call generate or load first"))
|
||||
return 1
|
||||
}
|
||||
@@ -219,7 +215,7 @@ func query() int32 {
|
||||
}
|
||||
|
||||
if input.DID == "" {
|
||||
input.DID = enclave.did
|
||||
input.DID = state.GetDID()
|
||||
}
|
||||
|
||||
if !strings.HasPrefix(input.DID, "did:") {
|
||||
|
||||
Reference in New Issue
Block a user