chore: avoid SubtleCrypto
This commit is contained in:
parent
84ad3556a7
commit
424c7c79d7
@ -35,7 +35,7 @@ export default function useEventPublisher() {
|
|||||||
|
|
||||||
async function signEvent(ev: NEvent): Promise<NEvent> {
|
async function signEvent(ev: NEvent): Promise<NEvent> {
|
||||||
if (hasNip07 && !privKey) {
|
if (hasNip07 && !privKey) {
|
||||||
ev.Id = await ev.CreateId();
|
ev.Id = ev.CreateId();
|
||||||
const tmpEv = (await barrierNip07(() => window.nostr.signEvent(ev.ToObject()))) as RawEvent;
|
const tmpEv = (await barrierNip07(() => window.nostr.signEvent(ev.ToObject()))) as RawEvent;
|
||||||
return new NEvent(tmpEv as TaggedRawEvent);
|
return new NEvent(tmpEv as TaggedRawEvent);
|
||||||
} else if (privKey) {
|
} else if (privKey) {
|
||||||
|
@ -24,6 +24,7 @@
|
|||||||
"semi": false
|
"semi": false
|
||||||
},
|
},
|
||||||
"dependencies": {
|
"dependencies": {
|
||||||
|
"@noble/hashes": "^1.2.0",
|
||||||
"@noble/secp256k1": "^1.7.1",
|
"@noble/secp256k1": "^1.7.1",
|
||||||
"bech32": "^2.0.0",
|
"bech32": "^2.0.0",
|
||||||
"events": "^3.3.0",
|
"events": "^3.3.0",
|
||||||
|
@ -1,4 +1,5 @@
|
|||||||
import * as secp from "@noble/secp256k1";
|
import * as secp from "@noble/secp256k1";
|
||||||
|
import { sha256 } from "@noble/hashes/sha256";
|
||||||
import * as base64 from "@protobufjs/base64";
|
import * as base64 from "@protobufjs/base64";
|
||||||
import { HexKey, RawEvent, TaggedRawEvent } from "./index";
|
import { HexKey, RawEvent, TaggedRawEvent } from "./index";
|
||||||
import EventKind from "./EventKind";
|
import EventKind from "./EventKind";
|
||||||
@ -78,7 +79,7 @@ export default class Event {
|
|||||||
* Sign this message with a private key
|
* Sign this message with a private key
|
||||||
*/
|
*/
|
||||||
async Sign(key: HexKey) {
|
async Sign(key: HexKey) {
|
||||||
this.Id = await this.CreateId();
|
this.Id = this.CreateId();
|
||||||
|
|
||||||
const sig = await secp.schnorr.sign(this.Id, key);
|
const sig = await secp.schnorr.sign(this.Id, key);
|
||||||
this.Signature = secp.utils.bytesToHex(sig);
|
this.Signature = secp.utils.bytesToHex(sig);
|
||||||
@ -92,12 +93,12 @@ export default class Event {
|
|||||||
* @returns True if valid signature
|
* @returns True if valid signature
|
||||||
*/
|
*/
|
||||||
async Verify() {
|
async Verify() {
|
||||||
const id = await this.CreateId();
|
const id = this.CreateId();
|
||||||
const result = await secp.schnorr.verify(this.Signature, id, this.PubKey);
|
const result = await secp.schnorr.verify(this.Signature, id, this.PubKey);
|
||||||
return result;
|
return result;
|
||||||
}
|
}
|
||||||
|
|
||||||
async CreateId() {
|
CreateId() {
|
||||||
const payload = [
|
const payload = [
|
||||||
0,
|
0,
|
||||||
this.PubKey,
|
this.PubKey,
|
||||||
@ -107,9 +108,7 @@ export default class Event {
|
|||||||
this.Content,
|
this.Content,
|
||||||
];
|
];
|
||||||
|
|
||||||
const payloadData = new TextEncoder().encode(JSON.stringify(payload));
|
const hash = secp.utils.bytesToHex(sha256(JSON.stringify(payload)));
|
||||||
const data = await secp.utils.sha256(payloadData);
|
|
||||||
const hash = secp.utils.bytesToHex(data);
|
|
||||||
if (this.Id !== "" && hash !== this.Id) {
|
if (this.Id !== "" && hash !== this.Id) {
|
||||||
console.debug(payload);
|
console.debug(payload);
|
||||||
throw "ID doesnt match!";
|
throw "ID doesnt match!";
|
||||||
|
Loading…
x
Reference in New Issue
Block a user