Skip to content

Commit

Permalink
Move ecid to agent.
Browse files Browse the repository at this point in the history
  • Loading branch information
q-uint committed Mar 25, 2024
1 parent 27e90f3 commit 4990206
Show file tree
Hide file tree
Showing 16 changed files with 1,288 additions and 61 deletions.
29 changes: 29 additions & 0 deletions agent.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"encoding/hex"
"fmt"
"net/url"
"reflect"
"time"

"github.com/aviate-labs/agent-go/candid/idl"
Expand All @@ -25,6 +26,32 @@ var DefaultConfig = Config{}
// icp0 is the default host for the Internet Computer.
var icp0, _ = url.Parse("https://icp0.io/")

func effectiveCanisterID(canisterId principal.Principal, args []any) principal.Principal {
// If the canisterId is not aaaaa-aa, return it.
if len(canisterId.Raw) > 0 || len(args) == 0 {
return canisterId
}

v := reflect.ValueOf(args[0])
if v.Kind() == reflect.Struct {
t := v.Type()
// Get the field with the ic tag "canister_id".
for idx := range t.NumField() {
if tag := t.Field(idx).Tag.Get("ic"); tag == "canister_id" {
ecid := v.Field(idx).Interface()
switch ecid := ecid.(type) {
case principal.Principal:
return ecid
default:
// If the field is not a principal, return the original canisterId.
return canisterId
}
}
}
}
return canisterId
}

