Created
March 16, 2021 21:36
-
-
Save andrewxhill/81186757fe7abc08e0bef6f45c553faa to your computer and use it in GitHub Desktop.
hello-remote-threads.go
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
module github.com/textileio/hello-go-threads | |
go 1.15 | |
require ( | |
github.com/textileio/go-datastore v0.4.5-0.20200819232101-baa577bf9422 // indirect | |
github.com/textileio/textile v1.0.14 // indirect | |
github.com/textileio/textile/v2 v2.6.5 // indirect | |
github.com/wangjia184/sortedset v0.0.0-20160527075905-f5d03557ba30 // indirect | |
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package main | |
import ( | |
"context" | |
"crypto/rand" | |
"crypto/tls" | |
"fmt" | |
"time" | |
crypto "github.com/libp2p/go-libp2p-crypto" | |
"github.com/textileio/go-threads/api/client" | |
"github.com/textileio/go-threads/core/thread" | |
"github.com/textileio/textile/api/common" | |
"google.golang.org/grpc" | |
"google.golang.org/grpc/credentials" | |
) | |
func getRandomUser() (thread.Identity, error) { | |
privateKey, _, err := crypto.GenerateEd25519Key(rand.Reader) | |
if err != nil { | |
return nil, err | |
} | |
myIdentity := thread.NewLibp2pIdentity(privateKey) | |
return myIdentity, nil | |
} | |
func main() { | |
// Create an API Client | |
creds := credentials.NewTLS(&tls.Config{}) | |
auth := common.Credentials{} | |
opts := []grpc.DialOption{grpc.WithTransportCredentials(creds), grpc.WithPerRPCCredentials(auth)} | |
cli, err := client.NewClient("api.hub.textile.io:443", opts...) | |
if err != nil { | |
panic(err) | |
} | |
userGroupKey := "<key>" | |
userGroupSecret := "<secret>" | |
// Add our user group key to the context | |
keyCtx := common.NewAPIKeyContext(context.Background(), userGroupKey) | |
// Add a signature using our user group secret | |
sigCtx, err := common.CreateAPISigContext(keyCtx, time.Now().Add(time.Minute), userGroupSecret) | |
if err != nil { | |
panic(err) | |
} | |
user, err := getRandomUser() | |
if err != nil { | |
panic(err) | |
} | |
// Generate a new token for the user | |
token, err := cli.GetToken(sigCtx, user) | |
if err != nil { | |
panic(err) | |
} | |
userCtx := thread.NewTokenContext(sigCtx, token) | |
threadID := thread.NewIDV1(thread.Raw, 32) | |
fmt.Println(threadID) | |
err = cli.NewDB(userCtx, threadID) | |
if err != nil { | |
panic(err) | |
} | |
fmt.Println("Success.") | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment