This repository has been archived by the owner on Apr 15, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
170 lines (136 loc) · 4.72 KB
/
main.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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
// +build wasm
package main
import (
"context"
"fmt"
"syscall/js"
logging "github.com/ipfs/go-log"
"github.com/quorumcontrol/tupelo-go-sdk/wasm/jsclient"
"github.com/quorumcontrol/tupelo-go-sdk/wasm/jslibs"
"github.com/quorumcontrol/tupelo-go-sdk/wasm/jspubsub"
"github.com/quorumcontrol/tupelo-go-sdk/wasm/jsstore"
"github.com/quorumcontrol/tupelo-go-sdk/wasm/then"
)
var exitChan chan bool
var clientSingleton *jsclient.JSClient
func init() {
exitChan = make(chan bool)
}
func main() {
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
js.Global().Get("Go").Set("exit", js.FuncOf(func(this js.Value, args []js.Value) interface{} {
exitChan <- true
return nil
}))
js.Global().Set(
"populateLibrary",
js.FuncOf(func(this js.Value, args []js.Value) interface{} {
if len(args) != 2 || !args[0].Truthy() || !args[1].Truthy() {
err := fmt.Errorf("error, must supply a valid object")
panic(err)
}
helperLibs := args[1]
cids := helperLibs.Get("cids")
ipfsBlock := helperLibs.Get("ipfs-block")
if !cids.Truthy() || !ipfsBlock.Truthy() {
err := fmt.Errorf("error, must supply a library object containing cids and ipfs-block")
go fmt.Println(err)
panic(err)
}
jslibs.Cids = cids
jslibs.IpfsBlock = ipfsBlock
jsObj := args[0]
jsObj.Set("generateKey", js.FuncOf(func(this js.Value, args []js.Value) interface{} {
return jsclient.GenerateKey()
}))
jsObj.Set("passPhraseKey", js.FuncOf(func(this js.Value, args []js.Value) interface{} {
return jsclient.PassPhraseKey(args[0], args[1])
}))
jsObj.Set("keyFromPrivateBytes", js.FuncOf(func(this js.Value, args []js.Value) interface{} {
return jsclient.KeyFromPrivateBytes(args[0])
}))
jsObj.Set("ecdsaPubkeyToDid", js.FuncOf(func(this js.Value, args []js.Value) interface{} {
return jsclient.EcdsaPubkeyToDid(args[0])
}))
jsObj.Set("ecdsaPubkeyToAddress", js.FuncOf(func(this js.Value, args []js.Value) interface{} {
return jsclient.EcdsaPubkeyToAddress(args[0])
}))
jsObj.Set("newEmptyTree", js.FuncOf(func(this js.Value, args []js.Value) interface{} {
return jsclient.NewEmptyTree(args[0], args[1])
}))
jsObj.Set("getTip", js.FuncOf(func(this js.Value, args []js.Value) interface{} {
return clientSingleton.GetTip(args[0])
}))
jsObj.Set("getLatest", js.FuncOf(func(this js.Value, args []js.Value) interface{} {
return clientSingleton.GetLatest(args[0])
}))
jsObj.Set("tokenPayloadForTransaction", js.FuncOf(func(this js.Value, args []js.Value) interface{} {
jsOpts := args[0]
return jsclient.TokenPayloadForTransaction(
jsOpts.Get("blockService"),
jsOpts.Get("tip"),
jsOpts.Get("tokenName"),
jsOpts.Get("sendId"),
jsOpts.Get("jsSendTxProof"),
)
}))
jsObj.Set("verifyProof", js.FuncOf(func(this js.Value, args []js.Value) interface{} {
return clientSingleton.VerifyProof(args[0])
}))
jsObj.Set("setLogLevel", js.FuncOf(func(this js.Value, args []js.Value) interface{} {
logging.SetLogLevel(args[0].String(), args[1].String())
return nil
}))
jsObj.Set("startClient", js.FuncOf(func(this js.Value, args []js.Value) interface{} {
t := then.New()
go func() {
// js passes in:
// interface IClientOptions {
// pubsub: IPubSub,
// notaryGroup: Uint8Array // protobuf encoded config.NotaryGroup
// blockService: IBlockService,
// }
jsOpts := args[0]
config, err := jsclient.JsConfigToHumanConfig(jsOpts.Get("notaryGroup"))
if err != nil {
t.Reject(fmt.Errorf("error converting config %w", err))
return
}
store := jsstore.New(jsOpts.Get("blockService"))
bridge := jspubsub.NewPubSubBridge(jsOpts.Get("pubsub"))
cli := jsclient.New(bridge, config, store)
err = cli.Start(ctx)
if err != nil {
t.Reject(err)
return
}
clientSingleton = cli
t.Resolve(true)
}()
return t
}))
jsObj.Set("waitForRound", js.FuncOf(func(this js.Value, args []js.Value) interface{} {
return clientSingleton.WaitForRound()
}))
jsObj.Set("playTransactions", js.FuncOf(func(this js.Value, args []js.Value) interface{} {
// js passes in:
// interface IPlayTransactionOptions {
// privateKey: Uint8Array,
// tip: CID,
// transactions: Uint8Array[], // protobuf encoded array of transactions.Transaction
// }
jsOpts := args[0]
if clientSingleton == nil {
t := then.New()
t.Reject(fmt.Errorf("no client has been started"))
return t
}
return clientSingleton.PlayTransactions(jsOpts.Get("privateKey"), jsOpts.Get("tip"), jsOpts.Get("transactions"))
}))
return jsObj
}),
)
go js.Global().Get("Go").Call("readyResolver")
<-exitChan
}