Something went wrong on our end
-
Jono Wenger authoredJono Wenger authored
cmix.go 5.25 KiB
////////////////////////////////////////////////////////////////////////////////
// Copyright © 2022 xx foundation //
// //
// Use of this source code is governed by a license that can be found in the //
// LICENSE file. //
////////////////////////////////////////////////////////////////////////////////
//go:build js && wasm
package wasm
import (
"gitlab.com/elixxir/client/v4/bindings"
"gitlab.com/elixxir/xxdk-wasm/utils"
"syscall/js"
)
// Cmix wraps the [bindings.Cmix] object so its methods can be wrapped to be
// Javascript compatible.
type Cmix struct {
api *bindings.Cmix
}
// newCmixJS creates a new Javascript compatible object (map[string]any) that
// matches the [Cmix] structure.
func newCmixJS(api *bindings.Cmix) map[string]any {
c := Cmix{api}
cmix := map[string]any{
// cmix.go
"GetID": js.FuncOf(c.GetID),
// identity.go
"MakeReceptionIdentity": js.FuncOf(
c.MakeReceptionIdentity),
"MakeLegacyReceptionIdentity": js.FuncOf(
c.MakeLegacyReceptionIdentity),
"GetReceptionRegistrationValidationSignature": js.FuncOf(
c.GetReceptionRegistrationValidationSignature),
// follow.go
"StartNetworkFollower": js.FuncOf(c.StartNetworkFollower),
"StopNetworkFollower": js.FuncOf(c.StopNetworkFollower),
"SetTrackNetworkPeriod": js.FuncOf(c.SetTrackNetworkPeriod),
"WaitForNetwork": js.FuncOf(c.WaitForNetwork),
"ReadyToSend": js.FuncOf(c.ReadyToSend),
"NetworkFollowerStatus": js.FuncOf(c.NetworkFollowerStatus),
"GetNodeRegistrationStatus": js.FuncOf(c.GetNodeRegistrationStatus),
"IsReady": js.FuncOf(c.IsReady),
"PauseNodeRegistrations": js.FuncOf(c.PauseNodeRegistrations),
"ChangeNumberOfNodeRegistrations": js.FuncOf(c.ChangeNumberOfNodeRegistrations),
"HasRunningProcessies": js.FuncOf(c.HasRunningProcessies),
"IsHealthy": js.FuncOf(c.IsHealthy),
"GetRunningProcesses": js.FuncOf(c.GetRunningProcesses),
"AddHealthCallback": js.FuncOf(c.AddHealthCallback),
"RemoveHealthCallback": js.FuncOf(c.RemoveHealthCallback),
"RegisterClientErrorCallback": js.FuncOf(c.RegisterClientErrorCallback),
"TrackServicesWithIdentity": js.FuncOf(c.TrackServicesWithIdentity),
"TrackServices": js.FuncOf(c.TrackServices),
// connect.go
"Connect": js.FuncOf(c.Connect),
// delivery.go
"WaitForRoundResult": js.FuncOf(c.WaitForRoundResult),
// authenticatedConnection.go
"ConnectWithAuthentication": js.FuncOf(c.ConnectWithAuthentication),
}
return cmix
}
// NewCmix creates user storage, generates keys, connects, and registers with
// the network. Note that this does not register a username/identity, but merely
// creates a new cryptographic identity for adding such information at a later
// date.
//
// Users of this function should delete the storage directory on error.
//
// Parameters:
// - args[0] - NDF JSON ([ndf.NetworkDefinition]) (string).
// - args[1] - Storage directory path (string).
// - args[2] - Password used for storage (Uint8Array).
// - args[3] - Registration code (string).
//
// Returns a promise:
// - Resolves on success.
// - Rejected with an error if creating a new cMix client fails.
func NewCmix(_ js.Value, args []js.Value) any {
ndfJSON := args[0].String()
storageDir := args[1].String()
password := utils.CopyBytesToGo(args[2])
registrationCode := args[3].String()
promiseFn := func(resolve, reject func(args ...any) js.Value) {
err := bindings.NewCmix(ndfJSON, storageDir, password, registrationCode)
if err != nil {
reject(utils.JsTrace(err))
} else {
resolve()
}
}
return utils.CreatePromise(promiseFn)
}
// LoadCmix will load an existing user storage from the storageDir using the
// password. This will fail if the user storage does not exist or the password
// is incorrect.
//
// The password is passed as a byte array so that it can be cleared from memory
// and stored as securely as possible using the MemGuard library.
//
// LoadCmix does not block on network connection and instead loads and starts
// subprocesses to perform network operations.
//
// Parameters:
// - args[0] - Storage directory path (string).
// - args[1] - Password used for storage (Uint8Array).
// - args[2] - JSON of [xxdk.CMIXParams] (Uint8Array).
//
// Returns a promise:
// - Resolves to a Javascript representation of the [Cmix] object.
// - Rejected with an error if loading [Cmix] fails.
func LoadCmix(_ js.Value, args []js.Value) any {
storageDir := args[0].String()
password := utils.CopyBytesToGo(args[1])
cmixParamsJSON := utils.CopyBytesToGo(args[2])
promiseFn := func(resolve, reject func(args ...any) js.Value) {
net, err := bindings.LoadCmix(storageDir, password, cmixParamsJSON)
if err != nil {
reject(utils.JsTrace(err))
} else {
resolve(newCmixJS(net))
}
}
return utils.CreatePromise(promiseFn)
}
// GetID returns the ID for this [bindings.Cmix] in the cmixTracker.
//
// Returns:
// - Tracker ID (int).
func (c *Cmix) GetID(js.Value, []js.Value) any {
return c.api.GetID()
}