-
Notifications
You must be signed in to change notification settings - Fork 195
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
feat: implemented nip04 #1777
Merged
Merged
feat: implemented nip04 #1777
Changes from all commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
4cbf9cc
feat: implemented nip04
reneaaron a7c341e
fix: added test
reneaaron da31845
fix: refactored decrypt to crypto-js
reneaaron 71dd5fc
fix: refactored encrypto to crypto-js
reneaaron b222477
fix: ts fixes
reneaaron 78faf81
fix: ts fixes
reneaaron c4f800b
fix: ts fixes
reneaaron d74ac62
Update src/extension/background-script/actions/nostr/decryptOrPrompt.ts
reneaaron ea240b0
Update src/extension/background-script/actions/nostr/encryptOrPrompt.ts
reneaaron 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
35 changes: 35 additions & 0 deletions
35
src/extension/background-script/actions/nostr/decryptOrPrompt.ts
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,35 @@ | ||
import state from "~/extension/background-script/state"; | ||
import { MessageDecryptGet } from "~/types"; | ||
|
||
const decryptOrPrompt = async (message: MessageDecryptGet) => { | ||
if (!("host" in message.origin)) { | ||
console.error("error", message.origin); | ||
return; | ||
} | ||
|
||
const result = await prompt(message); | ||
|
||
return result; | ||
}; | ||
|
||
const prompt = async (message: MessageDecryptGet) => { | ||
try { | ||
// TODO: Add prompt & permissions | ||
|
||
const response = { | ||
data: state | ||
.getState() | ||
.getNostr() | ||
.decrypt(message.args.peer, message.args.ciphertext), | ||
}; | ||
|
||
return response; | ||
} catch (e) { | ||
console.error("decrypt failed", e); | ||
if (e instanceof Error) { | ||
return { error: e.message }; | ||
} | ||
} | ||
}; | ||
|
||
export default decryptOrPrompt; |
35 changes: 35 additions & 0 deletions
35
src/extension/background-script/actions/nostr/encryptOrPrompt.ts
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,35 @@ | ||
import state from "~/extension/background-script/state"; | ||
import { MessageEncryptGet } from "~/types"; | ||
|
||
const encryptOrPrompt = async (message: MessageEncryptGet) => { | ||
if (!("host" in message.origin)) { | ||
console.error("error", message.origin); | ||
return; | ||
} | ||
|
||
const result = await prompt(message); | ||
|
||
return result; | ||
}; | ||
|
||
const prompt = async (message: MessageEncryptGet) => { | ||
try { | ||
// TODO: Add prompt & permissions | ||
|
||
const response = { | ||
data: state | ||
.getState() | ||
.getNostr() | ||
.encrypt(message.args.peer, message.args.plaintext), | ||
}; | ||
|
||
return response; | ||
} catch (e) { | ||
console.error("encrypt failed", e); | ||
if (e instanceof Error) { | ||
return { error: e.message }; | ||
} | ||
} | ||
}; | ||
|
||
export default encryptOrPrompt; |
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
29 changes: 29 additions & 0 deletions
29
src/extension/background-script/nostr/__test__/nostr.test.ts
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,29 @@ | ||
import Nostr from "~/extension/background-script/nostr"; | ||
|
||
const alice = { | ||
privateKey: | ||
"9ab5b12ade1d9c27207ff0264e9fb155c77c9361c9b6a27c865fce1b2c0ddf0e", | ||
publicKey: "0bf50e2fdc927853c12b64c06f6a703cfad8086e79b18b1eb864f3fab7fc6f74", | ||
}; | ||
|
||
const bob = { | ||
privateKey: | ||
"b7eab8ab34aac491217a31059ec017e51c63d09c828e39ee3a40a016bc9d0cbf", | ||
publicKey: "519f5ae2cd7d4b970c4edadb2efc947c9b803838de918d1c5bfd4b9c1a143b72", | ||
}; | ||
|
||
describe("nostr", () => { | ||
test("encrypt & decrypt", async () => { | ||
const nostr = new Nostr(); | ||
nostr.getPrivateKey = jest.fn().mockReturnValue(alice.privateKey); | ||
|
||
const message = "Secret message that is sent from Alice to Bob"; | ||
const encrypted = nostr.encrypt(bob.publicKey, message); | ||
|
||
nostr.getPrivateKey = jest.fn().mockReturnValue(bob.privateKey); | ||
|
||
const decrypted = nostr.decrypt(alice.publicKey, encrypted); | ||
|
||
expect(decrypted).toMatch(message); | ||
}); | ||
}); |
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 |
---|---|---|
@@ -1,4 +1,10 @@ | ||
import * as secp256k1 from "@noble/secp256k1"; | ||
import { Buffer } from "buffer"; | ||
import { AES } from "crypto-js"; | ||
import * as CryptoJS from "crypto-js"; | ||
import Base64 from "crypto-js/enc-base64"; | ||
import Hex from "crypto-js/enc-hex"; | ||
import Utf8 from "crypto-js/enc-utf8"; | ||
import { decryptData, encryptData } from "~/common/lib/crypto"; | ||
import { Event } from "~/extension/ln/nostr/types"; | ||
|
||
|
@@ -36,6 +42,35 @@ class Nostr { | |
event.sig = signature; | ||
return event; | ||
} | ||
|
||
encrypt(pubkey: string, text: string) { | ||
const key = secp256k1.getSharedSecret(this.getPrivateKey(), "02" + pubkey); | ||
const normalizedKey = Buffer.from(key.slice(1, 33)); | ||
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. what is this doing?
|
||
const hexNormalizedKey = secp256k1.utils.bytesToHex(normalizedKey); | ||
const hexKey = Hex.parse(hexNormalizedKey); | ||
|
||
const encrypted = AES.encrypt(text, hexKey, { | ||
iv: CryptoJS.lib.WordArray.random(16), | ||
reneaaron marked this conversation as resolved.
Show resolved
Hide resolved
|
||
}); | ||
|
||
return `${encrypted.toString()}?iv=${encrypted.iv.toString( | ||
CryptoJS.enc.Base64 | ||
)}`; | ||
} | ||
|
||
decrypt(pubkey: string, ciphertext: string) { | ||
const [cip, iv] = ciphertext.split("?iv="); | ||
const key = secp256k1.getSharedSecret(this.getPrivateKey(), "02" + pubkey); | ||
reneaaron marked this conversation as resolved.
Show resolved
Hide resolved
|
||
const normalizedKey = Buffer.from(key.slice(1, 33)); | ||
const hexNormalizedKey = secp256k1.utils.bytesToHex(normalizedKey); | ||
const hexKey = Hex.parse(hexNormalizedKey); | ||
|
||
const decrypted = AES.decrypt(cip, hexKey, { | ||
iv: Base64.parse(iv), | ||
}); | ||
|
||
return Utf8.stringify(decrypted); | ||
} | ||
} | ||
|
||
export default Nostr; |
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
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
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.
why is this
02
?