What activation does in a Wails app
Activation ties a key to one computer. Clavem answers with an instance.id and a signed token, and your app stores the key, the id and the token in the keychain, as in the validation guide. Every later launch validates with that id. A product’s device limit, 3 by default, decides how many computers can be active. Activating again with a fingerprint that is already active returns the same seat.
The fingerprint
instance_name is for people: the buyer sees it on the device page, so use the hostname. fingerprint is for machines, a stable hashed identifier up to 200 characters. machineid.ProtectedID returns an HMAC-SHA256 in hex, computed over your app id with the machine id as the key. It is 64 characters and does not expose the raw id. Its README names the sources: IOPlatformUUID on macOS, MachineGuid from the registry on Windows, and /var/lib/dbus/machine-id or /etc/machine-id on Linux.
// go get github.com/denisbrodbeck/machineid
func fingerprint() (string, error) {
return machineid.ProtectedID("com.example.inkwell") // keep this string fixed after shipping
}
Activate and deactivate
Add the license and limit fields to the reply struct from the validation guide, then add these two methods. Fingerprinter is func() (string, error).
type reply struct {
// ...Valid, Error and Token as before
Activated bool `json:"activated"`
Instance *struct {
ID string `json:"id"`
} `json:"instance"`
LicenseKey *struct {
ActivationLimit *int `json:"activation_limit"` // null means unlimited
ActivationUsage int `json:"activation_usage"`
} `json:"license_key"`
}
// LimitError carries the counts, because only its message reaches the webview.
type LimitError struct{ Limit, Used int }
func (e *LimitError) Error() string {
return fmt.Sprintf("This license key has reached the activation limit (%d of %d devices in use).", e.Used, e.Limit)
}
// Activate is called once per computer, when the user pastes a key.
func (c *Client) Activate(ctx context.Context, key string, fp Fingerprinter) error {
key = strings.TrimSpace(key)
name, _ := os.Hostname()
if name == "" {
name = "Computer"
}
body := map[string]string{"license_key": key, "instance_name": name}
if fp != nil {
if v, err := fp(); err == nil {
body["fingerprint"] = v // leave it out if the id cannot be read
}
}
status, r, _, err := c.call(ctx, "activate", body)
if err != nil {
return errors.New("Could not reach the license server. Check the connection and try again.")
}
if !r.Activated || r.Instance == nil || r.Token == "" {
if status == 400 && r.LicenseKey != nil && r.LicenseKey.ActivationLimit != nil &&
r.LicenseKey.ActivationUsage >= *r.LicenseKey.ActivationLimit {
return &LimitError{Limit: *r.LicenseKey.ActivationLimit, Used: r.LicenseKey.ActivationUsage}
}
if r.Error == "" {
r.Error = fmt.Sprintf("Activation failed (HTTP %d).", status)
}
return errors.New(r.Error)
}
return c.Store.Save(Saved{Key: key, InstanceID: r.Instance.ID, Token: r.Token})
}
// Deactivate frees this computer's seat. Bind it to a button.
func (c *Client) Deactivate(ctx context.Context) error {
saved, _ := c.Store.Load()
if saved == nil {
return nil
}
status, _, _, err := c.call(ctx, "deactivate", map[string]string{"license_key": saved.Key, "instance_id": saved.InstanceID})
if err != nil {
return errors.New("Could not reach the license server, so this seat is still in use. Try again online.")
}
if status == 200 || status == 404 { // 404: already gone on the server
return c.Store.Clear()
}
return fmt.Errorf("Deactivation failed (HTTP %d).", status)
}
Expose them on your bound struct with two return values, for example Activate(key string) (license.Result, error): the new state first, the error second. The Wails v2 documentation says an error returned as the second value reaches the front end through the promise’s reject handler, so the webview shows the message in a catch. To push a state change instead of returning it, Wails v2 has runtime.EventsEmit(ctx, "license:state", result) and v3 has app.Event.Emit("license:state", result), with EventsOn or Events.On in the front end.
What I ran
On the Go Playground against an in-process mock of the documented responses. The playground has no network, so this did not touch the live API.
| Case | Result |
|---|---|
Activate with GOOD |
nil, key trimmed, instance id stored |
| 400, limit 3, usage 3 | *LimitError: “This license key has reached the activation limit (3 of 3 devices in use).” |
| 404 | “license_key not found.” |
| Deactivate, 200 | nil, keychain cleared |
Deactivate, 404 instance_id not found. |
nil, keychain cleared |
Wails and Go details
- Never from
OnShutdownorOnBeforeClose. Those v2 hooks run on every quit. Use a “Deactivate this computer” button. - Cloned machines. The README warns that cloned Windows installs share a
MachineGuid, and thatmachine-idandMachineGuidcan be changed by an administrator. A cloned image can therefore share one seat. Treat the fingerprint as friction, not security. - Linux in containers or minimal installs may have no
/etc/machine-id.ProtectedIDreturns an error then. The code above leaves out the fingerprint; the seat is then not reused after a reinstall. - Show the limit message as written. Tell buyers they can remove a computer on the device page at
/licenses/managewith the key alone.
Doing this with Clavem
Activation and deactivation take a key and an instance id and need no authentication. The fields, errors and the fingerprint rule are in the API reference.
Questions
Why not deactivate in OnShutdown?
A Wails app shuts down every time the user closes the window, often with no network. Deactivating there would free the seat each time and needs a connection at the worst moment. Activating again with the same fingerprint returns the same seat anyway.
Which machine ID library should I use?
denisbrodbeck/machineid reads the OS machine id and offers ProtectedID, which mixes your app id into an HMAC-SHA256 with the machine id as the key. Its README lists the sources per OS and warns that cloned Windows installs share an id.