diff --git a/README.md b/README.md index 38dd95093b98c83220d1af039768d0da110cb0ef..a96835a5c0a7a4c83a3939c16d6f94f0569cb788 100644 --- a/README.md +++ b/README.md @@ -23,9 +23,9 @@ be run using the following command. $ GOOS=js GOARCH=wasm go test ./... ``` -Note, this will fail because `wasm/wasm_js.s` contains commands only recognized +Note, this will fail because `utils/utils_js.s` contains commands only recognized by the Go WebAssembly compiler and for some reason not recognized by the test -runner. To get tests to run, temporarily delete the body of `wasm/wasm_js.s` +runner. To get tests to run, temporarily delete the body of `utils/utils_js.s` during testing. ## Testing @@ -65,7 +65,7 @@ global.Go = class { go: { // ... // func Throw(exception string, message string) - 'gitlab.com/elixxir/xxdk-wasm/wasm.throw': (sp) => { + 'gitlab.com/elixxir/xxdk-wasm/utils.throw': (sp) => { const exception = loadString(sp + 8) const message = loadString(sp + 24) throw globalThis[exception](message) diff --git a/go.mod b/go.mod index 1745ad6574c797af3ae460bbc9efde0905387f66..f01654c7e9c5fabf4c1f488a6cebc2c8ca4b06bb 100644 --- a/go.mod +++ b/go.mod @@ -5,41 +5,55 @@ go 1.17 require ( github.com/pkg/errors v0.9.1 github.com/spf13/jwalterweatherman v1.1.0 - gitlab.com/elixxir/client v1.5.1-0.20220907184817-0409e48f068a + gitlab.com/elixxir/client v1.5.1-0.20220908221453-a8b65cb24385 ) require ( - git.xx.network/elixxir/grpc-web-go-client v0.0.0-20220829220442-4f51c27ab822 // indirect + git.xx.network/elixxir/grpc-web-go-client v0.0.0-20220908162406-67a330f8f67a // indirect github.com/andres-erbsen/clock v0.0.0-20160526145045-9e14626cd129 // indirect github.com/badoux/checkmail v1.2.1 // indirect github.com/cenkalti/backoff/v4 v4.1.3 // indirect github.com/cloudflare/circl v1.2.0 // indirect github.com/desertbit/timer v0.0.0-20180107155436-c41aec40b27f // indirect github.com/elliotchance/orderedmap v1.4.0 // indirect + github.com/fsnotify/fsnotify v1.5.4 // indirect github.com/golang-collections/collections v0.0.0-20130729185459-604e922904d3 // indirect github.com/golang/protobuf v1.5.2 // indirect github.com/gorilla/websocket v1.5.0 // indirect + github.com/hashicorp/hcl v1.0.0 // indirect github.com/improbable-eng/grpc-web v0.15.0 // indirect + github.com/inconshreveable/mousetrap v1.0.0 // indirect github.com/json-iterator/go v1.1.12 // indirect github.com/klauspost/compress v1.11.7 // indirect github.com/klauspost/cpuid/v2 v2.1.0 // indirect github.com/ktr0731/grpc-web-go-client v0.2.8 // indirect + github.com/magiconair/properties v1.8.6 // indirect github.com/mattn/go-isatty v0.0.14 // indirect github.com/mitchellh/go-homedir v1.1.0 // indirect + github.com/mitchellh/mapstructure v1.5.0 // indirect github.com/nfnt/resize v0.0.0-20180221191011-83c6a9932646 // indirect + github.com/pelletier/go-toml v1.9.5 // indirect + github.com/pelletier/go-toml/v2 v2.0.2 // indirect + github.com/pkg/profile v1.6.0 // indirect github.com/rs/cors v1.8.2 // indirect github.com/skip2/go-qrcode v0.0.0-20200617195104-da1b6568686e // indirect github.com/soheilhy/cmux v0.1.5 // indirect + github.com/spf13/afero v1.9.2 // indirect + github.com/spf13/cast v1.5.0 // indirect + github.com/spf13/cobra v1.5.0 // indirect + github.com/spf13/pflag v1.0.5 // indirect + github.com/spf13/viper v1.12.0 // indirect + github.com/subosito/gotenv v1.4.0 // indirect github.com/ttacon/builder v0.0.0-20170518171403-c099f663e1c2 // indirect github.com/ttacon/libphonenumber v1.2.1 // indirect github.com/tyler-smith/go-bip39 v1.1.0 // indirect github.com/zeebo/blake3 v0.2.3 // indirect gitlab.com/elixxir/bloomfilter v0.0.0-20211222005329-7d931ceead6f // indirect - gitlab.com/elixxir/comms v0.0.4-0.20220907184530-d8eec143a1e8 // indirect + gitlab.com/elixxir/comms v0.0.4-0.20220908220232-4755ca5e8bcc // indirect gitlab.com/elixxir/crypto v0.0.7-0.20220902165412-5c5e3e990e84 // indirect gitlab.com/elixxir/ekv v0.2.1-0.20220901224437-ab4cbf94bf8b // indirect gitlab.com/elixxir/primitives v0.0.3-0.20220901220638-1acc75fabdc6 // indirect - gitlab.com/xx_network/comms v0.0.4-0.20220902164216-e3272eb0efac // indirect + gitlab.com/xx_network/comms v0.0.4-0.20220908215521-17222b8efc87 // indirect gitlab.com/xx_network/crypto v0.0.5-0.20220902182733-69aad094b487 // indirect gitlab.com/xx_network/primitives v0.0.4-0.20220809193445-9fc0a5209548 // indirect gitlab.com/xx_network/ring v0.0.3-0.20220222211904-da613960ad93 // indirect @@ -52,5 +66,8 @@ require ( google.golang.org/genproto v0.0.0-20220822174746-9e6da59bd2fc // indirect google.golang.org/grpc v1.49.0 // indirect google.golang.org/protobuf v1.28.1 // indirect + gopkg.in/ini.v1 v1.66.6 // indirect + gopkg.in/yaml.v2 v2.4.0 // indirect + gopkg.in/yaml.v3 v3.0.1 // indirect nhooyr.io/websocket v1.8.7 // indirect ) diff --git a/go.sum b/go.sum index 4fe1fb49442ccebe46f1b54153fecc6db7833b4f..59d50fece8bdf87e632ed0ffd16d0f9412b20e3b 100644 --- a/go.sum +++ b/go.sum @@ -56,6 +56,8 @@ cloud.google.com/go/storage v1.14.0/go.mod h1:GrKmX003DSIwi9o29oFT7YDnHYwZoctc3f dmitri.shuralyov.com/gpu/mtl v0.0.0-20190408044501-666a987793e9/go.mod h1:H6x//7gZCb22OMCxBHrMx7a5I7Hp++hsVxbQ4BYO7hU= git.xx.network/elixxir/grpc-web-go-client v0.0.0-20220829220442-4f51c27ab822 h1:s2Lxh7KcUbACkGKI2SqaGTeaKdO6GCtfU6/F12Q3Hp4= git.xx.network/elixxir/grpc-web-go-client v0.0.0-20220829220442-4f51c27ab822/go.mod h1:GrZ4Fy3YfaNe7RLnai+H+jE+fwqFA90tVmYOpKK90Yg= +git.xx.network/elixxir/grpc-web-go-client v0.0.0-20220908162406-67a330f8f67a h1:qR2z2D4h8S7ks3Mn2y+V/WltXnYhaZH+DZlkn3IuY8A= +git.xx.network/elixxir/grpc-web-go-client v0.0.0-20220908162406-67a330f8f67a/go.mod h1:D5gtpA4jngKUZQjEyua+HLIFteqYTC9b1IJe4QXPmv4= github.com/BurntSushi/toml v0.3.1/go.mod h1:xHWCNGjB5oqiDr8zfno3MHue2Ht5sIBksp03qcyfWMU= github.com/BurntSushi/xgb v0.0.0-20160522181843-27f122750802/go.mod h1:IVnqGOEym/WlBOVXweHU+Q+/VP0lqqI8lqeDx9IjBqo= github.com/DataDog/datadog-go v3.2.0+incompatible/go.mod h1:LButxg5PwREeZtORoXG3tL4fMGNddJ+vMq1mwgfaqoQ= @@ -164,6 +166,7 @@ github.com/franela/goreq v0.0.0-20171204163338-bcd34c9993f8/go.mod h1:ZhphrRTfi2 github.com/frankban/quicktest v1.14.3/go.mod h1:mgiwOwqx65TmIk1wJ6Q7wvnVMocbUorkibMOrVTHZps= github.com/fsnotify/fsnotify v1.4.7/go.mod h1:jwhsz4b93w/PPRr/qN1Yymfu8t87LnFCMoQvtojpjFo= github.com/fsnotify/fsnotify v1.4.9/go.mod h1:znqG4EE+3YCdAaPaxE2ZRY/06pZUdp0tY4IgpuI1SZQ= +github.com/fsnotify/fsnotify v1.5.4 h1:jRbGcIw6P2Meqdwuo0H1p6JVLbL5DHKAKlYndzMwVZI= github.com/fsnotify/fsnotify v1.5.4/go.mod h1:OVB6XrOHzAwXMpEM7uPOzcehqUV2UqJxmVXmkdnm1bU= github.com/ghodss/yaml v1.0.0/go.mod h1:4dBDuWmgqj2HViK6kFavaiC9ZROes6MMH2rRYeMEF04= github.com/gin-contrib/sse v0.1.0 h1:Y/yl/+YNO8GZSjAhjMsSuLt29uWRFHdHYUb5lYOV9qE= @@ -327,6 +330,7 @@ github.com/hashicorp/go.net v0.0.1/go.mod h1:hjKkEWcCURg++eb33jQU7oqQcI9XDCnUzHA github.com/hashicorp/golang-lru v0.5.0/go.mod h1:/m3WP610KZHVQ1SGc6re/UDhFvYD7pJ4Ao+sR/qLZy8= github.com/hashicorp/golang-lru v0.5.1/go.mod h1:/m3WP610KZHVQ1SGc6re/UDhFvYD7pJ4Ao+sR/qLZy8= github.com/hashicorp/golang-lru v0.5.4/go.mod h1:iADmTwqILo4mZ8BN3D2Q6+9jd8WM5uGBxy+E8yxSoD4= +github.com/hashicorp/hcl v1.0.0 h1:0Anlzjpi4vEasTeNFn2mLJgTSwt0+6sfsiTG8qcWGx4= github.com/hashicorp/hcl v1.0.0/go.mod h1:E5yfLk+7swimpb2L/Alb/PJmXilQ/rhwaUYs4T20WEQ= github.com/hashicorp/logutils v1.0.0/go.mod h1:QIAnNjmIWmVIIkWDTG1z5v++HQmx9WQRO+LraFDTW64= github.com/hashicorp/mdns v1.0.0/go.mod h1:tL+uN++7HEJ6SQLQ2/p+z2pH24WQKWjBPkE0mNTz8vQ= @@ -344,6 +348,7 @@ github.com/improbable-eng/grpc-web v0.12.0/go.mod h1:6hRR09jOEG81ADP5wCQju1z71g6 github.com/improbable-eng/grpc-web v0.14.1/go.mod h1:zEjGHa8DAlkoOXmswrNvhUGEYQA9UI7DhrGeHR1DMGU= github.com/improbable-eng/grpc-web v0.15.0 h1:BN+7z6uNXZ1tQGcNAuaU1YjsLTApzkjt2tzCixLaUPQ= github.com/improbable-eng/grpc-web v0.15.0/go.mod h1:1sy9HKV4Jt9aEs9JSnkWlRJPuPtwNr0l57L4f878wP8= +github.com/inconshreveable/mousetrap v1.0.0 h1:Z8tu5sraLXCXIcARxBp/8cbvlwVa7Z1NHg9XEKhtSvM= github.com/inconshreveable/mousetrap v1.0.0/go.mod h1:PxqpIevigyE2G7u3NXJIT2ANytuPF1OarO4DADm73n8= github.com/influxdata/influxdb1-client v0.0.0-20191209144304-8bf82d3c094d/go.mod h1:qj24IKcXYK6Iy9ceXlo3Tc+vtHo9lIhSX5JddghvEPo= github.com/jmespath/go-jmespath v0.0.0-20180206201540-c2b33e8439af/go.mod h1:Nht3zPeWKUH0NzdCt2Blrr5ys8VGpn0CEB0cQHVjt7k= @@ -399,6 +404,7 @@ github.com/lightstep/lightstep-tracer-go v0.18.1/go.mod h1:jlF1pusYV4pidLvZ+XD0U github.com/liyue201/goqr v0.0.0-20200803022322-df443203d4ea h1:uyJ13zfy6l79CM3HnVhDalIyZ4RJAyVfDrbnfFeJoC4= github.com/liyue201/goqr v0.0.0-20200803022322-df443203d4ea/go.mod h1:w4pGU9PkiX2hAWyF0yuHEHmYTQFAd6WHzp6+IY7JVjE= github.com/lyft/protoc-gen-validate v0.0.13/go.mod h1:XbGvPuh87YZc5TdIa2/I4pLk0QoUACkjt2znoq26NVQ= +github.com/magiconair/properties v1.8.6 h1:5ibWZ6iY0NctNGWo87LalDlEZ6R41TqbbDamhfG/Qzo= github.com/magiconair/properties v1.8.6/go.mod h1:y3VJvCyxH9uVvJTWEGAELF3aiYNyPKd5NZ3oSwXrF60= github.com/mattn/go-colorable v0.0.9/go.mod h1:9vuHe8Xs5qXnSaW/c/ABM9alt+Vo+STaOChaDxuIBZU= github.com/mattn/go-colorable v0.1.4/go.mod h1:U0ppj6V5qS13XJ6of8GYAs25YV2eR4EVcfRqFIhoBtE= @@ -431,6 +437,7 @@ github.com/mitchellh/iochan v1.0.0/go.mod h1:JwYml1nuB7xOzsp52dPpHFffvOCDupsG0Qu github.com/mitchellh/mapstructure v0.0.0-20160808181253-ca63d7c062ee/go.mod h1:FVVH3fgwuzCH5S8UJGiWEs2h04kUh9fWfEaFds41c1Y= github.com/mitchellh/mapstructure v1.1.2/go.mod h1:FVVH3fgwuzCH5S8UJGiWEs2h04kUh9fWfEaFds41c1Y= github.com/mitchellh/mapstructure v1.4.3/go.mod h1:bFUtVrKA4DC2yAKiSyO/QUcy7e+RRV2QTWOzhPopBRo= +github.com/mitchellh/mapstructure v1.5.0 h1:jeMsZIYE/09sWLaz43PL7Gy6RuMjD2eJVyuac5Z2hdY= github.com/mitchellh/mapstructure v1.5.0/go.mod h1:bFUtVrKA4DC2yAKiSyO/QUcy7e+RRV2QTWOzhPopBRo= github.com/mitchellh/reflectwalk v1.0.0/go.mod h1:mSTlrgnPZtwu0c4WaC2kGObEpuNDbx0jmZXqmk4esnw= github.com/modern-go/concurrent v0.0.0-20180228061459-e0a39a4cb421/go.mod h1:6dJC0mAP4ikYIbvyc7fijjWJddQyLn8Ig3JB5CqoB9Q= @@ -476,8 +483,10 @@ github.com/pact-foundation/pact-go v1.0.4/go.mod h1:uExwJY4kCzNPcHRj+hCR/HBbOOIw github.com/pascaldekloe/goe v0.0.0-20180627143212-57f6aae5913c/go.mod h1:lzWF7FIEvWOWxwDKqyGYQf6ZUaNfKdP144TG7ZOy1lc= github.com/pascaldekloe/goe v0.1.0/go.mod h1:lzWF7FIEvWOWxwDKqyGYQf6ZUaNfKdP144TG7ZOy1lc= github.com/pborman/uuid v1.2.0/go.mod h1:X/NO0urCmaxf9VXbdlT7C2Yzkj2IKimNn4k+gtPdI/k= +github.com/pelletier/go-toml v1.9.5 h1:4yBQzkHv+7BHq2PQUZF3Mx0IYxG7LsP222s7Agd3ve8= github.com/pelletier/go-toml v1.9.5/go.mod h1:u1nR/EPcESfeI/szUZKdtJ0xRNbUoANCkoOuaOx1Y+c= github.com/pelletier/go-toml/v2 v2.0.1/go.mod h1:r9LEWfGN8R5k0VXJ+0BkIe7MYkRdwZOjgMj2KwnJFUo= +github.com/pelletier/go-toml/v2 v2.0.2 h1:+jQXlF3scKIcSEKkdHzXhCTDLPFi5r1wnK6yPS+49Gw= github.com/pelletier/go-toml/v2 v2.0.2/go.mod h1:MovirKjgVRESsAvNZlAjtFwV867yGuwRkXbG66OzopI= github.com/performancecopilot/speed v3.0.0+incompatible/go.mod h1:/CLtqpZ5gBg1M9iaPbIdPPGyKcA8hKdoy6hAWba7Yac= github.com/pierrec/lz4 v1.0.2-0.20190131084431-473cd7ce01a1/go.mod h1:3/3N9NVKO0jef7pBehbT1qWhCMrIgbYNnFAZCqQ5LRc= @@ -487,6 +496,7 @@ github.com/pkg/errors v0.8.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINE github.com/pkg/errors v0.9.1 h1:FEBLx1zS214owpjy7qsBeixbURkuhQAwrK5UwLGTwt4= github.com/pkg/errors v0.9.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0= github.com/pkg/profile v1.2.1/go.mod h1:hJw3o1OdXxsrSjjVksARp5W95eeEaEfptyVZyv6JUPA= +github.com/pkg/profile v1.6.0 h1:hUDfIISABYI59DyeB3OTay/HxSRwTQ8rB/H83k6r5dM= github.com/pkg/profile v1.6.0/go.mod h1:qBsxPvzyUincmltOk6iyRVxHYg4adc0OFOv72ZdLa18= github.com/pkg/sftp v1.13.1/go.mod h1:3HaPG6Dq1ILlpPZRO0HVMrsydcdLt6HRDccSgb87qRg= github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM= @@ -551,14 +561,19 @@ github.com/sony/gobreaker v0.4.1/go.mod h1:ZKptC7FHNvhBz7dN2LGjPVBz2sZJmc0/PkyDJ github.com/spaolacci/murmur3 v0.0.0-20180118202830-f09979ecbc72/go.mod h1:JwIasOWyU6f++ZhiEuf87xNszmSA2myDM2Kzu9HwQUA= github.com/spaolacci/murmur3 v1.1.0/go.mod h1:JwIasOWyU6f++ZhiEuf87xNszmSA2myDM2Kzu9HwQUA= github.com/spf13/afero v1.8.2/go.mod h1:CtAatgMJh6bJEIs48Ay/FOnkljP3WeGUG0MC1RfAqwo= +github.com/spf13/afero v1.9.2 h1:j49Hj62F0n+DaZ1dDCvhABaPNSGNkt32oRFxI33IEMw= github.com/spf13/afero v1.9.2/go.mod h1:iUV7ddyEEZPO5gA3zD4fJt6iStLlL+Lg4m2cihcDf8Y= +github.com/spf13/cast v1.5.0 h1:rj3WzYc11XZaIZMPKmwP96zkFEnnAmV8s6XbB2aY32w= github.com/spf13/cast v1.5.0/go.mod h1:SpXXQ5YoyJw6s3/6cMTQuxvgRl3PCJiyaX9p6b155UU= github.com/spf13/cobra v0.0.3/go.mod h1:1l0Ry5zgKvJasoi3XT1TypsSe7PqH0Sj9dhYf7v3XqQ= +github.com/spf13/cobra v1.5.0 h1:X+jTBEBqF0bHN+9cSMgmfuvv2VHJ9ezmFNf9Y/XstYU= github.com/spf13/cobra v1.5.0/go.mod h1:dWXEIy2H428czQCjInthrTRUg7yKbok+2Qi/yBIJoUM= github.com/spf13/jwalterweatherman v1.1.0 h1:ue6voC5bR5F8YxI5S67j9i582FU4Qvo2bmqnqMYADFk= github.com/spf13/jwalterweatherman v1.1.0/go.mod h1:aNWZUN0dPAAO/Ljvb5BEdw96iTZ0EXowPYD95IqWIGo= github.com/spf13/pflag v1.0.1/go.mod h1:DYY7MBk1bdzusC3SYhjObp+wFpr4gzcvqqNjLnInEg4= +github.com/spf13/pflag v1.0.5 h1:iy+VFUOCP1a+8yFto/drg2CJ5u0yRoB7fZw3DKv/JXA= github.com/spf13/pflag v1.0.5/go.mod h1:McXfInJRrz4CZXVZOBLb0bTZqETkiAhM9Iw0y3An2Bg= +github.com/spf13/viper v1.12.0 h1:CZ7eSOd3kZoaYDLbXnmzgQI5RlciuXBMA+18HwHRfZQ= github.com/spf13/viper v1.12.0/go.mod h1:b6COn30jlNxbm/V2IqWiNWkJ+vZNiMNksliPCiuKtSI= github.com/streadway/amqp v0.0.0-20190404075320-75d898a42a94/go.mod h1:AZpEONHx3DKn8O/DFsRAY58/XVQiIPMTMB1SddzLXVw= github.com/streadway/amqp v0.0.0-20190827072141-edfb9018d271/go.mod h1:AZpEONHx3DKn8O/DFsRAY58/XVQiIPMTMB1SddzLXVw= @@ -577,6 +592,7 @@ github.com/stretchr/testify v1.7.2/go.mod h1:R6va5+xMeoiuVRoj+gSkQ7d3FALtqAAGI1F github.com/stretchr/testify v1.8.0 h1:pSgiaMZlXftHpm5L7V1+rVB+AZJydKsMxsQBIJw4PKk= github.com/stretchr/testify v1.8.0/go.mod h1:yNjHg4UonilssWZ8iaSj1OCr/vHnekPRkoO+kdMU+MU= github.com/subosito/gotenv v1.3.0/go.mod h1:YzJjq/33h7nrwdY+iHMhEOEEbW0ovIz0tB6t6PwAXzs= +github.com/subosito/gotenv v1.4.0 h1:yAzM1+SmVcz5R4tXGsNMu1jUl2aOJXoiWUCEwwnGrvs= github.com/subosito/gotenv v1.4.0/go.mod h1:mZd6rFysKEcUhUHXJk0C/08wAgyDBFuwEYL7vWWGaGo= github.com/tmc/grpc-websocket-proxy v0.0.0-20170815181823-89b8d40f7ca8/go.mod h1:ncp9v5uamzpCO7NfCPTXjqaC+bZgJeR0sMTm6dMHP7U= github.com/ttacon/builder v0.0.0-20170518171403-c099f663e1c2 h1:5u+EJUQiosu3JFX0XS0qTf5FznsMOzTjGqavBGuCbo0= @@ -613,8 +629,12 @@ gitlab.com/elixxir/bloomfilter v0.0.0-20211222005329-7d931ceead6f h1:yXGvNBqzZwA gitlab.com/elixxir/bloomfilter v0.0.0-20211222005329-7d931ceead6f/go.mod h1:H6jztdm0k+wEV2QGK/KYA+MY9nj9Zzatux/qIvDDv3k= gitlab.com/elixxir/client v1.5.1-0.20220907184817-0409e48f068a h1:l7cNBeWqxN4x6sNKZmG7nPYaSqTcalRPFF1n+V/UGlE= gitlab.com/elixxir/client v1.5.1-0.20220907184817-0409e48f068a/go.mod h1:HvISFfNXL/XCJ6ynSrEwG9kPEp14i87G5y4wyL1H8R0= +gitlab.com/elixxir/client v1.5.1-0.20220908221453-a8b65cb24385 h1:fTpXiV6XOEzAZNiYEU8GJDNxMcjqZrGMRYyKWK9bfec= +gitlab.com/elixxir/client v1.5.1-0.20220908221453-a8b65cb24385/go.mod h1:NNHMHtOHRAOgd6QinkRZoJTNN5j6lI8fDvvnuUEOcv0= gitlab.com/elixxir/comms v0.0.4-0.20220907184530-d8eec143a1e8 h1:xbJBQdMdB+mMsKIVXqx7eAWzZmulA3KdRhlbyAk0NIc= gitlab.com/elixxir/comms v0.0.4-0.20220907184530-d8eec143a1e8/go.mod h1:xE9NKMAxzvTXmyJ5BMjBZFWMPfvQpw3rB7be2VYvU8E= +gitlab.com/elixxir/comms v0.0.4-0.20220908220232-4755ca5e8bcc h1:sD4o87jVG3zX47QDViHim3SR7NyvX/pJL6gqC7IVMrQ= +gitlab.com/elixxir/comms v0.0.4-0.20220908220232-4755ca5e8bcc/go.mod h1:hCC0OOdJI/PQRs9jMCrxKcRmY0fL8Mu5iJGBhsmjHyU= gitlab.com/elixxir/crypto v0.0.0-20200804182833-984246dea2c4/go.mod h1:ucm9SFKJo+K0N2GwRRpaNr+tKXMIOVWzmyUD0SbOu2c= gitlab.com/elixxir/crypto v0.0.3/go.mod h1:ZNgBOblhYToR4m8tj4cMvJ9UsJAUKq+p0gCp07WQmhA= gitlab.com/elixxir/crypto v0.0.7-0.20220902165412-5c5e3e990e84 h1:sQCoZ+w09X/8LjQo9m7bg51IoX4AXSrXBtU8eblM/II= @@ -632,6 +652,8 @@ gitlab.com/elixxir/primitives v0.0.3-0.20220901220638-1acc75fabdc6/go.mod h1:9Bb gitlab.com/xx_network/comms v0.0.0-20200805174823-841427dd5023/go.mod h1:owEcxTRl7gsoM8c3RQ5KAm5GstxrJp5tn+6JfQ4z5Hw= gitlab.com/xx_network/comms v0.0.4-0.20220902164216-e3272eb0efac h1:LitOitWXN/4VNXe4dfR7Kp0reqoYmduVaExrmT1pylg= gitlab.com/xx_network/comms v0.0.4-0.20220902164216-e3272eb0efac/go.mod h1:S5p4aZTz1rpN27E36U+nCQbqw6ZqQfnJNeFS54DnnJ0= +gitlab.com/xx_network/comms v0.0.4-0.20220908215521-17222b8efc87 h1:6MuZhbmRkz3dQ+qLAIBKsVcOFk2KMOSCnBNYNKANrLE= +gitlab.com/xx_network/comms v0.0.4-0.20220908215521-17222b8efc87/go.mod h1:lY9eVavkU2+nMP/eP2zgLrwO3w1gKZjSr7+cvWuJPmA= gitlab.com/xx_network/crypto v0.0.3/go.mod h1:DF2HYvvCw9wkBybXcXAgQMzX+MiGbFPjwt3t17VRqRE= gitlab.com/xx_network/crypto v0.0.4/go.mod h1:+lcQEy+Th4eswFgQDwT0EXKp4AXrlubxalwQFH5O0Mk= gitlab.com/xx_network/crypto v0.0.5-0.20220729193517-1e5e96f39f6e/go.mod h1:/SJf+R75E+QepdTLh0H1/udsovxx2Q5ru34q1v0umKk= @@ -1206,6 +1228,7 @@ gopkg.in/errgo.v2 v2.1.0/go.mod h1:hNsd1EY+bozCKY1Ytp96fpM3vjJbqLJn88ws8XvfDNI= gopkg.in/fsnotify.v1 v1.4.7/go.mod h1:Tz8NjZHkW78fSQdbUxIjBTcgA1z1m8ZHf0WmKUhAMys= gopkg.in/gcfg.v1 v1.2.3/go.mod h1:yesOnuUOFQAhST5vPY4nbZsb/huCgGGXlipJsBn0b3o= gopkg.in/ini.v1 v1.66.4/go.mod h1:pNLf8WUiyNEtQjuu5G5vTm06TEv9tsIgeAvK8hOrP4k= +gopkg.in/ini.v1 v1.66.6 h1:LATuAqN/shcYAOkv3wl2L4rkaKqkcgTBQjOyYDvcPKI= gopkg.in/ini.v1 v1.66.6/go.mod h1:pNLf8WUiyNEtQjuu5G5vTm06TEv9tsIgeAvK8hOrP4k= gopkg.in/resty.v1 v1.12.0/go.mod h1:mDo4pnntr5jdWRML875a/NmxYqAlA73dVijT2AXvQQo= gopkg.in/tomb.v1 v1.0.0-20141024135613-dd632973f1e7/go.mod h1:dt/ZhP58zS4L8KSrWDmTeBkI65Dw0HsyUHuEVlX15mw= diff --git a/main.go b/main.go index 28fcf234b3ac5ac2560e9c501d1aa248e71ebe40..e44e8571e911125ac90bd2438650359442bc81ab 100644 --- a/main.go +++ b/main.go @@ -14,6 +14,7 @@ import ( "github.com/pkg/errors" jww "github.com/spf13/jwalterweatherman" "gitlab.com/elixxir/client/bindings" + "gitlab.com/elixxir/xxdk-wasm/utils" "gitlab.com/elixxir/xxdk-wasm/wasm" "os" "syscall/js" @@ -23,6 +24,10 @@ func main() { fmt.Println("Starting xxDK WebAssembly bindings.") fmt.Printf("Client version %s\n", bindings.GetVersion()) + // utils/array.go + js.Global().Set("Uint8ArrayToBase64", js.FuncOf(utils.Uint8ArrayToBase64)) + js.Global().Set("Base64ToUint8Array", js.FuncOf(utils.Base64ToUint8Array)) + // wasm/backup.go js.Global().Set("NewCmixFromBackup", js.FuncOf(wasm.NewCmixFromBackup)) js.Global().Set("InitializeBackup", js.FuncOf(wasm.InitializeBackup)) @@ -34,7 +39,6 @@ func main() { // wasm/cmix.go js.Global().Set("NewCmix", js.FuncOf(wasm.NewCmix)) js.Global().Set("LoadCmix", js.FuncOf(wasm.LoadCmix)) - js.Global().Set("GetLoadCmix", js.FuncOf(wasm.GetLoadCmix)) // wasm/dummy.go js.Global().Set("NewDummyTrafficManager", @@ -125,7 +129,7 @@ func main() { defer func() { jww.CRITICAL.Printf("Before recover\n") if rec := recover(); rec != nil { - wasm.Throw(wasm.TypeError, errors.Errorf(fmt.Sprintf("%+v", rec))) + utils.Throw(utils.TypeError, errors.Errorf(fmt.Sprintf("%+v", rec))) } jww.CRITICAL.Printf("After recover\n") }() diff --git a/utils/array.go b/utils/array.go new file mode 100644 index 0000000000000000000000000000000000000000..1157dce2a0db2b9796465de198b41ee2efed1104 --- /dev/null +++ b/utils/array.go @@ -0,0 +1,41 @@ +//////////////////////////////////////////////////////////////////////////////// +// Copyright © 2022 xx network SEZC // +// // +// Use of this source code is governed by a license that can be found in the // +// LICENSE file // +//////////////////////////////////////////////////////////////////////////////// + +package utils + +import ( + "encoding/base64" + "syscall/js" +) + +// Uint8ArrayToBase64 encodes an uint8 array to a base 64 string. +// +// Parameters: +// - args[0] - uint8 array (Uint8Array) +// +// Returns: +// - Base 64 encoded string (string). +func Uint8ArrayToBase64(_ js.Value, args []js.Value) interface{} { + return base64.StdEncoding.EncodeToString(CopyBytesToGo(args[0])) +} + +// Base64ToUint8Array decodes a base 64 encoded string to a Uint8Array. +// +// Parameters: +// - args[0] - base 64 encoded string (string) +// +// Returns: +// - Decoded uint8 array (Uint8Array). +// - Throws TypeError if decoding the string fails. +func Base64ToUint8Array(_ js.Value, args []js.Value) interface{} { + b, err := base64.StdEncoding.DecodeString(args[0].String()) + if err != nil { + Throw(TypeError, err) + } + + return CopyBytesToJS(b) +} diff --git a/wasm/utils.go b/utils/utils.go similarity index 68% rename from wasm/utils.go rename to utils/utils.go index 5c7a62ad757f73db07b6997ce2428ea1fd2bca6c..47066401a09ebedbec4c614613e598d3cb5e7dcb 100644 --- a/wasm/utils.go +++ b/utils/utils.go @@ -1,3 +1,10 @@ +//////////////////////////////////////////////////////////////////////////////// +// Copyright © 2020 xx network SEZC // +// // +// Use of this source code is governed by a license that can be found in the // +// LICENSE file // +//////////////////////////////////////////////////////////////////////////////// + //////////////////////////////////////////////////////////////////////////////// // Copyright © 2022 xx foundation // // // @@ -7,7 +14,7 @@ //go:build js && wasm -package wasm +package utils import ( "encoding/json" @@ -61,6 +68,19 @@ func JsonToJS(src []byte) js.Value { return js.ValueOf(inInterface) } +// JsError converts the error to a Javascript Error. +func JsError(err error) js.Value { + errorConstructor := js.Global().Get("Error") + return errorConstructor.New(err.Error()) +} + +// JsTrace converts the error to a Javascript Error that includes the error's +// stack trace. +func JsTrace(err error) js.Value { + errorConstructor := js.Global().Get("Error") + return errorConstructor.New(fmt.Sprintf("%+v", err)) +} + // Throw function stub to throws Javascript exceptions. The exception must be // one of the defined Exception below. Any other error types will result in an // error. @@ -68,6 +88,30 @@ func Throw(exception Exception, err error) { throw(exception, fmt.Sprintf("%+v", err)) } +type PromiseFn func(resolve, reject func(args ...interface{}) js.Value) + +// CreatePromise creates a Javascript promise to return the value of a blocking +// Go function to Javascript. +func CreatePromise(f PromiseFn) interface{} { + // Create handler for promise (this will be a Javascript function) + handler := js.FuncOf(func(this js.Value, args []js.Value) interface{} { + // It receives two arguments, which are JS functions: resolve and reject + resolve := args[0] + reject := args[1] + + // Spawn a new go routine to perform the blocking function + go func() { + f(resolve.Invoke, reject.Invoke) + }() + + return nil + }) + + // Create and return the Promise object + promiseConstructor := js.Global().Get("Promise") + return promiseConstructor.New(handler) +} + func throw(exception Exception, message string) // Exception are the possible Javascript error types that can be thrown. diff --git a/wasm/wasm_js.s b/utils/utils_js.s similarity index 100% rename from wasm/wasm_js.s rename to utils/utils_js.s diff --git a/wasm/authenticatedConnection.go b/wasm/authenticatedConnection.go index ca170f49f9e875561248fc046c70f0aaf48a4d7b..d462797ac75f329c6efdcc4b0964215aa9139d7e 100644 --- a/wasm/authenticatedConnection.go +++ b/wasm/authenticatedConnection.go @@ -11,6 +11,7 @@ package wasm import ( "gitlab.com/elixxir/client/bindings" + "gitlab.com/elixxir/xxdk-wasm/utils" "syscall/js" ) @@ -49,13 +50,13 @@ func (ac *AuthenticatedConnection) IsAuthenticated(js.Value, []js.Value) interfa // - Javascript representation of the Connection object // - throws a TypeError if creating loading the parameters or connecting fails func (c *Cmix) ConnectWithAuthentication(_ js.Value, args []js.Value) interface{} { - recipientContact := CopyBytesToGo(args[1]) - e2eParamsJSON := CopyBytesToGo(args[2]) + recipientContact := utils.CopyBytesToGo(args[1]) + e2eParamsJSON := utils.CopyBytesToGo(args[2]) ac, err := c.api.ConnectWithAuthentication( args[0].Int(), recipientContact, e2eParamsJSON) if err != nil { - Throw(TypeError, err) + utils.Throw(utils.TypeError, err) return nil } diff --git a/wasm/backup.go b/wasm/backup.go index c3bfba7e0fa4d762a2dfcd30cc312e9c9a5178f3..ca3072c0105ffc87b170fe3d71aaeb21ea27bde9 100644 --- a/wasm/backup.go +++ b/wasm/backup.go @@ -11,6 +11,7 @@ package wasm import ( "gitlab.com/elixxir/client/bindings" + "gitlab.com/elixxir/xxdk-wasm/utils" "syscall/js" ) @@ -44,7 +45,7 @@ type updateBackupFunc struct { } func (ubf *updateBackupFunc) UpdateBackup(encryptedBackup []byte) { - ubf.updateBackup(CopyBytesToJS(encryptedBackup)) + ubf.updateBackup(utils.CopyBytesToJS(encryptedBackup)) } //////////////////////////////////////////////////////////////////////////////// @@ -69,17 +70,17 @@ func NewCmixFromBackup(_ js.Value, args []js.Value) interface{} { ndfJSON := args[0].String() storageDir := args[1].String() backupPassphrase := args[2].String() - sessionPassword := CopyBytesToGo(args[3]) - backupFileContents := CopyBytesToGo(args[4]) + sessionPassword := utils.CopyBytesToGo(args[3]) + backupFileContents := utils.CopyBytesToGo(args[4]) report, err := bindings.NewCmixFromBackup(ndfJSON, storageDir, backupPassphrase, sessionPassword, backupFileContents) if err != nil { - Throw(TypeError, err) + utils.Throw(utils.TypeError, err) return nil } - return CopyBytesToJS(report) + return utils.CopyBytesToJS(report) } //////////////////////////////////////////////////////////////////////////////// @@ -101,11 +102,11 @@ func NewCmixFromBackup(_ js.Value, args []js.Value) interface{} { // - Javascript representation of the Backup object // - Throws a TypeError if initializing the Backup fails. func InitializeBackup(_ js.Value, args []js.Value) interface{} { - cb := &updateBackupFunc{WrapCB(args[3], "UpdateBackup")} + cb := &updateBackupFunc{utils.WrapCB(args[3], "UpdateBackup")} api, err := bindings.InitializeBackup( args[0].Int(), args[1].Int(), args[2].String(), cb) if err != nil { - Throw(TypeError, err) + utils.Throw(utils.TypeError, err) return nil } @@ -130,10 +131,10 @@ func InitializeBackup(_ js.Value, args []js.Value) interface{} { // - Javascript representation of the Backup object // - Throws a TypeError if initializing the Backup fails. func ResumeBackup(_ js.Value, args []js.Value) interface{} { - cb := &updateBackupFunc{WrapCB(args[2], "UpdateBackup")} + cb := &updateBackupFunc{utils.WrapCB(args[2], "UpdateBackup")} api, err := bindings.ResumeBackup(args[0].Int(), args[1].Int(), cb) if err != nil { - Throw(TypeError, err) + utils.Throw(utils.TypeError, err) return nil } @@ -148,7 +149,7 @@ func ResumeBackup(_ js.Value, args []js.Value) interface{} { func (b *Backup) StopBackup(js.Value, []js.Value) interface{} { err := b.api.StopBackup() if err != nil { - Throw(TypeError, err) + utils.Throw(utils.TypeError, err) return nil } diff --git a/wasm/broadcast.go b/wasm/broadcast.go index df8bd0851594ee39abca5562f82b2f7e6331cfa3..456027b4c63828ac80578ebecc5eea62dea9b7cf 100644 --- a/wasm/broadcast.go +++ b/wasm/broadcast.go @@ -11,6 +11,7 @@ package wasm import ( "gitlab.com/elixxir/client/bindings" + "gitlab.com/elixxir/xxdk-wasm/utils" "syscall/js" ) @@ -48,11 +49,11 @@ func newChannelJS(api *bindings.Channel) map[string]interface{} { // - Javascript representation of the Channel object. // - Throws a TypeError if creation fails. func NewBroadcastChannel(_ js.Value, args []js.Value) interface{} { - channelDefinition := CopyBytesToGo(args[1]) + channelDefinition := utils.CopyBytesToGo(args[1]) api, err := bindings.NewBroadcastChannel(args[0].Int(), channelDefinition) if err != nil { - Throw(TypeError, err) + utils.Throw(utils.TypeError, err) return nil } @@ -66,7 +67,7 @@ type broadcastListener struct { } func (bl *broadcastListener) Callback(payload []byte, err error) { - bl.callback(CopyBytesToJS(payload), err.Error()) + bl.callback(utils.CopyBytesToJS(payload), err.Error()) } // Listen registers a BroadcastListener for a given method. This allows users to @@ -82,9 +83,9 @@ func (bl *broadcastListener) Callback(payload []byte, err error) { // - Throws a TypeError if registering the listener fails. func (c *Channel) Listen(_ js.Value, args []js.Value) interface{} { err := c.api.Listen( - &broadcastListener{WrapCB(args[0], "Callback")}, args[1].Int()) + &broadcastListener{utils.WrapCB(args[0], "Callback")}, args[1].Int()) if err != nil { - Throw(TypeError, err) + utils.Throw(utils.TypeError, err) return nil } @@ -102,13 +103,13 @@ func (c *Channel) Listen(_ js.Value, args []js.Value) interface{} { // Cmix.WaitForRoundResult to see if the broadcast succeeded (Uint8Array). // - Throws a TypeError if broadcasting fails. func (c *Channel) Broadcast(_ js.Value, args []js.Value) interface{} { - report, err := c.api.Broadcast(CopyBytesToGo(args[0])) + report, err := c.api.Broadcast(utils.CopyBytesToGo(args[0])) if err != nil { - Throw(TypeError, err) + utils.Throw(utils.TypeError, err) return nil } - return CopyBytesToJS(report) + return utils.CopyBytesToJS(report) } // BroadcastAsymmetric sends a given payload over the broadcast channel using @@ -124,13 +125,13 @@ func (c *Channel) Broadcast(_ js.Value, args []js.Value) interface{} { // - Throws a TypeError if broadcasting fails. func (c *Channel) BroadcastAsymmetric(_ js.Value, args []js.Value) interface{} { report, err := c.api.BroadcastAsymmetric( - CopyBytesToGo(args[0]), CopyBytesToGo(args[1])) + utils.CopyBytesToGo(args[0]), utils.CopyBytesToGo(args[1])) if err != nil { - Throw(TypeError, err) + utils.Throw(utils.TypeError, err) return nil } - return CopyBytesToJS(report) + return utils.CopyBytesToJS(report) } // MaxPayloadSize returns the maximum possible payload size which can be @@ -159,11 +160,11 @@ func (c *Channel) MaxAsymmetricPayloadSize(js.Value, []js.Value) interface{} { func (c *Channel) Get(js.Value, []js.Value) interface{} { def, err := c.api.Get() if err != nil { - Throw(TypeError, err) + utils.Throw(utils.TypeError, err) return nil } - return CopyBytesToJS(def) + return utils.CopyBytesToJS(def) } // Stop stops the channel from listening for more messages. diff --git a/wasm/cmix.go b/wasm/cmix.go index 6d4105745c653291c2a78bf43cf1858c0c2d22a1..70c2bed182e13601e14a4fc4e9a224bfe7cfcc2f 100644 --- a/wasm/cmix.go +++ b/wasm/cmix.go @@ -11,6 +11,7 @@ package wasm import ( "gitlab.com/elixxir/client/bindings" + "gitlab.com/elixxir/xxdk-wasm/utils" "syscall/js" ) @@ -74,12 +75,12 @@ func newCmixJS(api *bindings.Cmix) map[string]interface{} { // Returns: // - throws a TypeError if creating new Cmix fails. func NewCmix(_ js.Value, args []js.Value) interface{} { - password := CopyBytesToGo(args[2]) + password := utils.CopyBytesToGo(args[2]) err := bindings.NewCmix( args[0].String(), args[1].String(), password, args[3].String()) if err != nil { - Throw(TypeError, err) + utils.Throw(utils.TypeError, err) return nil } @@ -102,31 +103,23 @@ func NewCmix(_ js.Value, args []js.Value) interface{} { // - args[2] - JSON of [xxdk.CMIXParams] (Uint8Array) // // Returns: -// - Javascript representation of the Cmix object -// - throws a TypeError if creating loading Cmix fails +// - A promise that returns a Javascript representation of the Cmix object. +// - Throws a error if loading Cmix fails. func LoadCmix(_ js.Value, args []js.Value) interface{} { - password := CopyBytesToGo(args[1]) - cmixParamsJSON := CopyBytesToGo(args[2]) + storageDir := args[0].String() + password := utils.CopyBytesToGo(args[1]) + cmixParamsJSON := utils.CopyBytesToGo(args[2]) - go func() { - _, err := bindings.LoadCmix(args[0].String(), password, cmixParamsJSON) + promiseFn := func(resolve, reject func(args ...interface{}) js.Value) { + net, err := bindings.LoadCmix(storageDir, password, cmixParamsJSON) if err != nil { - Throw(TypeError, err) + reject(utils.JsTrace(err)) + } else { + resolve(newCmixJS(net)) } - }() - - return bindings.GetCurrentID() -} + } -// GetLoadCmix returns the cmix object for the ID. -// -// Parameters: -// - args[0] - ID of Cmix object in tracker (int). -// -// Returns: -// - Javascript representation of the Cmix object -func GetLoadCmix(_ js.Value, args []js.Value) interface{} { - return newCmixJS(bindings.GetCmix(args[0].Int())) + return utils.CreatePromise(promiseFn) } // GetID returns the ID for this [bindings.Cmix] in the cmixTracker. diff --git a/wasm/connect.go b/wasm/connect.go index a3ceda0de48d375299c1aa94fd6ecbaa783f6692..779ff4c9ae37f5f5124af5fe0db4ea12f7aa756b 100644 --- a/wasm/connect.go +++ b/wasm/connect.go @@ -11,6 +11,7 @@ package wasm import ( "gitlab.com/elixxir/client/bindings" + "gitlab.com/elixxir/xxdk-wasm/utils" "syscall/js" ) @@ -59,11 +60,11 @@ func (c *Connection) GetID(js.Value, []js.Value) interface{} { // - Javascript representation of the Connection object // - throws a TypeError if creating loading the parameters or connecting fails func (c *Cmix) Connect(_ js.Value, args []js.Value) interface{} { - recipientContact := CopyBytesToGo(args[1]) - e2eParamsJSON := CopyBytesToGo(args[2]) + recipientContact := utils.CopyBytesToGo(args[1]) + e2eParamsJSON := utils.CopyBytesToGo(args[2]) api, err := c.api.Connect(args[0].Int(), recipientContact, e2eParamsJSON) if err != nil { - Throw(TypeError, err) + utils.Throw(utils.TypeError, err) return nil } @@ -86,12 +87,12 @@ func (c *Cmix) Connect(_ js.Value, args []js.Value) interface{} { // cmix.WaitForRoundResult to see if the send succeeded (Uint8Array) // - throws a TypeError if sending fails func (c *Connection) SendE2E(_ js.Value, args []js.Value) interface{} { - sendReport, err := c.api.SendE2E(args[0].Int(), CopyBytesToGo(args[1])) + sendReport, err := c.api.SendE2E(args[0].Int(), utils.CopyBytesToGo(args[1])) if err != nil { - Throw(TypeError, err) + utils.Throw(utils.TypeError, err) return nil } - return CopyBytesToJS(sendReport) + return utils.CopyBytesToJS(sendReport) } // Close deletes this Connection's partner.Manager and releases resources. @@ -101,7 +102,7 @@ func (c *Connection) SendE2E(_ js.Value, args []js.Value) interface{} { func (c *Connection) Close(js.Value, []js.Value) interface{} { err := c.api.Close() if err != nil { - Throw(TypeError, err) + utils.Throw(utils.TypeError, err) return nil } @@ -113,7 +114,7 @@ func (c *Connection) Close(js.Value, []js.Value) interface{} { // Returns: // - bytes of the partner's [id.ID] (Uint8Array) func (c *Connection) GetPartner(js.Value, []js.Value) interface{} { - return CopyBytesToJS(c.api.GetPartner()) + return utils.CopyBytesToJS(c.api.GetPartner()) } // listener adheres to the [bindings.Listener] interface. @@ -122,7 +123,7 @@ type listener struct { name func(args ...interface{}) js.Value } -func (l *listener) Hear(item []byte) { l.hear(CopyBytesToJS(item)) } +func (l *listener) Hear(item []byte) { l.hear(utils.CopyBytesToJS(item)) } func (l *listener) Name() string { return l.name().String() } // RegisterListener is used for E2E reception and allows for reading data sent @@ -137,9 +138,9 @@ func (l *listener) Name() string { return l.name().String() } // - throws a TypeError is registering the listener fails func (c *Connection) RegisterListener(_ js.Value, args []js.Value) interface{} { err := c.api.RegisterListener(args[0].Int(), - &listener{WrapCB(args[1], "Hear"), WrapCB(args[1], "Name")}) + &listener{utils.WrapCB(args[1], "Hear"), utils.WrapCB(args[1], "Name")}) if err != nil { - Throw(TypeError, err) + utils.Throw(utils.TypeError, err) return nil } diff --git a/wasm/delivery.go b/wasm/delivery.go index d2b8d31efa923ada83881fb4e57c48392018ff14..b0f340978e6a92ccb510bc36c60a8875b309c77f 100644 --- a/wasm/delivery.go +++ b/wasm/delivery.go @@ -10,6 +10,7 @@ package wasm import ( + "gitlab.com/elixxir/xxdk-wasm/utils" "syscall/js" ) @@ -21,7 +22,7 @@ type messageDeliveryCallback struct { func (mdc *messageDeliveryCallback) EventCallback( delivered, timedOut bool, roundResults []byte) { - mdc.eventCallback(delivered, timedOut, CopyBytesToJS(roundResults)) + mdc.eventCallback(delivered, timedOut, utils.CopyBytesToJS(roundResults)) } // WaitForRoundResult allows the caller to get notified if the rounds a message @@ -48,12 +49,12 @@ func (mdc *messageDeliveryCallback) EventCallback( // - throws a TypeError if the parameters are invalid or getting round results // fails func (c *Cmix) WaitForRoundResult(_ js.Value, args []js.Value) interface{} { - roundList := CopyBytesToGo(args[0]) - mdc := &messageDeliveryCallback{WrapCB(args[1], "EventCallback")} + roundList := utils.CopyBytesToGo(args[0]) + mdc := &messageDeliveryCallback{utils.WrapCB(args[1], "EventCallback")} err := c.api.WaitForRoundResult(roundList, mdc, args[2].Int()) if err != nil { - Throw(TypeError, err) + utils.Throw(utils.TypeError, err) return nil } diff --git a/wasm/dummy.go b/wasm/dummy.go index 822acd644a2e33632aad75fd369c6cf6a894e8f7..a8400f356be09045da9dcfe3da2234bbd5838fe9 100644 --- a/wasm/dummy.go +++ b/wasm/dummy.go @@ -11,6 +11,7 @@ package wasm import ( "gitlab.com/elixxir/client/bindings" + "gitlab.com/elixxir/xxdk-wasm/utils" "syscall/js" ) @@ -56,7 +57,7 @@ func NewDummyTrafficManager(_ js.Value, args []js.Value) interface{} { dt, err := bindings.NewDummyTrafficManager( args[0].Int(), args[1].Int(), args[2].Int(), args[3].Int()) if err != nil { - Throw(TypeError, err) + utils.Throw(utils.TypeError, err) return nil } @@ -80,7 +81,7 @@ func NewDummyTrafficManager(_ js.Value, args []js.Value) interface{} { func (dt *DummyTraffic) SetStatus(_ js.Value, args []js.Value) interface{} { err := dt.api.SetStatus(args[0].Bool()) if err != nil { - Throw(TypeError, err) + utils.Throw(utils.TypeError, err) return nil } diff --git a/wasm/e2e.go b/wasm/e2e.go index 98203fc778d98eefa0af3fbfb6c0ce518c25e2e8..06d0215437cfccb71140108c0aba4cce19d35efc 100644 --- a/wasm/e2e.go +++ b/wasm/e2e.go @@ -11,6 +11,7 @@ package wasm import ( "gitlab.com/elixxir/client/bindings" + "gitlab.com/elixxir/xxdk-wasm/utils" "syscall/js" ) @@ -91,13 +92,13 @@ func (e *E2e) GetID(js.Value, []js.Value) interface{} { // - Throws a TypeError if logging in fails. func Login(_ js.Value, args []js.Value) interface{} { callbacks := newAuthCallbacks(args[1]) - identity := CopyBytesToGo(args[2]) - e2eParamsJSON := CopyBytesToGo(args[3]) + identity := utils.CopyBytesToGo(args[2]) + e2eParamsJSON := utils.CopyBytesToGo(args[3]) newE2E, err := bindings.Login( args[0].Int(), callbacks, identity, e2eParamsJSON) if err != nil { - Throw(TypeError, err) + utils.Throw(utils.TypeError, err) return nil } @@ -121,13 +122,13 @@ func Login(_ js.Value, args []js.Value) interface{} { // - Throws a TypeError if logging in fails. func LoginEphemeral(_ js.Value, args []js.Value) interface{} { callbacks := newAuthCallbacks(args[1]) - identity := CopyBytesToGo(args[2]) - e2eParamsJSON := CopyBytesToGo(args[3]) + identity := utils.CopyBytesToGo(args[2]) + e2eParamsJSON := utils.CopyBytesToGo(args[3]) newE2E, err := bindings.LoginEphemeral( args[0].Int(), callbacks, identity, e2eParamsJSON) if err != nil { - Throw(TypeError, err) + utils.Throw(utils.TypeError, err) return nil } @@ -139,7 +140,7 @@ func LoginEphemeral(_ js.Value, args []js.Value) interface{} { // Returns: // - Marshalled [contact.Contact] (Uint8Array) func (e *E2e) GetContact(js.Value, []js.Value) interface{} { - return CopyBytesToJS(e.api.GetContact()) + return utils.CopyBytesToJS(e.api.GetContact()) } // GetUdAddressFromNdf retrieve the User Discovery's network address fom the @@ -156,7 +157,7 @@ func (e *E2e) GetUdAddressFromNdf(js.Value, []js.Value) interface{} { // Returns: // - Public certificate in PEM format (Uint8Array) func (e *E2e) GetUdCertFromNdf(js.Value, []js.Value) interface{} { - return CopyBytesToJS(e.api.GetUdCertFromNdf()) + return utils.CopyBytesToJS(e.api.GetUdCertFromNdf()) } // GetUdContactFromNdf assembles the User Discovery's contact file from the data @@ -168,11 +169,11 @@ func (e *E2e) GetUdCertFromNdf(js.Value, []js.Value) interface{} { func (e *E2e) GetUdContactFromNdf(js.Value, []js.Value) interface{} { b, err := e.api.GetUdContactFromNdf() if err != nil { - Throw(TypeError, err) + utils.Throw(utils.TypeError, err) return nil } - return CopyBytesToJS(b) + return utils.CopyBytesToJS(b) } //////////////////////////////////////////////////////////////////////////////// @@ -193,15 +194,15 @@ func newAuthCallbacks(value js.Value) *authCallbacks { a := &authCallbacks{} if value.Get("Request").Type() == js.TypeFunction { - a.request = WrapCB(value, "Request") + a.request = utils.WrapCB(value, "Request") } if value.Get("Confirm").Type() == js.TypeFunction { - a.confirm = WrapCB(value, "Confirm") + a.confirm = utils.WrapCB(value, "Confirm") } if value.Get("Reset").Type() == js.TypeFunction { - a.reset = WrapCB(value, "Reset") + a.reset = utils.WrapCB(value, "Reset") } return a @@ -210,7 +211,7 @@ func newAuthCallbacks(value js.Value) *authCallbacks { func (a *authCallbacks) Request( contact, receptionId []byte, ephemeralId, roundId int64) { if a.request != nil { - a.request(CopyBytesToJS(contact), CopyBytesToJS(receptionId), + a.request(utils.CopyBytesToJS(contact), utils.CopyBytesToJS(receptionId), ephemeralId, roundId) } } @@ -218,7 +219,7 @@ func (a *authCallbacks) Request( func (a *authCallbacks) Confirm( contact, receptionId []byte, ephemeralId, roundId int64) { if a.confirm != nil { - a.confirm(CopyBytesToJS(contact), CopyBytesToJS(receptionId), + a.confirm(utils.CopyBytesToJS(contact), utils.CopyBytesToJS(receptionId), ephemeralId, roundId) } @@ -226,7 +227,7 @@ func (a *authCallbacks) Confirm( func (a *authCallbacks) Reset( contact, receptionId []byte, ephemeralId, roundId int64) { if a.reset != nil { - a.reset(CopyBytesToJS(contact), CopyBytesToJS(receptionId), + a.reset(utils.CopyBytesToJS(contact), utils.CopyBytesToJS(receptionId), ephemeralId, roundId) } } diff --git a/wasm/e2eAuth.go b/wasm/e2eAuth.go index 39158b8d387142f4a4e4b99b7e272b74724b415f..abcff102206eba1f60af4a3ad67b85f9978ea38f 100644 --- a/wasm/e2eAuth.go +++ b/wasm/e2eAuth.go @@ -10,6 +10,7 @@ package wasm import ( + "gitlab.com/elixxir/xxdk-wasm/utils" "syscall/js" ) @@ -36,18 +37,22 @@ import ( // - args[1] - JSON of [fact.FactList] (Uint8Array). // // Returns: -// - ID of the round (int). -// - Throws TypeError if the request fails. +// - A promise that returns the ID of the round (int). +// - Throws error if the request fails. func (e *E2e) Request(_ js.Value, args []js.Value) interface{} { - partnerContact := CopyBytesToGo(args[0]) - factsListJson := CopyBytesToGo(args[1]) - rid, err := e.api.Request(partnerContact, factsListJson) - if err != nil { - Throw(TypeError, err) - return nil + partnerContact := utils.CopyBytesToGo(args[0]) + factsListJson := utils.CopyBytesToGo(args[1]) + + promiseFn := func(resolve, reject func(args ...interface{}) js.Value) { + rid, err := e.api.Request(partnerContact, factsListJson) + if err != nil { + reject(utils.JsTrace(err)) + } else { + resolve(rid) + } } - return rid + return utils.CreatePromise(promiseFn) } // Confirm sends a confirmation for a received request. It can only be called @@ -73,10 +78,10 @@ func (e *E2e) Request(_ js.Value, args []js.Value) interface{} { // - ID of the round (int). // - Throws TypeError if the confirmation fails. func (e *E2e) Confirm(_ js.Value, args []js.Value) interface{} { - partnerContact := CopyBytesToGo(args[0]) + partnerContact := utils.CopyBytesToGo(args[0]) rid, err := e.api.Confirm(partnerContact) if err != nil { - Throw(TypeError, err) + utils.Throw(utils.TypeError, err) return nil } @@ -104,10 +109,10 @@ func (e *E2e) Confirm(_ js.Value, args []js.Value) interface{} { // - ID of the round (int). // - Throws TypeError if the reset fails. func (e *E2e) Reset(_ js.Value, args []js.Value) interface{} { - partnerContact := CopyBytesToGo(args[0]) + partnerContact := utils.CopyBytesToGo(args[0]) rid, err := e.api.Reset(partnerContact) if err != nil { - Throw(TypeError, err) + utils.Throw(utils.TypeError, err) return nil } @@ -129,10 +134,10 @@ func (e *E2e) Reset(_ js.Value, args []js.Value) interface{} { // - ID of the round (int). // - Throws TypeError if the confirmation fails. func (e *E2e) ReplayConfirm(_ js.Value, args []js.Value) interface{} { - partnerContact := CopyBytesToGo(args[0]) + partnerContact := utils.CopyBytesToGo(args[0]) rid, err := e.api.ReplayConfirm(partnerContact) if err != nil { - Throw(TypeError, err) + utils.Throw(utils.TypeError, err) return nil } @@ -154,10 +159,10 @@ func (e *E2e) CallAllReceivedRequests(js.Value, []js.Value) interface{} { // Returns: // - Throws TypeError if the deletion fails. func (e *E2e) DeleteRequest(_ js.Value, args []js.Value) interface{} { - partnerContact := CopyBytesToGo(args[0]) + partnerContact := utils.CopyBytesToGo(args[0]) err := e.api.DeleteRequest(partnerContact) if err != nil { - Throw(TypeError, err) + utils.Throw(utils.TypeError, err) return nil } @@ -171,7 +176,7 @@ func (e *E2e) DeleteRequest(_ js.Value, args []js.Value) interface{} { func (e *E2e) DeleteAllRequests(js.Value, []js.Value) interface{} { err := e.api.DeleteAllRequests() if err != nil { - Throw(TypeError, err) + utils.Throw(utils.TypeError, err) return nil } @@ -185,7 +190,7 @@ func (e *E2e) DeleteAllRequests(js.Value, []js.Value) interface{} { func (e *E2e) DeleteSentRequests(js.Value, []js.Value) interface{} { err := e.api.DeleteSentRequests() if err != nil { - Throw(TypeError, err) + utils.Throw(utils.TypeError, err) return nil } @@ -199,7 +204,7 @@ func (e *E2e) DeleteSentRequests(js.Value, []js.Value) interface{} { func (e *E2e) DeleteReceiveRequests(js.Value, []js.Value) interface{} { err := e.api.DeleteReceiveRequests() if err != nil { - Throw(TypeError, err) + utils.Throw(utils.TypeError, err) return nil } @@ -215,14 +220,14 @@ func (e *E2e) DeleteReceiveRequests(js.Value, []js.Value) interface{} { // - The marshalled bytes of [contact.Contact] (Uint8Array). // - Throws TypeError if getting the received request fails. func (e *E2e) GetReceivedRequest(_ js.Value, args []js.Value) interface{} { - partnerContact := CopyBytesToGo(args[0]) + partnerContact := utils.CopyBytesToGo(args[0]) c, err := e.api.GetReceivedRequest(partnerContact) if err != nil { - Throw(TypeError, err) + utils.Throw(utils.TypeError, err) return nil } - return CopyBytesToJS(c) + return utils.CopyBytesToJS(c) } // VerifyOwnership checks if the received ownership proof is valid. @@ -236,12 +241,12 @@ func (e *E2e) GetReceivedRequest(_ js.Value, args []js.Value) interface{} { // - Returns true if the ownership is valid (boolean) // - Throws TypeError if loading the parameters fails. func (e *E2e) VerifyOwnership(_ js.Value, args []js.Value) interface{} { - receivedContact := CopyBytesToGo(args[0]) - verifiedContact := CopyBytesToGo(args[1]) + receivedContact := utils.CopyBytesToGo(args[0]) + verifiedContact := utils.CopyBytesToGo(args[1]) isValid, err := e.api.VerifyOwnership( receivedContact, verifiedContact, args[2].Int()) if err != nil { - Throw(TypeError, err) + utils.Throw(utils.TypeError, err) return nil } @@ -259,11 +264,11 @@ func (e *E2e) VerifyOwnership(_ js.Value, args []js.Value) interface{} { // Returns: // - Throws TypeError if the [id.ID] cannot be unmarshalled. func (e *E2e) AddPartnerCallback(_ js.Value, args []js.Value) interface{} { - partnerID := CopyBytesToGo(args[0]) + partnerID := utils.CopyBytesToGo(args[0]) callbacks := newAuthCallbacks(args[1]) err := e.api.AddPartnerCallback(partnerID, callbacks) if err != nil { - Throw(TypeError, err) + utils.Throw(utils.TypeError, err) return nil } @@ -279,10 +284,10 @@ func (e *E2e) AddPartnerCallback(_ js.Value, args []js.Value) interface{} { // Returns: // - Throws TypeError if the [id.ID] cannot be unmarshalled. func (e *E2e) DeletePartnerCallback(_ js.Value, args []js.Value) interface{} { - partnerID := CopyBytesToGo(args[0]) + partnerID := utils.CopyBytesToGo(args[0]) err := e.api.DeletePartnerCallback(partnerID) if err != nil { - Throw(TypeError, err) + utils.Throw(utils.TypeError, err) return nil } diff --git a/wasm/e2eHandler.go b/wasm/e2eHandler.go index ed90c0f9b1fa03dcac222e7b91710adb93c74fbc..8d6efc63c5e015936230de093d71cf78c0b9b336 100644 --- a/wasm/e2eHandler.go +++ b/wasm/e2eHandler.go @@ -10,7 +10,7 @@ package wasm import ( - jww "github.com/spf13/jwalterweatherman" + "gitlab.com/elixxir/xxdk-wasm/utils" "syscall/js" ) @@ -19,7 +19,7 @@ import ( // Returns: // - The marshalled bytes of the [id.ID] object (Uint8Array) func (e *E2e) GetReceptionID(js.Value, []js.Value) interface{} { - return CopyBytesToJS(e.api.GetReceptionID()) + return utils.CopyBytesToJS(e.api.GetReceptionID()) } // GetAllPartnerIDs returns a list of all partner IDs that the user has an E2E @@ -31,10 +31,10 @@ func (e *E2e) GetReceptionID(js.Value, []js.Value) interface{} { func (e *E2e) GetAllPartnerIDs(js.Value, []js.Value) interface{} { partnerIDs, err := e.api.GetAllPartnerIDs() if err != nil { - Throw(TypeError, err) + utils.Throw(utils.TypeError, err) return nil } - return CopyBytesToJS(partnerIDs) + return utils.CopyBytesToJS(partnerIDs) } // PayloadSize returns the max payload size for a partitionable E2E message. @@ -84,10 +84,10 @@ func (e *E2e) FirstPartitionSize(js.Value, []js.Value) interface{} { func (e *E2e) GetHistoricalDHPrivkey(js.Value, []js.Value) interface{} { privKey, err := e.api.GetHistoricalDHPrivkey() if err != nil { - Throw(TypeError, err) + utils.Throw(utils.TypeError, err) return nil } - return CopyBytesToJS(privKey) + return utils.CopyBytesToJS(privKey) } // GetHistoricalDHPubkey returns the user's marshalled historical DH public key. @@ -99,10 +99,10 @@ func (e *E2e) GetHistoricalDHPrivkey(js.Value, []js.Value) interface{} { func (e *E2e) GetHistoricalDHPubkey(js.Value, []js.Value) interface{} { pubKey, err := e.api.GetHistoricalDHPubkey() if err != nil { - Throw(TypeError, err) + utils.Throw(utils.TypeError, err) return nil } - return CopyBytesToJS(pubKey) + return utils.CopyBytesToJS(pubKey) } // HasAuthenticatedChannel returns true if an authenticated channel with the @@ -115,9 +115,9 @@ func (e *E2e) GetHistoricalDHPubkey(js.Value, []js.Value) interface{} { // - Existence of authenticated channel (boolean) // - Throws TypeError if unmarshalling the ID or getting the channel fails func (e *E2e) HasAuthenticatedChannel(_ js.Value, args []js.Value) interface{} { - exists, err := e.api.HasAuthenticatedChannel(CopyBytesToGo(args[0])) + exists, err := e.api.HasAuthenticatedChannel(utils.CopyBytesToGo(args[0])) if err != nil { - Throw(TypeError, err) + utils.Throw(utils.TypeError, err) return nil } return exists @@ -133,7 +133,7 @@ func (e *E2e) HasAuthenticatedChannel(_ js.Value, args []js.Value) interface{} { func (e *E2e) RemoveService(_ js.Value, args []js.Value) interface{} { err := e.api.RemoveService(args[0].String()) if err != nil { - Throw(TypeError, err) + utils.Throw(utils.TypeError, err) return nil } @@ -150,26 +150,26 @@ func (e *E2e) RemoveService(_ js.Value, args []js.Value) interface{} { // - args[3] - JSON [e2e.Params] (Uint8Array) // // Returns: -// - JSON of the [bindings.E2ESendReport], which can be passed into -// Cmix.WaitForRoundResult to see if the send succeeded (Uint8Array) -// - Throws TypeError if sending fails +// - A promise that returns the JSON of the [bindings.E2ESendReport], which can +// be passed into Cmix.WaitForRoundResult to see if the send succeeded +// (Uint8Array). +// - Throws error if sending fails. func (e *E2e) SendE2E(_ js.Value, args []js.Value) interface{} { - recipientId := CopyBytesToGo(args[1]) - payload := CopyBytesToGo(args[2]) - e2eParams := CopyBytesToGo(args[3]) + recipientId := utils.CopyBytesToGo(args[1]) + payload := utils.CopyBytesToGo(args[2]) + e2eParams := utils.CopyBytesToGo(args[3]) - go func() { + promiseFn := func(resolve, reject func(args ...interface{}) js.Value) { sendReport, err := e.api.SendE2E( args[0].Int(), recipientId, payload, e2eParams) if err != nil { - Throw(TypeError, err) + reject(utils.JsTrace(err)) + } else { + resolve(utils.CopyBytesToJS(sendReport)) } + } - jww.INFO.Printf("Send report: %+v", sendReport) - }() - - // return CopyBytesToJS(sendReport) - return nil + return utils.CreatePromise(promiseFn) } // processor wraps Javascript callbacks to adhere to the [bindings.Processor] @@ -181,7 +181,7 @@ type processor struct { func (p *processor) Process( message, receptionId []byte, ephemeralId, roundId int64) { - p.process(CopyBytesToJS(message), CopyBytesToJS(receptionId), ephemeralId, + p.process(utils.CopyBytesToJS(message), utils.CopyBytesToJS(receptionId), ephemeralId, roundId) } @@ -206,11 +206,11 @@ func (p *processor) String() string { // Returns: // - Throws TypeError if registering the service fails func (e *E2e) AddService(_ js.Value, args []js.Value) interface{} { - p := &processor{WrapCB(args[1], "Process"), WrapCB(args[1], "String")} + p := &processor{utils.WrapCB(args[1], "Process"), utils.WrapCB(args[1], "String")} err := e.api.AddService(args[0].String(), p) if err != nil { - Throw(TypeError, err) + utils.Throw(utils.TypeError, err) return nil } @@ -230,12 +230,12 @@ func (e *E2e) AddService(_ js.Value, args []js.Value) interface{} { // Returns: // - Throws TypeError if registering the service fails func (e *E2e) RegisterListener(_ js.Value, args []js.Value) interface{} { - recipientId := CopyBytesToGo(args[0]) - l := &listener{WrapCB(args[2], "Hear"), WrapCB(args[2], "Name")} + recipientId := utils.CopyBytesToGo(args[0]) + l := &listener{utils.WrapCB(args[2], "Hear"), utils.WrapCB(args[2], "Name")} err := e.api.RegisterListener(recipientId, args[1].Int(), l) if err != nil { - Throw(TypeError, err) + utils.Throw(utils.TypeError, err) return nil } diff --git a/wasm/errors.go b/wasm/errors.go index 4aae7fe7ec5709b0dbd7d06f9d93677ebb1186b1..a22cdb4cd6422f309ccc45732df86174667e7ea2 100644 --- a/wasm/errors.go +++ b/wasm/errors.go @@ -11,6 +11,7 @@ package wasm import ( "gitlab.com/elixxir/client/bindings" + "gitlab.com/elixxir/xxdk-wasm/utils" "syscall/js" ) @@ -51,7 +52,7 @@ func CreateUserFriendlyErrorMessage(_ js.Value, args []js.Value) interface{} { func UpdateCommonErrors(_ js.Value, args []js.Value) interface{} { err := bindings.UpdateCommonErrors(args[0].String()) if err != nil { - Throw(TypeError, err) + utils.Throw(utils.TypeError, err) return nil } diff --git a/wasm/fileTransfer.go b/wasm/fileTransfer.go index 52d4a82c41b8d4ef072800bb601400b1e2108364..1a7d7953f29f42d54f569f75caf6547bce3f4a0c 100644 --- a/wasm/fileTransfer.go +++ b/wasm/fileTransfer.go @@ -11,6 +11,7 @@ package wasm import ( "gitlab.com/elixxir/client/bindings" + "gitlab.com/elixxir/xxdk-wasm/utils" "syscall/js" ) @@ -55,7 +56,7 @@ type receiveFileCallback struct { } func (rfc *receiveFileCallback) Callback(payload []byte, err error) { - rfc.callback(CopyBytesToJS(payload), err.Error()) + rfc.callback(utils.CopyBytesToJS(payload), err.Error()) } // fileTransferSentProgressCallback wraps Javascript callbacks to adhere to the @@ -66,7 +67,7 @@ type fileTransferSentProgressCallback struct { func (spc *fileTransferSentProgressCallback) Callback( payload []byte, t *bindings.FilePartTracker, err error) { - spc.callback(CopyBytesToJS(payload), newFilePartTrackerJS(t), err.Error()) + spc.callback(utils.CopyBytesToJS(payload), newFilePartTrackerJS(t), err.Error()) } // fileTransferReceiveProgressCallback wraps Javascript callbacks to adhere to @@ -77,7 +78,7 @@ type fileTransferReceiveProgressCallback struct { func (rpc *fileTransferReceiveProgressCallback) Callback( payload []byte, t *bindings.FilePartTracker, err error) { - rpc.callback(CopyBytesToJS(payload), newFilePartTrackerJS(t), err.Error()) + rpc.callback(utils.CopyBytesToJS(payload), newFilePartTrackerJS(t), err.Error()) } //////////////////////////////////////////////////////////////////////////////// @@ -97,14 +98,14 @@ func (rpc *fileTransferReceiveProgressCallback) Callback( // - Javascript representation of the FileTransfer object. // - Throws a TypeError initialising the file transfer manager fails. func InitFileTransfer(_ js.Value, args []js.Value) interface{} { - rfc := &receiveFileCallback{WrapCB(args[1], "Callback")} - e2eFileTransferParamsJson := CopyBytesToGo(args[2]) - fileTransferParamsJson := CopyBytesToGo(args[3]) + rfc := &receiveFileCallback{utils.WrapCB(args[1], "Callback")} + e2eFileTransferParamsJson := utils.CopyBytesToGo(args[2]) + fileTransferParamsJson := utils.CopyBytesToGo(args[3]) api, err := bindings.InitFileTransfer( args[0].Int(), rfc, e2eFileTransferParamsJson, fileTransferParamsJson) if err != nil { - Throw(TypeError, err) + utils.Throw(utils.TypeError, err) return nil } @@ -126,18 +127,18 @@ func InitFileTransfer(_ js.Value, args []js.Value) interface{} { // - A unique ID for this file transfer (Uint8Array). // - Throws a TypeError if sending fails. func (f *FileTransfer) Send(_ js.Value, args []js.Value) interface{} { - payload := CopyBytesToGo(args[0]) - recipientID := CopyBytesToGo(args[1]) + payload := utils.CopyBytesToGo(args[0]) + recipientID := utils.CopyBytesToGo(args[1]) retry := float32(args[2].Float()) - spc := &fileTransferSentProgressCallback{WrapCB(args[3], "Callback")} + spc := &fileTransferSentProgressCallback{utils.WrapCB(args[3], "Callback")} ftID, err := f.api.Send(payload, recipientID, retry, spc, args[4].String()) if err != nil { - Throw(TypeError, err) + utils.Throw(utils.TypeError, err) return nil } - return CopyBytesToJS(ftID) + return utils.CopyBytesToJS(ftID) } // Receive returns the full file on the completion of the transfer. It deletes @@ -156,13 +157,13 @@ func (f *FileTransfer) Send(_ js.Value, args []js.Value) interface{} { // - Throws a TypeError the file transfer is incomplete or Receive has already // been called. func (f *FileTransfer) Receive(_ js.Value, args []js.Value) interface{} { - file, err := f.api.Receive(CopyBytesToGo(args[0])) + file, err := f.api.Receive(utils.CopyBytesToGo(args[0])) if err != nil { - Throw(TypeError, err) + utils.Throw(utils.TypeError, err) return nil } - return CopyBytesToJS(file) + return utils.CopyBytesToJS(file) } // CloseSend deletes a file from the internal storage once a transfer has @@ -178,9 +179,9 @@ func (f *FileTransfer) Receive(_ js.Value, args []js.Value) interface{} { // Returns: // - Throws a TypeError if the file transfer is incomplete. func (f *FileTransfer) CloseSend(_ js.Value, args []js.Value) interface{} { - err := f.api.CloseSend(CopyBytesToGo(args[0])) + err := f.api.CloseSend(utils.CopyBytesToGo(args[0])) if err != nil { - Throw(TypeError, err) + utils.Throw(utils.TypeError, err) return nil } @@ -208,12 +209,12 @@ func (f *FileTransfer) CloseSend(_ js.Value, args []js.Value) interface{} { // - Throws a TypeError if registering the callback fails. func (f *FileTransfer) RegisterSentProgressCallback( _ js.Value, args []js.Value) interface{} { - tidBytes := CopyBytesToGo(args[0]) - spc := &fileTransferSentProgressCallback{WrapCB(args[1], "Callback")} + tidBytes := utils.CopyBytesToGo(args[0]) + spc := &fileTransferSentProgressCallback{utils.WrapCB(args[1], "Callback")} err := f.api.RegisterSentProgressCallback(tidBytes, spc, args[2].String()) if err != nil { - Throw(TypeError, err) + utils.Throw(utils.TypeError, err) return nil } @@ -236,13 +237,13 @@ func (f *FileTransfer) RegisterSentProgressCallback( // - Throws a TypeError if registering the callback fails. func (f *FileTransfer) RegisterReceivedProgressCallback( _ js.Value, args []js.Value) interface{} { - tidBytes := CopyBytesToGo(args[0]) - rpc := &fileTransferReceiveProgressCallback{WrapCB(args[1], "Callback")} + tidBytes := utils.CopyBytesToGo(args[0]) + rpc := &fileTransferReceiveProgressCallback{utils.WrapCB(args[1], "Callback")} err := f.api.RegisterReceivedProgressCallback( tidBytes, rpc, args[2].String()) if err != nil { - Throw(TypeError, err) + utils.Throw(utils.TypeError, err) return nil } diff --git a/wasm/follow.go b/wasm/follow.go index 634ea5ceff23e4043540896fe5392fb0fac06c76..8da6a4d72b192590b0d8e84b861d1804b785940a 100644 --- a/wasm/follow.go +++ b/wasm/follow.go @@ -10,6 +10,7 @@ package wasm import ( + "gitlab.com/elixxir/xxdk-wasm/utils" "syscall/js" ) @@ -55,7 +56,7 @@ import ( func (c *Cmix) StartNetworkFollower(_ js.Value, args []js.Value) interface{} { err := c.api.StartNetworkFollower(args[0].Int()) if err != nil { - Throw(TypeError, err) + utils.Throw(utils.TypeError, err) return nil } @@ -73,7 +74,7 @@ func (c *Cmix) StartNetworkFollower(_ js.Value, args []js.Value) interface{} { func (c *Cmix) StopNetworkFollower(js.Value, []js.Value) interface{} { err := c.api.StopNetworkFollower() if err != nil { - Throw(TypeError, err) + utils.Throw(utils.TypeError, err) return nil } @@ -115,11 +116,11 @@ func (c *Cmix) NetworkFollowerStatus(js.Value, []js.Value) interface{} { func (c *Cmix) GetNodeRegistrationStatus(js.Value, []js.Value) interface{} { b, err := c.api.GetNodeRegistrationStatus() if err != nil { - Throw(TypeError, err) + utils.Throw(utils.TypeError, err) return nil } - return CopyBytesToJS(b) + return utils.CopyBytesToJS(b) } // HasRunningProcessies checks if any background threads are running and returns @@ -163,7 +164,7 @@ func (nhc *networkHealthCallback) Callback(health bool) { nhc.callback(health) } // - Returns a registration ID that can be used to unregister (int) func (c *Cmix) AddHealthCallback(_ js.Value, args []js.Value) interface{} { return c.api.AddHealthCallback( - &networkHealthCallback{WrapCB(args[0], "Callback")}) + &networkHealthCallback{utils.WrapCB(args[0], "Callback")}) } // RemoveHealthCallback removes a health callback using its registration ID. @@ -192,6 +193,6 @@ func (ce *clientError) Report(source, message, trace string) { // - args[0] - Javascript object that has functions that implement the // [bindings.ClientError] interface func (c *Cmix) RegisterClientErrorCallback(_ js.Value, args []js.Value) interface{} { - c.api.RegisterClientErrorCallback(&clientError{WrapCB(args[0], "Report")}) + c.api.RegisterClientErrorCallback(&clientError{utils.WrapCB(args[0], "Report")}) return nil } diff --git a/wasm/group.go b/wasm/group.go index 5158e4658e021289bd4155976d0fd638be71dd3f..d19da538da36abca4fde43c428529976f6b97eba 100644 --- a/wasm/group.go +++ b/wasm/group.go @@ -11,6 +11,7 @@ package wasm import ( "gitlab.com/elixxir/client/bindings" + "gitlab.com/elixxir/xxdk-wasm/utils" "syscall/js" ) @@ -55,13 +56,13 @@ func newGroupChatJS(api *bindings.GroupChat) map[string]interface{} { // - Javascript representation of the GroupChat object. // - Throws a TypeError if creating the GroupChat fails. func NewGroupChat(_ js.Value, args []js.Value) interface{} { - requestFunc := &groupRequest{WrapCB(args[1], "Callback")} + requestFunc := &groupRequest{utils.WrapCB(args[1], "Callback")} p := &groupChatProcessor{ - WrapCB(args[2], "Process"), WrapCB(args[2], "String")} + utils.WrapCB(args[2], "Process"), utils.WrapCB(args[2], "String")} api, err := bindings.NewGroupChat(args[0].Int(), requestFunc, p) if err != nil { - Throw(TypeError, err) + utils.Throw(utils.TypeError, err) return nil } @@ -87,17 +88,17 @@ func NewGroupChat(_ js.Value, args []js.Value) interface{} { // - Throws a TypeError if making the group fails. func (g *GroupChat) MakeGroup(_ js.Value, args []js.Value) interface{} { // (membershipBytes, message, name []byte) ([]byte, error) - membershipBytes := CopyBytesToGo(args[0]) - message := CopyBytesToGo(args[1]) - name := CopyBytesToGo(args[2]) + membershipBytes := utils.CopyBytesToGo(args[0]) + message := utils.CopyBytesToGo(args[1]) + name := utils.CopyBytesToGo(args[2]) report, err := g.api.MakeGroup(membershipBytes, message, name) if err != nil { - Throw(TypeError, err) + utils.Throw(utils.TypeError, err) return nil } - return CopyBytesToJS(report) + return utils.CopyBytesToJS(report) } // ResendRequest resends a group request to all members in the group. @@ -112,13 +113,13 @@ func (g *GroupChat) MakeGroup(_ js.Value, args []js.Value) interface{} { // succeeded. // - Throws a TypeError if resending the request fails. func (g *GroupChat) ResendRequest(_ js.Value, args []js.Value) interface{} { - report, err := g.api.ResendRequest(CopyBytesToGo(args[0])) + report, err := g.api.ResendRequest(utils.CopyBytesToGo(args[0])) if err != nil { - Throw(TypeError, err) + utils.Throw(utils.TypeError, err) return nil } - return CopyBytesToJS(report) + return utils.CopyBytesToJS(report) } // JoinGroup allows a user to join a group when a request is received. @@ -134,7 +135,7 @@ func (g *GroupChat) ResendRequest(_ js.Value, args []js.Value) interface{} { func (g *GroupChat) JoinGroup(_ js.Value, args []js.Value) interface{} { err := g.api.JoinGroup(args[0].Int()) if err != nil { - Throw(TypeError, err) + utils.Throw(utils.TypeError, err) return nil } @@ -150,9 +151,9 @@ func (g *GroupChat) JoinGroup(_ js.Value, args []js.Value) interface{} { // Returns: // - Throws a TypeError if leaving the group fails. func (g *GroupChat) LeaveGroup(_ js.Value, args []js.Value) interface{} { - err := g.api.LeaveGroup(CopyBytesToGo(args[0])) + err := g.api.LeaveGroup(utils.CopyBytesToGo(args[0])) if err != nil { - Throw(TypeError, err) + utils.Throw(utils.TypeError, err) return nil } @@ -173,16 +174,16 @@ func (g *GroupChat) LeaveGroup(_ js.Value, args []js.Value) interface{} { // - JSON of [bindings.GroupSendReport] (Uint8Array), which can be passed into // Cmix.WaitForRoundResult to see if the group message send succeeded. func (g *GroupChat) Send(_ js.Value, args []js.Value) interface{} { - groupId := CopyBytesToGo(args[0]) - message := CopyBytesToGo(args[1]) + groupId := utils.CopyBytesToGo(args[0]) + message := utils.CopyBytesToGo(args[1]) report, err := g.api.Send(groupId, message, args[2].String()) if err != nil { - Throw(TypeError, err) + utils.Throw(utils.TypeError, err) return nil } - return CopyBytesToJS(report) + return utils.CopyBytesToJS(report) } // GetGroups returns a list of group IDs that the user is a member of. @@ -194,11 +195,11 @@ func (g *GroupChat) GetGroups(js.Value, []js.Value) interface{} { // () ([]byte, error) groups, err := g.api.GetGroups() if err != nil { - Throw(TypeError, err) + utils.Throw(utils.TypeError, err) return nil } - return CopyBytesToJS(groups) + return utils.CopyBytesToJS(groups) } // GetGroup returns the group with the group ID. If no group exists, then the @@ -212,9 +213,9 @@ func (g *GroupChat) GetGroups(js.Value, []js.Value) interface{} { // - Javascript representation of the Group object. // - Throws a TypeError if getting the group fails func (g *GroupChat) GetGroup(_ js.Value, args []js.Value) interface{} { - grp, err := g.api.GetGroup(CopyBytesToGo(args[0])) + grp, err := g.api.GetGroup(utils.CopyBytesToGo(args[0])) if err != nil { - Throw(TypeError, err) + utils.Throw(utils.TypeError, err) return nil } @@ -262,7 +263,7 @@ func newGroupJS(api *bindings.Group) map[string]interface{} { // Returns: // - Uint8Array func (g *Group) GetName(js.Value, []js.Value) interface{} { - return CopyBytesToJS(g.api.GetName()) + return utils.CopyBytesToJS(g.api.GetName()) } // GetID return the 33-byte unique group ID. This represents the id.ID object. @@ -270,7 +271,7 @@ func (g *Group) GetName(js.Value, []js.Value) interface{} { // Returns: // - Uint8Array func (g *Group) GetID(js.Value, []js.Value) interface{} { - return CopyBytesToJS(g.api.GetID()) + return utils.CopyBytesToJS(g.api.GetID()) } // GetTrackedID returns the tracked ID of the Group object. This is used by the @@ -287,7 +288,7 @@ func (g *Group) GetTrackedID(js.Value, []js.Value) interface{} { // Returns: // - Uint8Array func (g *Group) GetInitMessage(js.Value, []js.Value) interface{} { - return CopyBytesToJS(g.api.GetInitMessage()) + return utils.CopyBytesToJS(g.api.GetInitMessage()) } // GetCreatedNano returns the time the group was created in nanoseconds. This is @@ -318,11 +319,11 @@ func (g *Group) GetCreatedMS(js.Value, []js.Value) interface{} { func (g *Group) GetMembership(js.Value, []js.Value) interface{} { membership, err := g.api.GetMembership() if err != nil { - Throw(TypeError, err) + utils.Throw(utils.TypeError, err) return nil } - return CopyBytesToJS(membership) + return utils.CopyBytesToJS(membership) } // Serialize serializes the Group. @@ -330,7 +331,7 @@ func (g *Group) GetMembership(js.Value, []js.Value) interface{} { // Returns: // - Byte representation of the Group (Uint8Array). func (g *Group) Serialize(js.Value, []js.Value) interface{} { - return CopyBytesToJS(g.api.Serialize()) + return utils.CopyBytesToJS(g.api.Serialize()) } //////////////////////////////////////////////////////////////////////////////// @@ -356,8 +357,8 @@ type groupChatProcessor struct { func (gcp *groupChatProcessor) Process(decryptedMessage, msg, receptionId []byte, ephemeralId, roundId int64, err error) { - gcp.callback(CopyBytesToJS(decryptedMessage), CopyBytesToJS(msg), - CopyBytesToJS(receptionId), ephemeralId, roundId, err.Error()) + gcp.callback(utils.CopyBytesToJS(decryptedMessage), utils.CopyBytesToJS(msg), + utils.CopyBytesToJS(receptionId), ephemeralId, roundId, err.Error()) } func (gcp *groupChatProcessor) String() string { diff --git a/wasm/identity.go b/wasm/identity.go index eb8fadf47b0a8d9fe14e847a1a922ab7f2429c7e..c94de98d55f6352a9846871f74afb9e3b870944e 100644 --- a/wasm/identity.go +++ b/wasm/identity.go @@ -12,6 +12,7 @@ package wasm import ( "gitlab.com/elixxir/client/bindings" "gitlab.com/elixxir/client/xxdk" + "gitlab.com/elixxir/xxdk-wasm/utils" "syscall/js" ) @@ -32,12 +33,12 @@ import ( // Returns: // - throws a TypeError if the identity cannot be stored in storage func StoreReceptionIdentity(_ js.Value, args []js.Value) interface{} { - identity := CopyBytesToGo(args[1]) + identity := utils.CopyBytesToGo(args[1]) err := bindings.StoreReceptionIdentity( args[0].String(), identity, args[2].Int()) if err != nil { - Throw(TypeError, err) + utils.Throw(utils.TypeError, err) return nil } @@ -57,11 +58,11 @@ func StoreReceptionIdentity(_ js.Value, args []js.Value) interface{} { func LoadReceptionIdentity(_ js.Value, args []js.Value) interface{} { ri, err := bindings.LoadReceptionIdentity(args[0].String(), args[1].Int()) if err != nil { - Throw(TypeError, err) + utils.Throw(utils.TypeError, err) return nil } - return CopyBytesToJS(ri) + return utils.CopyBytesToJS(ri) } // MakeReceptionIdentity generates a new cryptographic identity for receiving @@ -73,11 +74,11 @@ func LoadReceptionIdentity(_ js.Value, args []js.Value) interface{} { func (c *Cmix) MakeReceptionIdentity(js.Value, []js.Value) interface{} { ri, err := c.api.MakeReceptionIdentity() if err != nil { - Throw(TypeError, err) + utils.Throw(utils.TypeError, err) return nil } - return CopyBytesToJS(ri) + return utils.CopyBytesToJS(ri) } // MakeLegacyReceptionIdentity generates the legacy identity for receiving @@ -89,11 +90,11 @@ func (c *Cmix) MakeReceptionIdentity(js.Value, []js.Value) interface{} { func (c *Cmix) MakeLegacyReceptionIdentity(js.Value, []js.Value) interface{} { ri, err := c.api.MakeLegacyReceptionIdentity() if err != nil { - Throw(TypeError, err) + utils.Throw(utils.TypeError, err) return nil } - return CopyBytesToJS(ri) + return utils.CopyBytesToJS(ri) } // GetReceptionRegistrationValidationSignature returns the signature provided by @@ -103,7 +104,7 @@ func (c *Cmix) MakeLegacyReceptionIdentity(js.Value, []js.Value) interface{} { // - signature (Uint8Array) func (c *Cmix) GetReceptionRegistrationValidationSignature( js.Value, []js.Value) interface{} { - return CopyBytesToJS(c.api.GetReceptionRegistrationValidationSignature()) + return utils.CopyBytesToJS(c.api.GetReceptionRegistrationValidationSignature()) } //////////////////////////////////////////////////////////////////////////////// @@ -121,14 +122,14 @@ func (c *Cmix) GetReceptionRegistrationValidationSignature( // - Throws a TypeError if unmarshalling the identity fails func GetContactFromReceptionIdentity(_ js.Value, args []js.Value) interface{} { // Note that this function does not appear in normal bindings - identityJSON := CopyBytesToGo(args[0]) + identityJSON := utils.CopyBytesToGo(args[0]) identity, err := xxdk.UnmarshalReceptionIdentity(identityJSON) if err != nil { - Throw(TypeError, err) + utils.Throw(utils.TypeError, err) return nil } - return CopyBytesToJS(identity.GetContact().Marshal()) + return utils.CopyBytesToJS(identity.GetContact().Marshal()) } // GetIDFromContact returns the ID in the [contact.Contact] object. @@ -142,11 +143,11 @@ func GetContactFromReceptionIdentity(_ js.Value, args []js.Value) interface{} { func GetIDFromContact(_ js.Value, args []js.Value) interface{} { cID, err := bindings.GetIDFromContact([]byte(args[0].String())) if err != nil { - Throw(TypeError, err) + utils.Throw(utils.TypeError, err) return nil } - return CopyBytesToJS(cID) + return utils.CopyBytesToJS(cID) } // GetPubkeyFromContact returns the DH public key in the [contact.Contact] @@ -161,11 +162,11 @@ func GetIDFromContact(_ js.Value, args []js.Value) interface{} { func GetPubkeyFromContact(_ js.Value, args []js.Value) interface{} { key, err := bindings.GetPubkeyFromContact([]byte(args[0].String())) if err != nil { - Throw(TypeError, err) + utils.Throw(utils.TypeError, err) return nil } - return CopyBytesToJS(key) + return utils.CopyBytesToJS(key) } //////////////////////////////////////////////////////////////////////////////// @@ -183,11 +184,11 @@ func GetPubkeyFromContact(_ js.Value, args []js.Value) interface{} { // - marshalled bytes of the modified [contact.Contact] (string) // - throws a TypeError if loading or modifying the contact fails func SetFactsOnContact(_ js.Value, args []js.Value) interface{} { - marshaledContact := CopyBytesToGo(args[0]) - factListJSON := CopyBytesToGo(args[1]) + marshaledContact := utils.CopyBytesToGo(args[0]) + factListJSON := utils.CopyBytesToGo(args[1]) c, err := bindings.SetFactsOnContact(marshaledContact, factListJSON) if err != nil { - Throw(TypeError, err) + utils.Throw(utils.TypeError, err) return nil } @@ -203,11 +204,11 @@ func SetFactsOnContact(_ js.Value, args []js.Value) interface{} { // - JSON of [fact.FactList] (Uint8Array) // - throws a TypeError if loading the contact fails func GetFactsFromContact(_ js.Value, args []js.Value) interface{} { - fl, err := bindings.GetFactsFromContact(CopyBytesToGo(args[0])) + fl, err := bindings.GetFactsFromContact(utils.CopyBytesToGo(args[0])) if err != nil { - Throw(TypeError, err) + utils.Throw(utils.TypeError, err) return nil } - return CopyBytesToJS(fl) + return utils.CopyBytesToJS(fl) } diff --git a/wasm/logging.go b/wasm/logging.go index 24901db500f2c44bdf63019be0d17e8d65a2d502..e309addce9185edb6ec255abb0ffaf1d34ad12f6 100644 --- a/wasm/logging.go +++ b/wasm/logging.go @@ -13,6 +13,7 @@ import ( "github.com/pkg/errors" jww "github.com/spf13/jwalterweatherman" "gitlab.com/elixxir/client/bindings" + "gitlab.com/elixxir/xxdk-wasm/utils" "io" "log" "syscall/js" @@ -42,7 +43,7 @@ func LogLevel(_ js.Value, args []js.Value) interface{} { level := args[0].Int() if level < 0 || level > 6 { err := errors.Errorf("log level is not valid: log level: %d", level) - Throw(TypeError, err) + utils.Throw(utils.TypeError, err) return nil } diff --git a/wasm/ndf.go b/wasm/ndf.go index 9f1b3e9f1c56fe0e2ccf72efb0cb47167545cb90..7e00d3d988973c444afef910876cbecc67606fe5 100644 --- a/wasm/ndf.go +++ b/wasm/ndf.go @@ -11,6 +11,7 @@ package wasm import ( "gitlab.com/elixxir/client/bindings" + "gitlab.com/elixxir/xxdk-wasm/utils" "syscall/js" ) @@ -28,9 +29,9 @@ func DownloadAndVerifySignedNdfWithUrl(_ js.Value, args []js.Value) interface{} ndf, err := bindings.DownloadAndVerifySignedNdfWithUrl( args[0].String(), args[1].String()) if err != nil { - Throw(TypeError, err) + utils.Throw(utils.TypeError, err) return nil } - return CopyBytesToJS(ndf) + return utils.CopyBytesToJS(ndf) } diff --git a/wasm/params.go b/wasm/params.go index c63d0c7bacc501e9c77b5209bd141b31e83320c8..accafbc3166e80f6d489510e7cc5f9fc0beabae2 100644 --- a/wasm/params.go +++ b/wasm/params.go @@ -11,6 +11,7 @@ package wasm import ( "gitlab.com/elixxir/client/bindings" + "gitlab.com/elixxir/xxdk-wasm/utils" "syscall/js" ) @@ -21,7 +22,7 @@ import ( // Returns: // - JSON of [xxdk.CMIXParams] (Uint8Array) func GetDefaultCMixParams(js.Value, []js.Value) interface{} { - return CopyBytesToJS(bindings.GetDefaultCMixParams()) + return utils.CopyBytesToJS(bindings.GetDefaultCMixParams()) } // GetDefaultE2EParams returns a JSON serialized object with all of the E2E @@ -31,7 +32,7 @@ func GetDefaultCMixParams(js.Value, []js.Value) interface{} { // Returns: // - JSON of [xxdk.E2EParams] (Uint8Array) func GetDefaultE2EParams(js.Value, []js.Value) interface{} { - return CopyBytesToJS(bindings.GetDefaultE2EParams()) + return utils.CopyBytesToJS(bindings.GetDefaultE2EParams()) } // GetDefaultFileTransferParams returns a JSON serialized object with all the @@ -41,7 +42,7 @@ func GetDefaultE2EParams(js.Value, []js.Value) interface{} { // Returns: // - JSON of [fileTransfer.Params] (Uint8Array) func GetDefaultFileTransferParams(js.Value, []js.Value) interface{} { - return CopyBytesToJS(bindings.GetDefaultFileTransferParams()) + return utils.CopyBytesToJS(bindings.GetDefaultFileTransferParams()) } // GetDefaultSingleUseParams returns a JSON serialized object with all the @@ -51,7 +52,7 @@ func GetDefaultFileTransferParams(js.Value, []js.Value) interface{} { // Returns: // - JSON of [single.RequestParams] (Uint8Array) func GetDefaultSingleUseParams(js.Value, []js.Value) interface{} { - return CopyBytesToJS(bindings.GetDefaultSingleUseParams()) + return utils.CopyBytesToJS(bindings.GetDefaultSingleUseParams()) } // GetDefaultE2eFileTransferParams returns a JSON serialized object with all the @@ -61,5 +62,5 @@ func GetDefaultSingleUseParams(js.Value, []js.Value) interface{} { // Returns: // - JSON of [fileTransfer.e2e.Params] (Uint8Array) func GetDefaultE2eFileTransferParams(js.Value, []js.Value) interface{} { - return CopyBytesToJS(bindings.GetDefaultSingleUseParams()) + return utils.CopyBytesToJS(bindings.GetDefaultSingleUseParams()) } diff --git a/wasm/restlike.go b/wasm/restlike.go index 1833abd1721d580bed12c77ba343c2199d035dab..a12d8ee19b5e6e5501174a88f94ae40ce345f955 100644 --- a/wasm/restlike.go +++ b/wasm/restlike.go @@ -11,6 +11,7 @@ package wasm import ( "gitlab.com/elixxir/client/bindings" + "gitlab.com/elixxir/xxdk-wasm/utils" "syscall/js" ) @@ -28,17 +29,17 @@ import ( func RestlikeRequest(_ js.Value, args []js.Value) interface{} { cmixId := args[0].Int() connectionID := args[1].Int() - request := CopyBytesToGo(args[2]) - e2eParamsJSON := CopyBytesToGo(args[3]) + request := utils.CopyBytesToGo(args[2]) + e2eParamsJSON := utils.CopyBytesToGo(args[3]) msg, err := bindings.RestlikeRequest( cmixId, connectionID, request, e2eParamsJSON) if err != nil { - Throw(TypeError, err) + utils.Throw(utils.TypeError, err) return nil } - return CopyBytesToJS(msg) + return utils.CopyBytesToJS(msg) } // RestlikeRequestAuth performs an authenticated restlike request. @@ -55,15 +56,15 @@ func RestlikeRequest(_ js.Value, args []js.Value) interface{} { func RestlikeRequestAuth(_ js.Value, args []js.Value) interface{} { cmixId := args[0].Int() authConnectionID := args[1].Int() - request := CopyBytesToGo(args[2]) - e2eParamsJSON := CopyBytesToGo(args[3]) + request := utils.CopyBytesToGo(args[2]) + e2eParamsJSON := utils.CopyBytesToGo(args[3]) msg, err := bindings.RestlikeRequestAuth( cmixId, authConnectionID, request, e2eParamsJSON) if err != nil { - Throw(TypeError, err) + utils.Throw(utils.TypeError, err) return nil } - return CopyBytesToJS(msg) + return utils.CopyBytesToJS(msg) } diff --git a/wasm/restlikeSingle.go b/wasm/restlikeSingle.go index a3c546fd1ccf361d6534ed66e885d8cbdbccb476..bcc5e93a74d67028b0489a2e5148d1c0323b307f 100644 --- a/wasm/restlikeSingle.go +++ b/wasm/restlikeSingle.go @@ -11,6 +11,7 @@ package wasm import ( "gitlab.com/elixxir/client/bindings" + "gitlab.com/elixxir/xxdk-wasm/utils" "syscall/js" ) @@ -21,7 +22,7 @@ type restlikeCallback struct { } func (rlc *restlikeCallback) Callback(payload []byte, err error) { - rlc.callback(CopyBytesToJS(payload), err.Error()) + rlc.callback(utils.CopyBytesToJS(payload), err.Error()) } // RequestRestLike sends a restlike request to a given contact. @@ -37,17 +38,17 @@ func (rlc *restlikeCallback) Callback(payload []byte, err error) { // - Throws a TypeError if parsing the parameters or making the request fails. func RequestRestLike(_ js.Value, args []js.Value) interface{} { e2eID := args[0].Int() - recipient := CopyBytesToGo(args[1]) - request := CopyBytesToGo(args[2]) - paramsJSON := CopyBytesToGo(args[3]) + recipient := utils.CopyBytesToGo(args[1]) + request := utils.CopyBytesToGo(args[2]) + paramsJSON := utils.CopyBytesToGo(args[3]) msg, err := bindings.RequestRestLike(e2eID, recipient, request, paramsJSON) if err != nil { - Throw(TypeError, err) + utils.Throw(utils.TypeError, err) return nil } - return CopyBytesToJS(msg) + return utils.CopyBytesToJS(msg) } // AsyncRequestRestLike sends an asynchronous restlike request to a given @@ -68,15 +69,15 @@ func RequestRestLike(_ js.Value, args []js.Value) interface{} { // - Throws a TypeError if parsing the parameters or making the request fails. func AsyncRequestRestLike(_ js.Value, args []js.Value) interface{} { e2eID := args[0].Int() - recipient := CopyBytesToGo(args[1]) - request := CopyBytesToGo(args[2]) - paramsJSON := CopyBytesToGo(args[3]) - cb := &restlikeCallback{WrapCB(args[4], "Callback")} + recipient := utils.CopyBytesToGo(args[1]) + request := utils.CopyBytesToGo(args[2]) + paramsJSON := utils.CopyBytesToGo(args[3]) + cb := &restlikeCallback{utils.WrapCB(args[4], "Callback")} err := bindings.AsyncRequestRestLike( e2eID, recipient, request, paramsJSON, cb) if err != nil { - Throw(TypeError, err) + utils.Throw(utils.TypeError, err) return nil } diff --git a/wasm/secrets.go b/wasm/secrets.go index f604141e1b9fbdb8052b600ac2b034a51f4039e8..5a7b7127af4cfa5d5292f941553580fc672c086c 100644 --- a/wasm/secrets.go +++ b/wasm/secrets.go @@ -11,6 +11,7 @@ package wasm import ( "gitlab.com/elixxir/client/bindings" + "gitlab.com/elixxir/xxdk-wasm/utils" "syscall/js" ) @@ -25,5 +26,5 @@ import ( // - secret password (Uint8Array). func GenerateSecret(_ js.Value, args []js.Value) interface{} { secret := bindings.GenerateSecret(args[0].Int()) - return CopyBytesToJS(secret) + return utils.CopyBytesToJS(secret) } diff --git a/wasm/single.go b/wasm/single.go index 3f40bf613bf15ba4fc63d328136a64897a0c66be..7a55ffa7a876b5ab60b42d683ce7e852357f5cc3 100644 --- a/wasm/single.go +++ b/wasm/single.go @@ -11,6 +11,7 @@ package wasm import ( "gitlab.com/elixxir/client/bindings" + "gitlab.com/elixxir/xxdk-wasm/utils" "syscall/js" ) @@ -36,20 +37,20 @@ import ( // - Throws a TypeError if transmission fails. func TransmitSingleUse(_ js.Value, args []js.Value) interface{} { e2eID := args[0].Int() - recipient := CopyBytesToGo(args[1]) + recipient := utils.CopyBytesToGo(args[1]) tag := args[2].String() - payload := CopyBytesToGo(args[3]) - paramsJSON := CopyBytesToGo(args[4]) - responseCB := &singleUseResponse{WrapCB(args[5], "Callback")} + payload := utils.CopyBytesToGo(args[3]) + paramsJSON := utils.CopyBytesToGo(args[4]) + responseCB := &singleUseResponse{utils.WrapCB(args[5], "Callback")} report, err := bindings.TransmitSingleUse( e2eID, recipient, tag, payload, paramsJSON, responseCB) if err != nil { - Throw(TypeError, err) + utils.Throw(utils.TypeError, err) return nil } - return CopyBytesToJS(report) + return utils.CopyBytesToJS(report) } // Listen starts a single-use listener on a given tag using the passed in E2e @@ -67,10 +68,10 @@ func TransmitSingleUse(_ js.Value, args []js.Value) interface{} { // function used to stop the listener. // - Throws a TypeError if listening fails. func Listen(_ js.Value, args []js.Value) interface{} { - cb := &singleUseCallback{WrapCB(args[2], "Callback")} + cb := &singleUseCallback{utils.WrapCB(args[2], "Callback")} api, err := bindings.Listen(args[0].Int(), args[1].String(), cb) if err != nil { - Throw(TypeError, err) + utils.Throw(utils.TypeError, err) return nil } @@ -114,7 +115,7 @@ type singleUseCallback struct { } func (suc *singleUseCallback) Callback(callbackReport []byte, err error) { - suc.callback(CopyBytesToJS(callbackReport), err.Error()) + suc.callback(utils.CopyBytesToJS(callbackReport), err.Error()) } // singleUseResponse wraps Javascript callbacks to adhere to the @@ -124,5 +125,5 @@ type singleUseResponse struct { } func (sur *singleUseResponse) Callback(responseReport []byte, err error) { - sur.callback(CopyBytesToJS(responseReport), err.Error()) + sur.callback(utils.CopyBytesToJS(responseReport), err.Error()) } diff --git a/wasm/ud.go b/wasm/ud.go index 17627eeb6a4bcf5af32fea34d1d51dd973caf648..1c0198a753bfe5c0c2f11ccdd0ef3f06f6cd9618 100644 --- a/wasm/ud.go +++ b/wasm/ud.go @@ -11,6 +11,7 @@ package wasm import ( "gitlab.com/elixxir/client/bindings" + "gitlab.com/elixxir/xxdk-wasm/utils" "syscall/js" ) @@ -97,17 +98,17 @@ func (uns *udNetworkStatus) UdNetworkStatus() int { // - Throws a TypeError if creating or loading fails. func NewOrLoadUd(_ js.Value, args []js.Value) interface{} { e2eID := args[0].Int() - follower := &udNetworkStatus{WrapCB(args[1], "UdNetworkStatus")} + follower := &udNetworkStatus{utils.WrapCB(args[1], "UdNetworkStatus")} username := args[2].String() - registrationValidationSignature := CopyBytesToGo(args[3]) - cert := CopyBytesToGo(args[4]) - contactFile := CopyBytesToGo(args[5]) + registrationValidationSignature := utils.CopyBytesToGo(args[3]) + cert := utils.CopyBytesToGo(args[4]) + contactFile := utils.CopyBytesToGo(args[5]) address := args[6].String() api, err := bindings.NewOrLoadUd(e2eID, follower, username, registrationValidationSignature, cert, contactFile, address) if err != nil { - Throw(TypeError, err) + utils.Throw(utils.TypeError, err) return nil } @@ -149,18 +150,18 @@ func NewOrLoadUd(_ js.Value, args []js.Value) interface{} { // - Throws a TypeError if getting UD from backup fails. func NewUdManagerFromBackup(_ js.Value, args []js.Value) interface{} { e2eID := args[0].Int() - follower := &udNetworkStatus{WrapCB(args[1], "UdNetworkStatus")} - usernameFactJson := CopyBytesToGo(args[2]) - emailFactJson := CopyBytesToGo(args[3]) - phoneFactJson := CopyBytesToGo(args[4]) - cert := CopyBytesToGo(args[5]) - contactFile := CopyBytesToGo(args[6]) + follower := &udNetworkStatus{utils.WrapCB(args[1], "UdNetworkStatus")} + usernameFactJson := utils.CopyBytesToGo(args[2]) + emailFactJson := utils.CopyBytesToGo(args[3]) + phoneFactJson := utils.CopyBytesToGo(args[4]) + cert := utils.CopyBytesToGo(args[5]) + contactFile := utils.CopyBytesToGo(args[6]) address := args[7].String() api, err := bindings.NewUdManagerFromBackup(e2eID, follower, usernameFactJson, emailFactJson, phoneFactJson, cert, contactFile, address) if err != nil { - Throw(TypeError, err) + utils.Throw(utils.TypeError, err) return nil } @@ -173,7 +174,7 @@ func NewUdManagerFromBackup(_ js.Value, args []js.Value) interface{} { // Returns: // - JSON of [fact.FactList] (Uint8Array). func (ud *UserDiscovery) GetFacts(js.Value, []js.Value) interface{} { - return CopyBytesToJS(ud.api.GetFacts()) + return utils.CopyBytesToJS(ud.api.GetFacts()) } // GetContact returns the marshalled bytes of the [contact.Contact] for UD as @@ -185,11 +186,11 @@ func (ud *UserDiscovery) GetFacts(js.Value, []js.Value) interface{} { func (ud *UserDiscovery) GetContact(js.Value, []js.Value) interface{} { c, err := ud.api.GetContact() if err != nil { - Throw(TypeError, err) + utils.Throw(utils.TypeError, err) return nil } - return CopyBytesToJS(c) + return utils.CopyBytesToJS(c) } // ConfirmFact confirms a fact first registered via SendRegisterFact. The @@ -205,7 +206,7 @@ func (ud *UserDiscovery) GetContact(js.Value, []js.Value) interface{} { func (ud *UserDiscovery) ConfirmFact(_ js.Value, args []js.Value) interface{} { err := ud.api.ConfirmFact(args[0].String(), args[1].String()) if err != nil { - Throw(TypeError, err) + utils.Throw(utils.TypeError, err) return nil } @@ -228,9 +229,9 @@ func (ud *UserDiscovery) ConfirmFact(_ js.Value, args []js.Value) interface{} { // - The confirmation ID (string). // - Throws TypeError if sending the fact fails. func (ud *UserDiscovery) SendRegisterFact(_ js.Value, args []js.Value) interface{} { - confirmationID, err := ud.api.SendRegisterFact(CopyBytesToGo(args[0])) + confirmationID, err := ud.api.SendRegisterFact(utils.CopyBytesToGo(args[0])) if err != nil { - Throw(TypeError, err) + utils.Throw(utils.TypeError, err) return nil } @@ -247,9 +248,9 @@ func (ud *UserDiscovery) SendRegisterFact(_ js.Value, args []js.Value) interface // Returns: // - Throws TypeError if deletion fails. func (ud *UserDiscovery) PermanentDeleteAccount(_ js.Value, args []js.Value) interface{} { - err := ud.api.PermanentDeleteAccount(CopyBytesToGo(args[0])) + err := ud.api.PermanentDeleteAccount(utils.CopyBytesToGo(args[0])) if err != nil { - Throw(TypeError, err) + utils.Throw(utils.TypeError, err) return nil } @@ -265,9 +266,9 @@ func (ud *UserDiscovery) PermanentDeleteAccount(_ js.Value, args []js.Value) int // Returns: // - Throws TypeError if removing the fact fails. func (ud *UserDiscovery) RemoveFact(_ js.Value, args []js.Value) interface{} { - err := ud.api.RemoveFact(CopyBytesToGo(args[0])) + err := ud.api.RemoveFact(utils.CopyBytesToGo(args[0])) if err != nil { - Throw(TypeError, err) + utils.Throw(utils.TypeError, err) return nil } @@ -285,7 +286,7 @@ type udLookupCallback struct { } func (ulc *udLookupCallback) Callback(contactBytes []byte, err error) { - ulc.callback(CopyBytesToJS(contactBytes), err.Error()) + ulc.callback(utils.CopyBytesToJS(contactBytes), err.Error()) } // LookupUD returns the public key of the passed ID as known by the user @@ -305,19 +306,19 @@ func (ulc *udLookupCallback) Callback(contactBytes []byte, err error) { // - Throws a TypeError if the lookup fails. func LookupUD(_ js.Value, args []js.Value) interface{} { e2eID := args[0].Int() - udContact := CopyBytesToGo(args[1]) - cb := &udLookupCallback{WrapCB(args[2], "Callback")} - lookupId := CopyBytesToGo(args[3]) - singleRequestParamsJSON := CopyBytesToGo(args[4]) + udContact := utils.CopyBytesToGo(args[1]) + cb := &udLookupCallback{utils.WrapCB(args[2], "Callback")} + lookupId := utils.CopyBytesToGo(args[3]) + singleRequestParamsJSON := utils.CopyBytesToGo(args[4]) report, err := bindings.LookupUD( e2eID, udContact, cb, lookupId, singleRequestParamsJSON) if err != nil { - Throw(TypeError, err) + utils.Throw(utils.TypeError, err) return nil } - return CopyBytesToJS(report) + return utils.CopyBytesToJS(report) } //////////////////////////////////////////////////////////////////////////////// @@ -331,7 +332,7 @@ type udSearchCallback struct { } func (usc *udSearchCallback) Callback(contactListJSON []byte, err error) { - usc.callback(CopyBytesToJS(contactListJSON), err.Error()) + usc.callback(utils.CopyBytesToJS(contactListJSON), err.Error()) } // SearchUD searches user discovery for the passed Facts. The searchCallback @@ -352,17 +353,17 @@ func (usc *udSearchCallback) Callback(contactListJSON []byte, err error) { // - Throws a TypeError if the search fails. func SearchUD(_ js.Value, args []js.Value) interface{} { e2eID := args[0].Int() - udContact := CopyBytesToGo(args[1]) - cb := &udSearchCallback{WrapCB(args[2], "Callback")} - factListJSON := CopyBytesToGo(args[3]) - singleRequestParamsJSON := CopyBytesToGo(args[4]) + udContact := utils.CopyBytesToGo(args[1]) + cb := &udSearchCallback{utils.WrapCB(args[2], "Callback")} + factListJSON := utils.CopyBytesToGo(args[3]) + singleRequestParamsJSON := utils.CopyBytesToGo(args[4]) report, err := bindings.SearchUD( e2eID, udContact, cb, factListJSON, singleRequestParamsJSON) if err != nil { - Throw(TypeError, err) + utils.Throw(utils.TypeError, err) return nil } - return CopyBytesToJS(report) + return utils.CopyBytesToJS(report) }