diff --git a/.github/workflows/release.yaml b/.github/workflows/release.yaml index 66fd6340..87c94b1d 100644 --- a/.github/workflows/release.yaml +++ b/.github/workflows/release.yaml @@ -74,11 +74,11 @@ jobs: run: | git checkout . - name: Run GoReleaser - uses: goreleaser/goreleaser-action@v2 + uses: goreleaser/goreleaser-action@v6 with: distribution: goreleaser version: latest - args: release --rm-dist --config .goreleaser-mobile.yaml + args: release --clean --config .goreleaser-mobile.yaml env: GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} diff --git a/Makefile b/Makefile index 6b7a9803..9761a62d 100644 --- a/Makefile +++ b/Makefile @@ -74,7 +74,7 @@ release: -v `pwd`:/go/src/github.com/fairDataSociety/fairOS-dfs \ -v /var/run/docker.sock:/var/run/docker.sock \ -w /go/src/github.com/fairDataSociety/fairOS-dfs \ - ghcr.io/goreleaser/goreleaser-cross:v1.21.0 release --rm-dist + ghcr.io/goreleaser/goreleaser-cross:v1.21.0 release --clean .PHONY: release-dry-run release-dry-run: @@ -84,9 +84,8 @@ release-dry-run: -v `pwd`:/go/src/github.com/fairDataSociety/fairOS-dfs \ -v /var/run/docker.sock:/var/run/docker.sock \ -w /go/src/github.com/fairDataSociety/fairOS-dfs \ - ghcr.io/goreleaser/goreleaser-cross:v1.21.0 release --rm-dist \ - --skip-validate=true \ - --skip-publish + ghcr.io/goreleaser/goreleaser-cross:v1.21.0 release \ + --clean --skip-validate=true --skip-publish .PHONY: wasm wasm: diff --git a/cmd/dfs/cmd/server_test.go b/cmd/dfs/cmd/server_test.go index 82cadbbc..feae81ed 100644 --- a/cmd/dfs/cmd/server_test.go +++ b/cmd/dfs/cmd/server_test.go @@ -941,13 +941,19 @@ func TestApis(t *testing.T) { t.Fatal(err) } + if podForkResp.StatusCode != 200 { + forkRespErr, err := io.ReadAll(podForkResp.Body) + if err != nil { + t.Fatal(err) + } + t.Log(string(forkRespErr)) + podForkResp.Body.Close() + t.Fatal("pod fork failed with status code:", podForkResp.StatusCode) + } err = podForkResp.Body.Close() if err != nil { t.Fatal(err) } - if podForkResp.StatusCode != 200 { - t.Fatal("pod fork failed") - } podOpenRequest := &common.PodRequest{ PodName: podForkRequest.ForkName, } diff --git a/go.mod b/go.mod index 42ddc994..fc514c8b 100644 --- a/go.mod +++ b/go.mod @@ -3,8 +3,8 @@ module github.com/fairdatasociety/fairOS-dfs go 1.22.0 require ( - github.com/asabya/swarm-act v0.0.0-20241022090815-9b494c0051de - github.com/asabya/swarm-blockstore v0.0.0-20241022084926-8d6753f32697 + github.com/asabya/swarm-act v0.0.0-20241106102524-234e35a79f95 + github.com/asabya/swarm-blockstore v0.0.0-20241106102400-00f682459f25 github.com/btcsuite/btcd/btcec/v2 v2.3.2 github.com/c-bata/go-prompt v0.2.6 github.com/dustin/go-humanize v1.0.1 diff --git a/go.sum b/go.sum index 5560e5bf..05b09d9f 100644 --- a/go.sum +++ b/go.sum @@ -11,10 +11,10 @@ github.com/VictoriaMetrics/fastcache v1.12.2/go.mod h1:AmC+Nzz1+3G2eCPapF6UcsnkT github.com/aead/siphash v1.0.1/go.mod h1:Nywa3cDsYNNK3gaciGTWPwHt0wlpNV15vwmswBAUSII= github.com/armon/go-radix v1.0.0 h1:F4z6KzEeeQIMeLFa97iZU6vupzoecKdU5TX24SNppXI= github.com/armon/go-radix v1.0.0/go.mod h1:ufUuZ+zHj4x4TnLV4JWEpy2hxWSpsRywHrMgIH9cCH8= -github.com/asabya/swarm-act v0.0.0-20241022090815-9b494c0051de h1:d5PDY+pU7QCIu3VNSftUq6rc4hNC6GfxH8vI1JItWNU= -github.com/asabya/swarm-act v0.0.0-20241022090815-9b494c0051de/go.mod h1:JKFHSirdVKLWOdJpk7cl8732JiI+a9G64dzB58CEO8w= -github.com/asabya/swarm-blockstore v0.0.0-20241022084926-8d6753f32697 h1:MhH/TMo8jvCafB/WcI8MKajuWuodymKHiLZLGAQ35nE= -github.com/asabya/swarm-blockstore v0.0.0-20241022084926-8d6753f32697/go.mod h1:Tc8wjIjbbV0Ofrr3JPmxDcbmg571M6yFEdKOqWelWgo= +github.com/asabya/swarm-act v0.0.0-20241106102524-234e35a79f95 h1:+wmDLRIO5eY+aSvZprue0wUJg2fr1IHjPLHzEfJd5yQ= +github.com/asabya/swarm-act v0.0.0-20241106102524-234e35a79f95/go.mod h1:dIzdN1DEQVpjvzXVyB1b1RPJWO3ChEHOJzG/TYxD9KU= +github.com/asabya/swarm-blockstore v0.0.0-20241106102400-00f682459f25 h1:sx+0OADMTo5/EaG1PA7/Egt2WLIriCK3fExlVQE5SvE= +github.com/asabya/swarm-blockstore v0.0.0-20241106102400-00f682459f25/go.mod h1:Tc8wjIjbbV0Ofrr3JPmxDcbmg571M6yFEdKOqWelWgo= github.com/beorn7/perks v1.0.1 h1:VlbKKnNfV8bJzeqoa4cOKqO6bYr3WgKZxO8Z16+hsOM= github.com/beorn7/perks v1.0.1/go.mod h1:G2ZrVWU2WbWT9wwq4/hrbKbnv/1ERSJQ0ibhJ6rlkpw= github.com/bits-and-blooms/bitset v1.13.0 h1:bAQ9OPNFYbGHV6Nez0tmNI0RiEu7/hxlYJRUA0wFAVE= diff --git a/pkg/act/act_test.go b/pkg/act/act_test.go index 73122fab..a5530d7a 100644 --- a/pkg/act/act_test.go +++ b/pkg/act/act_test.go @@ -1,4 +1,4 @@ -package act +package act_test import ( "context" @@ -11,6 +11,8 @@ import ( "testing" "time" + "github.com/fairdatasociety/fairOS-dfs/pkg/act" + "github.com/asabya/swarm-blockstore/bee" "github.com/asabya/swarm-blockstore/bee/mock" "github.com/btcsuite/btcd/btcec/v2" @@ -64,7 +66,7 @@ func TestACT(t *testing.T) { acc := accounts[0] fd := feed.New(acc.GetUserAccountInfo(), mockClient, -1, 0, logger) - ownerACT := NewACT(mockClient, fd, acc, tm, logger) + ownerACT := act.NewACT(mockClient, fd, acc, tm, logger) actName := acts[0] for i := 1; i < 10; i++ { acc := accounts[i] @@ -74,11 +76,12 @@ func TestACT(t *testing.T) { } <-time.After(1 * time.Second) } - a, err := ownerACT.GetACT(actName) + _, err := ownerACT.GetACT(actName) if err != nil { t.Fatal(err) } - pubKeys, err := ownerACT.act.GetGrantees(context.Background(), swarm.NewAddress(a.GranteesRef)) + + pubKeys, err := ownerACT.GetGrantees(actName) if err != nil { t.Fatal(err) } @@ -90,7 +93,7 @@ func TestACT(t *testing.T) { acc := accounts[0] fd := feed.New(acc.GetUserAccountInfo(), mockClient, -1, 0, logger) - ownerACT := NewACT(mockClient, fd, acc, tm, logger) + ownerACT := act.NewACT(mockClient, fd, acc, tm, logger) actName := acts[1] for i := 1; i < 2; i++ { acc := accounts[i] @@ -106,7 +109,11 @@ func TestACT(t *testing.T) { if err != nil { t.Fatal(err) } - pubKeys, err := ownerACT.act.GetGrantees(context.Background(), swarm.NewAddress(a.GranteesRef)) + _, err = swarm.ParseHexAddress(a.GranteesRef) + if err != nil { + t.Fatal(err) + } + pubKeys, err := ownerACT.GetGrantees(actName) if err != nil { t.Fatal(err) } @@ -126,8 +133,11 @@ func TestACT(t *testing.T) { if err != nil { t.Fatal(err) } - - pubKeys, err = ownerACT.act.GetGrantees(context.Background(), swarm.NewAddress(a.GranteesRef)) + _, err = swarm.ParseHexAddress(a.GranteesRef) + if err != nil { + t.Fatal(err) + } + pubKeys, err = ownerACT.GetGrantees(actName) if err != nil { t.Fatal(err) } @@ -139,7 +149,7 @@ func TestACT(t *testing.T) { acc := accounts[0] fd := feed.New(acc.GetUserAccountInfo(), mockClient, -1, 0, logger) - ownerACT := NewACT(mockClient, fd, acc, tm, logger) + ownerACT := act.NewACT(mockClient, fd, acc, tm, logger) for _, actName := range acts { for i := 1; i < 10; i++ { acc := accounts[i] @@ -200,7 +210,7 @@ func TestACT(t *testing.T) { t.Fatal(err) } - ownerACT := NewACT(mockClient, fd, ownerAcc, tm, logger) + ownerACT := act.NewACT(mockClient, fd, ownerAcc, tm, logger) granteeAcc := accounts[1] _, err = ownerACT.CreateUpdateACT(acts[0], granteeAcc.GetUserAccountInfo().GetPublicKey(), nil) if err != nil { @@ -215,7 +225,7 @@ func TestACT(t *testing.T) { <-time.After(1 * time.Second) granteeFeed := feed.New(granteeAcc.GetUserAccountInfo(), mockClient, -1, 0, logger) - granteeACT := NewACT(mockClient, granteeFeed, granteeAcc, tm, logger) + granteeACT := act.NewACT(mockClient, granteeFeed, granteeAcc, tm, logger) err = granteeACT.SaveGrantedPod(acts[0], respOne) if err != nil { t.Fatal(err) @@ -237,8 +247,11 @@ func TestACT(t *testing.T) { t.Fatal(err) } <-time.After(time.Second) - - _, err = ownerACT.act.GetGrantees(context.Background(), swarm.NewAddress(actAfterRevoke.GranteesRef)) + addr, err = swarm.ParseHexAddress(actAfterRevoke.GranteesRef) + if err != nil { + t.Fatal(err) + } + _, err = ownerACT.GetGrantees(acts[0]) if err != nil { t.Fatal(err) } @@ -252,7 +265,7 @@ func TestACT(t *testing.T) { ownerAcc := accounts[0] fd := feed.New(ownerAcc.GetUserAccountInfo(), mockClient, -1, 0, logger) pod1 := pod.NewPod(mockClient, fd, ownerAcc, tm, sm, -1, 0, logger) - ownerACT := NewACT(mockClient, fd, ownerAcc, tm, logger) + ownerACT := act.NewACT(mockClient, fd, ownerAcc, tm, logger) granteeAcc := accounts[1] _, err := ownerACT.CreateUpdateACT(acts[1], granteeAcc.GetUserAccountInfo().GetPublicKey(), nil) if err != nil { diff --git a/pkg/act/grant.go b/pkg/act/grant.go index 5310492a..3cfb6783 100644 --- a/pkg/act/grant.go +++ b/pkg/act/grant.go @@ -31,8 +31,11 @@ func (t *ACT) GrantAccess(actName string, address swarm.Address) (*Content, erro return nil, ErrACTDoesNowExist } owner := t.acc.GetUserAccountInfo().GetAddress() - - uploadResp, err := t.act.HandleUpload(context.Background(), address, swarm.NewAddress(act.HistoryRef)) + addr, err := swarm.ParseHexAddress(act.HistoryRef) + if err != nil { + return nil, err + } + uploadResp, err := t.act.HandleUpload(context.Background(), address, addr) if err != nil { return nil, err } @@ -124,8 +127,8 @@ func (t *ACT) SaveGrantedPod(actName string, c *Content) error { a := &Act{ Name: actName, CreatedAt: c.AddedAt, - HistoryRef: swarm.ZeroAddress.Bytes(), - GranteesRef: reference.Bytes(), + HistoryRef: swarm.ZeroAddress.String(), + GranteesRef: reference.String(), Content: []*Content{c}, } list[actName] = a @@ -148,7 +151,11 @@ func (t *ACT) GetGrantees(actName string) ([]string, error) { if !ok { return nil, ErrACTDoesNowExist } - grantees, err := t.act.GetGrantees(context.Background(), swarm.NewAddress(act.GranteesRef)) + addr, err := swarm.ParseHexAddress(act.GranteesRef) + if err != nil { + return nil, err + } + grantees, err := t.act.GetGrantees(context.Background(), addr) if err != nil { return nil, err } diff --git a/pkg/act/new.go b/pkg/act/new.go index 2e615c8b..1980e333 100644 --- a/pkg/act/new.go +++ b/pkg/act/new.go @@ -6,8 +6,8 @@ import ( "fmt" "time" + swarm_act "github.com/asabya/swarm-act" "github.com/btcsuite/btcd/btcec/v2" - "github.com/ethersphere/bee/v2/pkg/api" "github.com/ethersphere/bee/v2/pkg/crypto" "github.com/ethersphere/bee/v2/pkg/swarm" "github.com/fairdatasociety/fairOS-dfs/pkg/utils" @@ -22,8 +22,8 @@ type List map[string]*Act // Act represents an Access Control Trie (ACT) with its metadata, grantees, and associated content. type Act struct { Name string `json:"name"` - HistoryRef []byte `json:"historyRef"` - GranteesRef []byte `json:"granteesRef"` + HistoryRef string `json:"historyRef"` + GranteesRef string `json:"granteesRef"` CreatedAt time.Time `json:"createdAt"` Content []*Content `json:"content"` } @@ -67,7 +67,7 @@ func (t *ACT) CreateUpdateACT(actName string, publicKeyGrant, publicKeyRevoke *e } var ( - resp = &api.GranteesPostResponse{} + resp = &swarm_act.GranteesPostResponse{} grantList []*ecdsa.PublicKey revokeList []*ecdsa.PublicKey owner = t.acc.GetUserAccountInfo().GetAddress() @@ -80,12 +80,16 @@ func (t *ACT) CreateUpdateACT(actName string, publicKeyGrant, publicKeyRevoke *e act = &Act{ Name: actName, CreatedAt: time.Now(), - HistoryRef: swarm.ZeroAddress.Bytes(), - GranteesRef: swarm.ZeroAddress.Bytes(), + HistoryRef: swarm.ZeroAddress.String(), + GranteesRef: swarm.ZeroAddress.String(), Content: []*Content{}, } grantList = []*ecdsa.PublicKey{publicKeyGrant} - resp, err = t.act.CreateGrantee(context.Background(), swarm.NewAddress(act.HistoryRef), grantList) + addr, err := swarm.ParseHexAddress(act.HistoryRef) + if err != nil { + return nil, err + } + resp, err = t.act.CreateGrantee(context.Background(), addr, grantList) if err != nil { return nil, err } @@ -107,8 +111,15 @@ func (t *ACT) CreateUpdateACT(actName string, publicKeyGrant, publicKeyRevoke *e } else { revokeList = nil } - - resp, err = t.act.RevokeGrant(context.Background(), swarm.NewAddress(act.GranteesRef), swarm.NewAddress(act.HistoryRef), grantList, revokeList) + haddr, err := swarm.ParseHexAddress(act.HistoryRef) + if err != nil { + return nil, err + } + gaddr, err := swarm.ParseHexAddress(act.GranteesRef) + if err != nil { + return nil, err + } + resp, err = t.act.RevokeGrant(context.Background(), gaddr, haddr, grantList, revokeList) if err != nil { return nil, err } @@ -118,8 +129,8 @@ func (t *ACT) CreateUpdateACT(actName string, publicKeyGrant, publicKeyRevoke *e } } - act.GranteesRef = resp.Reference.Bytes() - act.HistoryRef = resp.HistoryReference.Bytes() + act.GranteesRef = resp.Reference.String() + act.HistoryRef = resp.HistoryReference.String() list[actName] = act err = t.storeUserACTs(list) diff --git a/pkg/api/act.go b/pkg/api/act.go index 77cc779c..6f7b230f 100644 --- a/pkg/api/act.go +++ b/pkg/api/act.go @@ -31,6 +31,7 @@ type Content struct { // @Produce json // @Param actName path string true "unique act identifier" // @Param grantee query string true "grantee public key" +// @Param Cookie header string true "cookie parameter" // @Success 201 {object} response // @Failure 400 {object} response // @Failure 500 {object} response diff --git a/pkg/dfs/act_api.go b/pkg/dfs/act_api.go index a23965c4..28553f7f 100644 --- a/pkg/dfs/act_api.go +++ b/pkg/dfs/act_api.go @@ -74,7 +74,6 @@ func (a *API) OpenACTPod(sessionId, actName string) error { if err != nil { return err } - info, err := ui.GetPod().ReceivePodInfo(utils.NewReference(addr.Bytes())) if err != nil { return err diff --git a/pkg/dfs/api.go b/pkg/dfs/api.go index c3ff06a7..c2f7cc30 100644 --- a/pkg/dfs/api.go +++ b/pkg/dfs/api.go @@ -79,6 +79,11 @@ func NewDfsAPI(ctx context.Context, opts *Options) (*API, error) { logger.Errorf("dfs: bee client initialisation failed") return nil, errBeeClient } + // Setting cache size 0 will disable the cache. This is to change the default behaviour of lru itself. + // We have this -1 check hard coded in the feed package. -1 will disable the feed pool off. and write directly to swarm. + if opts.FeedCacheSize == 0 { + opts.FeedCacheSize = -1 + } users := user.NewUsers(c, ens, opts.FeedCacheSize, opts.FeedCacheTTL, logger) var sm subscriptionManager.SubscriptionManager @@ -91,12 +96,6 @@ func NewDfsAPI(ctx context.Context, opts *Options) (*API, error) { } } - // Setting cache size 0 will disable the cache. This is to change the default behaviour of lru itself. - // We have this -1 check hard coded in the feed package. -1 will disable the feed pool off. and write directly to swarm. - if opts.FeedCacheSize == 0 { - opts.FeedCacheSize = -1 - } - // discard tm logs as it creates too much noise tmLogger := logging.New(io.Discard, 0) ctx2, cancel := context.WithCancel(ctx) diff --git a/pkg/dfs/pod_api.go b/pkg/dfs/pod_api.go index 972b7c03..e7a737f5 100644 --- a/pkg/dfs/pod_api.go +++ b/pkg/dfs/pod_api.go @@ -51,7 +51,6 @@ func (a *API) CreatePod(podName, sessionId string) (*pod.Info, error) { if ui == nil { return nil, ErrUserNotLoggedIn } - // open the pod pi, err := a.prepareOwnPod(ui, podName) if err != nil { diff --git a/pkg/feed/handler.go b/pkg/feed/handler.go index 3479bdab..7978d120 100644 --- a/pkg/feed/handler.go +++ b/pkg/feed/handler.go @@ -297,7 +297,6 @@ retry: } return epoch, nil, err } - return req.Epoch, address, nil } diff --git a/pkg/pod/new.go b/pkg/pod/new.go index 26bd0aec..a3ea7f1c 100644 --- a/pkg/pod/new.go +++ b/pkg/pod/new.go @@ -42,7 +42,6 @@ func (p *Pod) CreatePod(podName, addressString, podPassword string) (*Info, erro if err != nil { return nil, err } - // check if pods is present and get free index podList, err := p.PodList() if err != nil { // skipcq: TCV-001 @@ -56,7 +55,6 @@ func (p *Pod) CreatePod(podName, addressString, podPassword string) (*Info, erro for _, pod := range podList.SharedPods { sharedPods[pod.Address] = pod.Name } - var accountInfo *account.Info var fd *feed.API var file *f.File diff --git a/swagger/docs.go b/swagger/docs.go index 1ecd8198..f4be7785 100644 --- a/swagger/docs.go +++ b/swagger/docs.go @@ -416,6 +416,13 @@ const docTemplate = `{ "name": "grantee", "in": "query", "required": true + }, + { + "type": "string", + "description": "cookie parameter", + "name": "Cookie", + "in": "header", + "required": true } ], "responses": { @@ -5198,16 +5205,10 @@ const docTemplate = `{ "type": "string" }, "granteesRef": { - "type": "array", - "items": { - "type": "integer" - } + "type": "string" }, "historyRef": { - "type": "array", - "items": { - "type": "integer" - } + "type": "string" }, "name": { "type": "string" diff --git a/swagger/swagger.json b/swagger/swagger.json index 65fccb40..67db802b 100644 --- a/swagger/swagger.json +++ b/swagger/swagger.json @@ -407,6 +407,13 @@ "name": "grantee", "in": "query", "required": true + }, + { + "type": "string", + "description": "cookie parameter", + "name": "Cookie", + "in": "header", + "required": true } ], "responses": { @@ -5189,16 +5196,10 @@ "type": "string" }, "granteesRef": { - "type": "array", - "items": { - "type": "integer" - } + "type": "string" }, "historyRef": { - "type": "array", - "items": { - "type": "integer" - } + "type": "string" }, "name": { "type": "string" diff --git a/swagger/swagger.yaml b/swagger/swagger.yaml index f17e96b3..4123871e 100644 --- a/swagger/swagger.yaml +++ b/swagger/swagger.yaml @@ -8,13 +8,9 @@ definitions: createdAt: type: string granteesRef: - items: - type: integer - type: array + type: string historyRef: - items: - type: integer - type: array + type: string name: type: string type: object @@ -1001,6 +997,11 @@ paths: name: grantee required: true type: string + - description: cookie parameter + in: header + name: Cookie + required: true + type: string produces: - application/json responses: diff --git a/wasm/main.go b/wasm/main.go index de1a27cf..0cbf0d78 100644 --- a/wasm/main.go +++ b/wasm/main.go @@ -6,7 +6,9 @@ import ( "bufio" "bytes" "context" + "crypto/ecdsa" "encoding/base64" + "encoding/hex" "encoding/json" "fmt" "io" @@ -15,7 +17,11 @@ import ( "strings" "syscall/js" + "github.com/ethereum/go-ethereum/crypto" + + "github.com/btcsuite/btcd/btcec/v2" "github.com/dustin/go-humanize" + "github.com/fairdatasociety/fairOS-dfs/pkg/act" "github.com/fairdatasociety/fairOS-dfs/pkg/collection" "github.com/fairdatasociety/fairOS-dfs/pkg/contracts" "github.com/fairdatasociety/fairOS-dfs/pkg/dfs" @@ -141,6 +147,15 @@ func registerWasmFunctions() { js.Global().Set("publicPodFileMeta", js.FuncOf(publicPodFileMeta)) js.Global().Set("publicPodDir", js.FuncOf(publicPodDir)) js.Global().Set("publicPodReceiveInfo", js.FuncOf(publicPodReceiveInfo)) + + js.Global().Set("actCreate", js.FuncOf(actCreate)) + js.Global().Set("actUpdate", js.FuncOf(actUpdate)) + js.Global().Set("actListGrantees", js.FuncOf(actListGrantees)) + js.Global().Set("actSharePod", js.FuncOf(actSharePod)) + js.Global().Set("actList", js.FuncOf(actList)) + js.Global().Set("actListPods", js.FuncOf(actListPods)) + js.Global().Set("actSavePod", js.FuncOf(actSavePod)) + js.Global().Set("actOpenPod", js.FuncOf(actOpenPod)) } func connect(_ js.Value, funcArgs []js.Value) interface{} { @@ -188,6 +203,7 @@ func connect(_ js.Value, funcArgs []js.Value) interface{} { EnsConfig: config, SubscriptionConfig: subConfig, Logger: logger, + FeedCacheSize: 0, } api, err = dfs.NewDfsAPI( ctx, @@ -281,7 +297,7 @@ func login(_ js.Value, funcArgs []js.Value) interface{} { go func() { loginResp, err := api.LoginUserV2(username, password, "") if err != nil { - reject.Invoke(fmt.Sprintf("Failed to create user : %s", err.Error())) + reject.Invoke(fmt.Sprintf("Failed to login : %s", err.Error())) return } ui, nameHash := loginResp.UserInfo, loginResp.NameHash @@ -290,6 +306,7 @@ func login(_ js.Value, funcArgs []js.Value) interface{} { addr := ui.GetAccount().GetUserAccountInfo().GetAddress() object.Set("address", addr.Hex()) object.Set("nameHash", nameHash) + object.Set("publicKey", hex.EncodeToString(crypto.FromECDSAPub(ui.GetAccount().GetUserAccountInfo().GetPublicKey()))) object.Set("sessionId", ui.GetSessionId()) resolve.Invoke(object) @@ -330,6 +347,7 @@ func walletLogin(_ js.Value, funcArgs []js.Value) interface{} { addr := ui.GetAccount().GetUserAccountInfo().GetAddress() object.Set("address", addr.Hex()) object.Set("nameHash", nameHash) + object.Set("publicKey", hex.EncodeToString(crypto.FromECDSAPub(ui.GetAccount().GetUserAccountInfo().GetPublicKey()))) object.Set("sessionId", ui.GetSessionId()) resolve.Invoke(object) }() @@ -367,6 +385,7 @@ func signatureLogin(_ js.Value, funcArgs []js.Value) interface{} { object := js.Global().Get("Object").New() object.Set("user", ui.GetUserName()) object.Set("address", lr.Address) + object.Set("publicKey", lr.PublicKey) object.Set("sessionId", ui.GetSessionId()) resolve.Invoke(object) }() @@ -3377,7 +3396,7 @@ func getSubscriptions(_ js.Value, funcArgs []js.Value) interface{} { subscription.Set("podAddress", v.PodAddress) subscription.Set("validTill", v.ValidTill) subscription.Set("infoLocation", utils.Encode(v.InfoLocation)) - subscriptions.SetIndex(i, js.ValueOf(v)) + subscriptions.SetIndex(i, js.ValueOf(subscription)) } object.Set("subscriptions", subscriptions) @@ -3509,7 +3528,7 @@ func getSubscribablePods(_ js.Value, funcArgs []js.Value) interface{} { subscription.Set("bid", v.Bids) subscription.Set("sells", v.Sells) subscription.Set("reports", v.Reports) - subscriptions.SetIndex(i, js.ValueOf(v)) + subscriptions.SetIndex(i, js.ValueOf(subscription)) } object.Set("subscribablePods", subscriptions) resolve.Invoke(object) @@ -3550,7 +3569,7 @@ func getSubRequests(_ js.Value, funcArgs []js.Value) interface{} { request.Set("buyerNameHash", utils.Encode(v.FdpBuyerNameHash[:])) request.Set("requestHash", utils.Encode(v.RequestHash[:])) request.Set("buyer", v.Buyer.Hex()) - subRequests.SetIndex(i, js.ValueOf(v)) + subRequests.SetIndex(i, js.ValueOf(request)) } object.Set("requests", subRequests) resolve.Invoke(object) @@ -3645,3 +3664,360 @@ func getNameHash(_ js.Value, funcArgs []js.Value) interface{} { promiseConstructor := js.Global().Get("Promise") return promiseConstructor.New(handler) } + +func actCreate(_ js.Value, funcArgs []js.Value) interface{} { + handler := js.FuncOf(func(_ js.Value, args []js.Value) interface{} { + resolve := args[0] + reject := args[1] + if api == nil { + reject.Invoke("not connected to fairOS") + return nil + } + + if len(funcArgs) != 3 { + reject.Invoke("not enough arguments. \"actCreate(sessionId, actName, grantee)\"") + return nil + } + sessionId := funcArgs[0].String() + actName := funcArgs[1].String() + grantee := funcArgs[2].String() + pubk, err := hex.DecodeString(grantee) + if err != nil { + reject.Invoke(fmt.Sprintf("failed to create act : %s", err.Error())) + return nil + } + pub, err := btcec.ParsePubKey(pubk) + if err != nil { + reject.Invoke(fmt.Sprintf("failed to create act : %s", err.Error())) + return nil + } + go func() { + err := api.CreateGranteePublicKey(sessionId, actName, pub.ToECDSA()) + if err != nil { + reject.Invoke(fmt.Sprintf("failed to create act : %s", err.Error())) + return + } + + resolve.Invoke("act created") + }() + + return nil + }) + + promiseConstructor := js.Global().Get("Promise") + return promiseConstructor.New(handler) +} + +func actUpdate(_ js.Value, funcArgs []js.Value) interface{} { + handler := js.FuncOf(func(_ js.Value, args []js.Value) interface{} { + resolve := args[0] + reject := args[1] + if api == nil { + reject.Invoke("not connected to fairOS") + return nil + } + + if len(funcArgs) != 4 { + reject.Invoke("not enough arguments. \"actUpdate(sessionId, actName, grant, revoke)\"") + return nil + } + sessionId := funcArgs[0].String() + actName := funcArgs[1].String() + + grantUser := funcArgs[2].String() + revokeUser := funcArgs[3].String() + if grantUser == "" && revokeUser == "" { + reject.Invoke("grant and revoke user public key cannot be empty") + return nil + } + if grantUser == revokeUser { + reject.Invoke("grant and revoke user public key cannot be same") + return nil + } + var ( + granteePubKey *ecdsa.PublicKey + removePubKey *ecdsa.PublicKey + ) + if grantUser != "" { + pubkg, err := hex.DecodeString(grantUser) + if err != nil { + reject.Invoke(fmt.Sprintf("failed to update act : %s", err.Error())) + return nil + } + pubg, err := btcec.ParsePubKey(pubkg) + if err != nil { + reject.Invoke(fmt.Sprintf("failed to update act : %s", err.Error())) + return nil + } + granteePubKey = pubg.ToECDSA() + } + if revokeUser != "" { + pubkr, err := hex.DecodeString(revokeUser) + if err != nil { + reject.Invoke(fmt.Sprintf("failed to update act : %s", err.Error())) + return nil + } + pubr, err := btcec.ParsePubKey(pubkr) + if err != nil { + reject.Invoke(fmt.Sprintf("failed to update act : %s", err.Error())) + return nil + } + removePubKey = pubr.ToECDSA() + } + + go func() { + err := api.GrantRevokeGranteePublicKey(sessionId, actName, granteePubKey, removePubKey) + if err != nil { + reject.Invoke(fmt.Sprintf("failed to update act : %s", err.Error())) + return + } + + resolve.Invoke("act updated") + }() + + return nil + }) + + promiseConstructor := js.Global().Get("Promise") + return promiseConstructor.New(handler) +} + +func actListGrantees(_ js.Value, funcArgs []js.Value) interface{} { + handler := js.FuncOf(func(_ js.Value, args []js.Value) interface{} { + resolve := args[0] + reject := args[1] + if api == nil { + reject.Invoke("not connected to fairOS") + return nil + } + + if len(funcArgs) != 2 { + reject.Invoke("not enough arguments. \"actListGrantees(sessionId, actName)\"") + return nil + } + sessionId := funcArgs[0].String() + actName := funcArgs[1].String() + + go func() { + grantees, err := api.ListGrantees(sessionId, actName) + if err != nil { + reject.Invoke(fmt.Sprintf("failed to list grantees act : %s", err.Error())) + return + } + object := js.Global().Get("Object").New() + granteeList := js.Global().Get("Array").New(len(grantees)) + object.Set("grantees", granteeList) + + resolve.Invoke(object) + }() + + return nil + }) + + promiseConstructor := js.Global().Get("Promise") + return promiseConstructor.New(handler) +} + +func actSharePod(_ js.Value, funcArgs []js.Value) interface{} { + handler := js.FuncOf(func(_ js.Value, args []js.Value) interface{} { + resolve := args[0] + reject := args[1] + if api == nil { + reject.Invoke("not connected to fairOS") + return nil + } + + if len(funcArgs) != 3 { + reject.Invoke("not enough arguments. \"actSharePod(sessionId, actName, podName)\"") + return nil + } + sessionId := funcArgs[0].String() + actName := funcArgs[1].String() + podName := funcArgs[2].String() + + go func() { + content, err := api.ACTPodShare(sessionId, podName, actName) + if err != nil { + reject.Invoke(fmt.Sprintf("failed to share pod to act : %s", err.Error())) + return + } + object := js.Global().Get("Object").New() + object.Set("reference", content.Reference) + object.Set("topic", base64.StdEncoding.EncodeToString(content.Topic)) + object.Set("owner", content.Owner.String()) + object.Set("publicKey", content.OwnerPublicKey) + + resolve.Invoke(object) + }() + + return nil + }) + + promiseConstructor := js.Global().Get("Promise") + return promiseConstructor.New(handler) +} + +func actList(_ js.Value, funcArgs []js.Value) interface{} { + handler := js.FuncOf(func(_ js.Value, args []js.Value) interface{} { + resolve := args[0] + reject := args[1] + if api == nil { + reject.Invoke("not connected to fairOS") + return nil + } + + if len(funcArgs) != 1 { + reject.Invoke("not enough arguments. \"actList(sessionId)\"") + return nil + } + sessionId := funcArgs[0].String() + + go func() { + list, err := api.GetACTs(sessionId) + if err != nil { + reject.Invoke(fmt.Sprintf("failed to share pod to act : %s", err.Error())) + return + } + object := js.Global().Get("Object").New() + acts := js.Global().Get("Array").New(len(list)) + counter := 0 + for _, v := range list { + act := js.Global().Get("Object").New() + act.Set("name", v.Name) + act.Set("historyRef", v.HistoryRef) + act.Set("granteeRef", v.GranteesRef) + acts.SetIndex(counter, js.ValueOf(act)) + counter++ + } + object.Set("acts", acts) + resolve.Invoke(object) + }() + + return nil + }) + + promiseConstructor := js.Global().Get("Promise") + return promiseConstructor.New(handler) +} + +func actListPods(_ js.Value, funcArgs []js.Value) interface{} { + handler := js.FuncOf(func(_ js.Value, args []js.Value) interface{} { + resolve := args[0] + reject := args[1] + if api == nil { + reject.Invoke("not connected to fairOS") + return nil + } + + if len(funcArgs) != 2 { + reject.Invoke("not enough arguments. \"actList(sessionId, actName)\"") + return nil + } + sessionId := funcArgs[0].String() + actName := funcArgs[1].String() + + go func() { + contents, err := api.GetACTContents(sessionId, actName) + if err != nil { + reject.Invoke(fmt.Sprintf("failed to share pod to act : %s", err.Error())) + return + } + object := js.Global().Get("Object").New() + cntnts := js.Global().Get("Array").New(len(contents)) + for i, v := range contents { + content := js.Global().Get("Object").New() + content.Set("reference", v.Reference) + content.Set("topic", base64.StdEncoding.EncodeToString(v.Topic)) + content.Set("owner", v.Owner.String()) + content.Set("publicKey", v.OwnerPublicKey) + cntnts.SetIndex(i, js.ValueOf(content)) + } + object.Set("contents", cntnts) + resolve.Invoke(object) + }() + + return nil + }) + + promiseConstructor := js.Global().Get("Promise") + return promiseConstructor.New(handler) +} + +func actSavePod(_ js.Value, funcArgs []js.Value) interface{} { + handler := js.FuncOf(func(_ js.Value, args []js.Value) interface{} { + resolve := args[0] + reject := args[1] + if api == nil { + reject.Invoke("not connected to fairOS") + return nil + } + + if len(funcArgs) != 6 { + reject.Invoke("not enough arguments. \"actList(sessionId, actName, reference, topic, owner, ownerPublicKey)\"") + return nil + } + sessionId := funcArgs[0].String() + actName := funcArgs[1].String() + reference := funcArgs[2].String() + topic := funcArgs[3].String() + t, err := base64.StdEncoding.DecodeString(topic) + if err != nil { + reject.Invoke(fmt.Sprintf("failed to save pod to act : %s", err.Error())) + return nil + } + owner := funcArgs[4].String() + ownerPublicKey := funcArgs[5].String() + contentReq := &act.Content{ + Reference: reference, + Topic: t, + Owner: utils.HexToAddress(owner), + OwnerPublicKey: ownerPublicKey, + } + go func() { + err := api.SaveACTPod(sessionId, actName, contentReq) + if err != nil { + reject.Invoke(fmt.Sprintf("failed to save pod to act : %s", err.Error())) + return + } + + resolve.Invoke("pod saved") + }() + + return nil + }) + + promiseConstructor := js.Global().Get("Promise") + return promiseConstructor.New(handler) +} + +func actOpenPod(_ js.Value, funcArgs []js.Value) interface{} { + handler := js.FuncOf(func(_ js.Value, args []js.Value) interface{} { + resolve := args[0] + reject := args[1] + if api == nil { + reject.Invoke("not connected to fairOS") + return nil + } + + if len(funcArgs) != 2 { + reject.Invoke("not enough arguments. \"actOpenPod(sessionId, actName)\"") + return nil + } + sessionId := funcArgs[0].String() + actName := funcArgs[1].String() + go func() { + err := api.OpenACTPod(sessionId, actName) + if err != nil { + reject.Invoke(fmt.Sprintf("failed to open pod to act : %s", err.Error())) + return + } + + resolve.Invoke("pod opened") + }() + + return nil + }) + + promiseConstructor := js.Global().Get("Promise") + return promiseConstructor.New(handler) +}