-
Notifications
You must be signed in to change notification settings - Fork 26
/
botauth_test.go
68 lines (61 loc) · 1.64 KB
/
botauth_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
package bot
import (
"context"
"encoding/json"
"log/slog"
"net/http"
"os"
"os/user"
"path/filepath"
"strings"
"testing"
"github.com/stretchr/testify/assert"
)
func TestSignRequest(t *testing.T) {
assert := assert.New(t)
keystore, err := read("./test_config.json")
assert.Nil(err)
su := keystore.BuildSafeUser()
logger := slog.Default()
ctx := context.Background()
client := NewDefaultClient(su, logger)
r, err := http.NewRequest(http.MethodGet, "https://test.com/account_addresses?chain_id=solana&user_id=2f73822c-56e8-4e5d-99a4-cdbf75cd", nil)
if err != nil {
t.Fatal(err)
}
ts := 1718102244
s, err := client.SignRequest(ctx, int64(ts), "489cfe0b-08d8-47f4-a330-fff193cc8086", r)
if err != nil {
return
}
assert.Equal("YTgyZTFhNmEtNzVjOS00MDEzLTgwYmMtMTAxODNlZWY0OWEyBM-CNnETfQGwHzNh4x0N5JsxofbCoCpbc7jikoR7C-Y", s)
}
type keystore struct {
AppID string `json:"app_id"`
SessionID string `json:"session_id"`
SessionPrivateKey string `json:"session_private_key"`
ServerPublicKey string `json:"server_public_key"`
SpendPrivateKey string `json:"pin"`
}
func (k *keystore) BuildSafeUser() *SafeUser {
return &SafeUser{
UserId: k.AppID,
SessionId: k.SessionID,
SessionPrivateKey: k.SessionPrivateKey,
ServerPublicKey: k.ServerPublicKey,
SpendPrivateKey: k.SpendPrivateKey,
}
}
func read(path string) (*keystore, error) {
if strings.HasPrefix(path, "~/") {
usr, _ := user.Current()
path = filepath.Join(usr.HomeDir, (path)[2:])
}
f, err := os.ReadFile(path)
if err != nil {
return nil, err
}
var keystore keystore
err = json.Unmarshal(f, &keystore)
return &keystore, err
}