-
Notifications
You must be signed in to change notification settings - Fork 25
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: use nip44 and versioning (#834)
* feat: use nip44 and versioning * fix: panic: runtime error: invalid memory address or nil pointer dereference * chore: check for version before notifying * chore: add nip47 cipher * chore: remove version check in event handler * chore: address feedback * chore: modify tests to use versioning and nip47 cipher * chore: add more tests * chore: add version param and change naming of test app functions * chore: cleanup event handler tests * chore: cleanup nip47 notifier tests * chore: dry up cipher test * chore: remove version param from create app * chore: republish info event for existing apps * chore: simplify isVersionSupported function * chore: rename event handler legacy test * chore: add nip04 test for create response * chore: add comment and allow empty request version --------- Co-authored-by: Fmar <frnandu@gmail.com>
- Loading branch information
1 parent
2a63435
commit 94c209a
Showing
15 changed files
with
623 additions
and
346 deletions.
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
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 |
---|---|---|
@@ -0,0 +1,87 @@ | ||
package cipher | ||
|
||
import ( | ||
"fmt" | ||
|
||
"github.com/nbd-wtf/go-nostr/nip04" | ||
"github.com/nbd-wtf/go-nostr/nip44" | ||
) | ||
|
||
const ( | ||
SUPPORTED_VERSIONS = "1.0 0.0" | ||
) | ||
|
||
type Nip47Cipher struct { | ||
version string | ||
pubkey string | ||
privkey string | ||
sharedSecret []byte | ||
conversationKey [32]byte | ||
} | ||
|
||
func NewNip47Cipher(version, pubkey, privkey string) (*Nip47Cipher, error) { | ||
_, err := isVersionSupported(version) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
var ss []byte | ||
var ck [32]byte | ||
if version == "0.0" { | ||
ss, err = nip04.ComputeSharedSecret(pubkey, privkey) | ||
if err != nil { | ||
return nil, err | ||
} | ||
} else { | ||
ck, err = nip44.GenerateConversationKey(pubkey, privkey) | ||
if err != nil { | ||
return nil, err | ||
} | ||
} | ||
|
||
return &Nip47Cipher{ | ||
version: version, | ||
pubkey: pubkey, | ||
privkey: privkey, | ||
sharedSecret: ss, | ||
conversationKey: ck, | ||
}, nil | ||
} | ||
|
||
func (c *Nip47Cipher) Encrypt(message string) (msg string, err error) { | ||
if c.version == "0.0" { | ||
msg, err = nip04.Encrypt(message, c.sharedSecret) | ||
if err != nil { | ||
return "", err | ||
} | ||
} else { | ||
msg, err = nip44.Encrypt(message, c.conversationKey) | ||
if err != nil { | ||
return "", err | ||
} | ||
} | ||
return msg, nil | ||
} | ||
|
||
func (c *Nip47Cipher) Decrypt(content string) (payload string, err error) { | ||
if c.version == "0.0" { | ||
payload, err = nip04.Decrypt(content, c.sharedSecret) | ||
if err != nil { | ||
return "", err | ||
} | ||
} else { | ||
payload, err = nip44.Decrypt(content, c.conversationKey) | ||
if err != nil { | ||
return "", err | ||
} | ||
} | ||
return payload, nil | ||
} | ||
|
||
func isVersionSupported(version string) (bool, error) { | ||
if version == "1.0" || version == "0.0" { | ||
return true, nil | ||
} | ||
|
||
return false, fmt.Errorf("invalid version: %s", version) | ||
} |
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 |
---|---|---|
@@ -0,0 +1,47 @@ | ||
package cipher | ||
|
||
import ( | ||
"fmt" | ||
"testing" | ||
|
||
"github.com/nbd-wtf/go-nostr" | ||
|
||
"github.com/stretchr/testify/assert" | ||
) | ||
|
||
func TestCipher(t *testing.T) { | ||
doTestCipher(t, "0.0") | ||
doTestCipher(t, "1.0") | ||
} | ||
|
||
func doTestCipher(t *testing.T, version string) { | ||
reqPrivateKey := nostr.GeneratePrivateKey() | ||
reqPubkey, err := nostr.GetPublicKey(reqPrivateKey) | ||
|
||
nip47Cipher, err := NewNip47Cipher(version, reqPubkey, reqPrivateKey) | ||
assert.NoError(t, err) | ||
|
||
payload := "test payload" | ||
msg, err := nip47Cipher.Encrypt(payload) | ||
assert.NoError(t, err) | ||
|
||
decrypted, err := nip47Cipher.Decrypt(msg) | ||
assert.Equal(t, payload, decrypted) | ||
} | ||
|
||
func TestCipher_UnsupportedVersions(t *testing.T) { | ||
doTestCipher_UnsupportedVersions(t, "1") | ||
doTestCipher_UnsupportedVersions(t, "x.1") | ||
doTestCipher_UnsupportedVersions(t, "1.x") | ||
doTestCipher_UnsupportedVersions(t, "2.0") | ||
doTestCipher_UnsupportedVersions(t, "0.5") | ||
} | ||
|
||
func doTestCipher_UnsupportedVersions(t *testing.T, version string) { | ||
reqPrivateKey := nostr.GeneratePrivateKey() | ||
reqPubkey, err := nostr.GetPublicKey(reqPrivateKey) | ||
|
||
_, err = NewNip47Cipher(version, reqPubkey, reqPrivateKey) | ||
assert.Error(t, err) | ||
assert.Equal(t, fmt.Sprintf("invalid version: %s", version), err.Error()) | ||
} |
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.