func uint64FromBytes(raw []byte) uint64 {
switch len(raw) {
case 1:
Expand Down Expand Up @@ -108,6 +135,7 @@ func (a Agent) Call(canisterID principal.Principal, methodName string, args []an
if err != nil {
return err
}
canisterID = effectiveCanisterID(canisterID, args)
a.logger.Printf("[AGENT] CALL %s %s", canisterID, methodName)
if _, err := a.call(canisterID, data); err != nil {
return err
Expand Down Expand Up @@ -208,6 +236,7 @@ func (a Agent) Query(canisterID principal.Principal, methodName string, args []a
if err != nil {
return err
}
canisterID = effectiveCanisterID(canisterID, args)
a.logger.Printf("[AGENT] QUERY %s %s", canisterID, methodName)
resp, err := a.query(canisterID, data)
if err != nil {
Expand Down
70 changes: 63 additions & 7 deletions agent_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,15 @@ import (
"github.com/aviate-labs/agent-go"
"github.com/aviate-labs/agent-go/candid/idl"
"github.com/aviate-labs/agent-go/ic"
mgmt "github.com/aviate-labs/agent-go/ic/ic"
"github.com/aviate-labs/agent-go/ic/icpledger"
"github.com/aviate-labs/agent-go/identity"
"github.com/aviate-labs/agent-go/mgmt"
"github.com/aviate-labs/agent-go/principal"
"testing"
)

var _ = new(testLogger)

func Example_anonymous_query() {
a, _ := agent.New(agent.DefaultConfig)
type Account struct {
Expand Down Expand Up @@ -96,10 +98,8 @@ func Example_query_secp256k1() {
// 0
}

func TestAgent_Call(t *testing.T) {
a, err := mgmt.NewAgent(agent.Config{
Logger: &testLogger{},
})
func TestAgent_Call_bitcoinGetBalanceQuery(t *testing.T) {
a, err := mgmt.NewAgent(ic.MANAGEMENT_CANISTER_PRINCIPAL, agent.DefaultConfig)
if err != nil {
t.Fatal(err)
}
Expand All @@ -112,7 +112,63 @@ func TestAgent_Call(t *testing.T) {
if err != nil {
t.Fatal(err)
}
fmt.Println(r)
if r == nil {
t.Fatal()
}
}

func TestAgent_Call_provisionalTopUpCanister(t *testing.T) {
a, err := mgmt.NewAgent(ic.MANAGEMENT_CANISTER_PRINCIPAL, agent.DefaultConfig)
if err != nil {
t.Fatal(err)
}
if err := a.ProvisionalTopUpCanister(mgmt.ProvisionalTopUpCanisterArgs{
CanisterId: ic.LEDGER_PRINCIPAL,
}); err == nil {
t.Fatal()
}
}

func TestAgent_Query_Ed25519(t *testing.T) {
id, err := identity.NewRandomEd25519Identity()
if err != nil {
t.Fatal(err)
}
a, _ := agent.New(agent.Config{
Identity: id,
})
type Account struct {
Account string `ic:"account"`
}
var balance struct {
E8S uint64 `ic:"e8s"`
}
if err := a.Query(ic.LEDGER_PRINCIPAL, "account_balance_dfx", []any{
Account{"9523dc824aa062dcd9c91b98f4594ff9c6af661ac96747daef2090b7fe87037d"},
}, []any{&balance}); err != nil {
t.Fatal(err)
}
}

func TestAgent_Query_Secp256k1(t *testing.T) {
id, err := identity.NewRandomSecp256k1Identity()
if err != nil {
t.Fatal(err)
}
a, _ := agent.New(agent.Config{
Identity: id,
})
type Account struct {
Account string `ic:"account"`
}
var balance struct {
E8S uint64 `ic:"e8s"`
}
if err := a.Query(ic.LEDGER_PRINCIPAL, "account_balance_dfx", []any{
Account{"9523dc824aa062dcd9c91b98f4594ff9c6af661ac96747daef2090b7fe87037d"},
}, []any{&balance}); err != nil {
t.Fatal(err)
}
}

func TestICPLedger_queryBlocks(t *testing.T) {
Expand All @@ -130,6 +186,6 @@ func TestICPLedger_queryBlocks(t *testing.T) {

type testLogger struct{}

func (l *testLogger) Printf(format string, v ...interface{}) {
func (t testLogger) Printf(format string, v ...interface{}) {
fmt.Printf("[TEST]"+format+"\n", v...)
}
14 changes: 12 additions & 2 deletions candid/idl/variant.go
Original file line number Diff line number Diff line change
Expand Up @@ -210,16 +210,26 @@ func (variant VariantType) structToVariant(value any) (*Variant, error) {
}

tag := ParseTags(field)
if !tag.VariantType {
return nil, fmt.Errorf("invalid variant field: %s", v.Type())
}
if !v.Field(i).IsNil() {
return &Variant{
Name: tag.Name,
Value: v.Field(i).Elem().Interface(),
}, nil
}
if i == v.NumField()-1 {
return &Variant{
Name: tag.Name,
Value: nil,
}, nil
}
}
return nil, fmt.Errorf("invalid variant: %s", v.Type())
default:
return nil, fmt.Errorf("invalid value kind: %s", v.Kind())
}
fmt.Printf("%#v\n", value)
return nil, fmt.Errorf("invalid value kind: %s", v.Kind())
}

func (variant VariantType) unmarshalMap(name string, value any, _v *map[string]any) error {
Expand Down
6 changes: 3 additions & 3 deletions go.mod
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
module github.com/aviate-labs/agent-go

go 1.21.0
go 1.22.1

require (
github.com/aviate-labs/leb128 v0.3.0
github.com/aviate-labs/secp256k1 v0.0.0-5e6736a
github.com/di-wu/parser v0.3.0
github.com/fxamacker/cbor/v2 v2.5.0
github.com/herumi/bls-go-binary v1.32.0
github.com/fxamacker/cbor/v2 v2.6.0
github.com/herumi/bls-go-binary v1.33.0
)

require github.com/x448/float16 v0.8.4 // indirect
8 changes: 4 additions & 4 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,9 @@ github.com/aviate-labs/secp256k1 v0.0.0-5e6736a h1:aQkG/D+l8Y7tr809l8pN+KebH2jza
github.com/aviate-labs/secp256k1 v0.0.0-5e6736a/go.mod h1:C/lr3F9TimrVkdZckG5mz+VU0TrmpeyVKUjzv2YyGwA=
github.com/di-wu/parser v0.3.0 h1:NMOvy5ifswgt4gsdhySVcKOQtvjC43cHZIfViWctqQY=
github.com/di-wu/parser v0.3.0/go.mod h1:SLp58pW6WamdmznrVRrw2NTyn4wAvT9rrEFynKX7nYo=
github.com/fxamacker/cbor/v2 v2.5.0 h1:oHsG0V/Q6E/wqTS2O1Cozzsy69nqCiguo5Q1a1ADivE=
github.com/fxamacker/cbor/v2 v2.5.0/go.mod h1:TA1xS00nchWmaBnEIxPSE5oHLuJBAVvqrtAnWBwBCVo=
github.com/herumi/bls-go-binary v1.32.0 h1:QX0+mmrCyBNpaVwYahl85yWnxYKDPpNAPIZWOIpP+eU=
github.com/herumi/bls-go-binary v1.32.0/go.mod h1:O4Vp1AfR4raRGwFeQpr9X/PQtncEicMoOe6BQt1oX0Y=
github.com/fxamacker/cbor/v2 v2.6.0 h1:sU6J2usfADwWlYDAFhZBQ6TnLFBHxgesMrQfQgk1tWA=
github.com/fxamacker/cbor/v2 v2.6.0/go.mod h1:pxXPTn3joSm21Gbwsv0w9OSA2y1HFR9qXEeXQVeNoDQ=
github.com/herumi/bls-go-binary v1.33.0 h1:OJwWkXTsxF7SLHT8cBLJfb6i97KHfrG4DkgejLcDm78=
github.com/herumi/bls-go-binary v1.33.0/go.mod h1:O4Vp1AfR4raRGwFeQpr9X/PQtncEicMoOe6BQt1oX0Y=
github.com/x448/float16 v0.8.4 h1:qLwI1I70+NjRFUR3zs1JPUCgaCXSh3SW62uAKT1mSBM=
github.com/x448/float16 v0.8.4/go.mod h1:14CWIYCyZA/cWjXOioeEpHeN/83MdbZDRQHoFcYsOfg=
Loading

0 comments on commit 4990206

Please sign in to comment.