Skip to content

bug: $module is not defined in browser #1094

Closed
@EwenQuim

Description

@EwenQuim

This one is a bit disturbing. I have got a very simple script that works perfectly on node but not on the browser.

package main

import (
	"crypto/ed25519"
	"encoding/base64"

	"github.com/gopherjs/gopherjs/js"
)

func SignMessage(secBase64, pubBase64, displayedName, message, messageId string) string {
	sec, err := base64.URLEncoding.DecodeString(secBase64)
	if err != nil || len(sec) != ed25519.PrivateKeySize {
		return ""
	}
	pub, err := base64.URLEncoding.DecodeString(pubBase64)
	if err != nil {
		return ""
	}
	msgToSign := append([]byte(message), pub...)
	msgToSign = append(msgToSign, []byte(displayedName)...)
	msgToSign = append(msgToSign, []byte(messageId)...)
	signature := ed25519.Sign(sec, msgToSign)
	return base64.URLEncoding.EncodeToString(signature)
}

func main() {
	js.Module.Get("exports").Set("SignMessage", SignMessage)
}

You can easily reproduce it with gopherjs build -o lib.js and this small html file:

<!DOCTYPE html>
<html>
<head>
  <title>Title</title>
  <script src="./lib.js"></script>
</head>
<body>
  Test
  <script>
    console.log(SignMessage("test", "test", "test", "test", ""));
  </script>
</body>
</html>

image

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions