-
Notifications
You must be signed in to change notification settings - Fork 1
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
add functions in support of new ndaujs package for exchange integrators #3
Open
jsgjnr
wants to merge
1
commit into
master
Choose a base branch
from
jsg-ndaujs-updates
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -9,14 +9,16 @@ package main | |
// https://www.apache.org/licenses/LICENSE-2.0.txt | ||
// - -- --- ---- ----- | ||
|
||
|
||
import ( | ||
"encoding/hex" | ||
"fmt" | ||
"math" | ||
"regexp" | ||
"syscall/js" | ||
|
||
"github.com/ndau/ndaumath/pkg/address" | ||
"github.com/ndau/ndaumath/pkg/keyaddr" | ||
"github.com/ndau/ndaumath/pkg/signature" | ||
) | ||
|
||
// exit will quit the go program, causing the application to no longer respond to function calls. | ||
|
@@ -72,6 +74,136 @@ func newKey(this js.Value, args []js.Value) interface{} { | |
return nil | ||
} | ||
|
||
// JS Usage: newEdKey(cb) --> no args | ||
func newEdKey(this js.Value, args []js.Value) interface{} { | ||
go func(args []js.Value) { | ||
logDebug("newEdKey") | ||
|
||
// clean args | ||
callback, _, err := handleArgs(args, 0, "newEdKey") | ||
if err != nil { | ||
return | ||
} | ||
|
||
// do work | ||
public, private, err := signature.Generate(signature.Ed25519, nil) | ||
if err != nil { | ||
jsLogReject(callback, "error creating new ed key: %s", err) | ||
return | ||
} | ||
|
||
pubkeyText, err := public.MarshalText() | ||
if err != nil { | ||
jsLogReject(callback, "error marshalling new public ed key: %s", err) | ||
return | ||
} | ||
|
||
privkeyText, err := private.MarshalText() | ||
if err != nil { | ||
jsLogReject(callback, "error marshalling new private ed key: %s", err) | ||
return | ||
} | ||
|
||
// Make an map[string]interface{} so syscall will turn it into a js object. | ||
obj := make(map[string]interface{}) | ||
obj["pubkey"] = string(pubkeyText) | ||
obj["privkey"] = string(privkeyText) | ||
|
||
// return result | ||
callback.Invoke(nil, js.ValueOf(obj)) | ||
return | ||
}(args) | ||
return nil | ||
} | ||
|
||
// JS Usage: newEdKey(seed, cb) | ||
func newEdKeyFromSeed(this js.Value, args []js.Value) interface{} { | ||
go func(args []js.Value) { | ||
logDebug("newEdKeyFromSeed") | ||
|
||
// clean args | ||
callback, remainder, err := handleArgs(args, 1, "newEdKeyFromSeed") | ||
if err != nil { | ||
return | ||
} | ||
|
||
seedString := remainder[0].String() | ||
seed, err := hex.DecodeString(seedString) | ||
if err != nil { | ||
jsLogReject(callback, "error decoding data from hex: %s", err) | ||
return | ||
} | ||
|
||
// do work | ||
public, private, err := signature.GenerateFromSeed(signature.Ed25519, seed) | ||
if err != nil { | ||
jsLogReject(callback, "error creating new ed key: %s", err) | ||
return | ||
} | ||
|
||
pubkeyText, err := public.MarshalText() | ||
if err != nil { | ||
jsLogReject(callback, "error marshalling new public ed key: %s", err) | ||
return | ||
} | ||
|
||
privkeyText, err := private.MarshalText() | ||
if err != nil { | ||
jsLogReject(callback, "error marshalling new private ed key: %s", err) | ||
return | ||
} | ||
|
||
// Make an map[string]interface{} so syscall will turn it into a js object. | ||
obj := make(map[string]interface{}) | ||
obj["pubkey"] = string(pubkeyText) | ||
obj["privkey"] = string(privkeyText) | ||
|
||
// return result | ||
callback.Invoke(nil, js.ValueOf(obj)) | ||
return | ||
}(args) | ||
return nil | ||
} | ||
|
||
// JS Usage: addrFromPublicKey(pubKey, cb) | ||
func addrFromPublicKey(this js.Value, args []js.Value) interface{} { | ||
go func(args []js.Value) { | ||
logDebug("addrFromPublicKey") | ||
|
||
// clean args | ||
callback, remainder, err := handleArgs(args, 1, "addrFromPublicKey") | ||
if err != nil { | ||
return | ||
} | ||
|
||
pubKeyString := remainder[0].String() | ||
|
||
// do work | ||
key, err := signature.ParseKey(pubKeyString) | ||
if err != nil { | ||
jsLogReject(callback, "error parsing public key: %s", err) | ||
return | ||
} | ||
|
||
_, ok := key.(*signature.PublicKey) | ||
if !ok { | ||
jsLogReject(callback, "addresses can only be generated from public keys") | ||
return | ||
} | ||
|
||
addr, err := address.Generate(address.KindExchange, key.KeyBytes()) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. If this is for general |
||
if err != nil { | ||
jsLogReject(callback, "error generating address from key: %s", err) | ||
return | ||
} | ||
|
||
// return result | ||
callback.Invoke(nil, addr.String()) | ||
return | ||
}(args) | ||
return nil | ||
} | ||
|
||
// JS Usage: wordsToBytes(language, words, cb) | ||
// language defaults to en if not specified. | ||
func wordsToBytes(this js.Value, args []js.Value) interface{} { | ||
|
@@ -370,7 +502,71 @@ func sign(this js.Value, args []js.Value) interface{} { | |
msg := remainder[1].String() | ||
|
||
// do work | ||
sig, err := k.Sign(msg) | ||
sig, err := k.SignSecP(msg) | ||
if err != nil { | ||
logError(fmt.Sprintf("error creating signature: key length: %s, msg: %s, err: %s", len(k.Key), msg, err.Error())) | ||
jsLogReject(callback, "error creating signature: %s", err) | ||
return | ||
} | ||
|
||
// return result | ||
callback.Invoke(nil, sig.Signature) | ||
return | ||
}(args) | ||
return nil | ||
} | ||
|
||
// JS Usage: signEdB64(privateKey, base64Message, cb) | ||
func signEdB64(this js.Value, args []js.Value) interface{} { | ||
js.Global().Get("console").Call("log", "sign starting") | ||
go func(args []js.Value) { | ||
logDebug("signEdB64") | ||
// clean args | ||
callback, remainder, err := handleArgs(args, 2, "signEdB64") | ||
if err != nil { | ||
return | ||
} | ||
|
||
k := keyaddr.Key{ | ||
Key: remainder[0].String(), | ||
} | ||
|
||
msg := remainder[1].String() | ||
|
||
// do work | ||
sig, err := k.SignEdB64(msg) | ||
if err != nil { | ||
logError(fmt.Sprintf("error creating signature: key length: %s, msg: %s, err: %s", len(k.Key), msg, err.Error())) | ||
jsLogReject(callback, "error creating signature: %s", err) | ||
return | ||
} | ||
|
||
// return result | ||
callback.Invoke(nil, sig.Signature) | ||
return | ||
}(args) | ||
return nil | ||
} | ||
|
||
// JS Usage: signEdText(privateKey, textMessage, cb) | ||
func signEdText(this js.Value, args []js.Value) interface{} { | ||
js.Global().Get("console").Call("log", "sign starting") | ||
go func(args []js.Value) { | ||
logDebug("signEdText") | ||
// clean args | ||
callback, remainder, err := handleArgs(args, 2, "signEdText") | ||
if err != nil { | ||
return | ||
} | ||
|
||
k := keyaddr.Key{ | ||
Key: remainder[0].String(), | ||
} | ||
|
||
msg := remainder[1].String() | ||
|
||
// do work | ||
sig, err := k.SignEdText(msg) | ||
if err != nil { | ||
logError(fmt.Sprintf("error creating signature: key length: %s, msg: %s, err: %s", len(k.Key), msg, err.Error())) | ||
jsLogReject(callback, "error creating signature: %s", err) | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Should we check for correct seed length here? I don't think it's necessary, since a bad key length will just cause
GenerateFromSeed
to return an error.