Public
Edited
Apr 6, 2022
1 fork
4 stars
Insert cell
# Nostr Playground
Insert cell
difficulty = 1n
Insert cell
mine(event)
Insert cell
event = ({
pubkey: "22e804d26ed16b68db5259e78449e96dab5d464c8f470bda3eb1a70467f2c793",
created_at: 1648504074,
kind: 1,
tags: [
[
"p",
"8355095016fddbe31fcf1453b26f613553e9758cf2263e190eac8fd96a3d3de9",
"wss://nostr-relay.wlvs.space"
],
[
"p",
"d7e747f60a16bf0081c0a88184a34086cc13e6edb0662d4e55202531b47be026",
"wss://nostr-relay.wlvs.space"
],
[
"p",
"5c6c25b7ef18d8633e97512159954e1aa22809c6b763e94b9f91071836d00217",
"wss://nostr-relay.wlvs.space"
],
[
"p",
"ad5aab5be883a571ea37b231cd996d37522e77d0f121cedfd6787b91d848268e",
"wss://nostr-pub.wellorder.net"
],
[
"e",
"7377fa81fc6c7ae7f7f4ef8938d4a603f7bf98183b35ab128235cc92d4bebf96",
"wss://nostr-relay.wlvs.space"
],
[
"e",
"9cd7f75bde4249f28cd0ee6bc4bf7394bff11d608b54b520e9aa95ac09f47c36",
"wss://nostr-relay.wlvs.space"
]
],
content:
"it's got a smart switch that provides power, voltage, current in real-time connected to Home Assistant, I then have a Node-Red flow that watches the power and detects whether it is in use.\nthe washer/dryer here are smart appliances, I could probably hook them up to Nostr too"
})
Insert cell
md`# Utils`
Insert cell
function* mine(event) {
const clonedEvent = JSON.parse(JSON.stringify(event));
delete clonedEvent.id;

let nonce = 0n;
const nonceTag = ["nonce", { toJSON: () => nonce.toString() }];
clonedEvent.tags.push(nonceTag);

let hash, hashStr;
const target = (0xffffn * 2n ** 228n) / difficulty;
do {
hashStr = getEventHash(clonedEvent);
hash = BigInt(`0x${hashStr.toString("hex")}`);

yield {
status: "mining",
nonce
};

if (hash < target) {
break;
}
nonce++;
} while (true);

yield {
status: "done",
event: { id: hashStr.toString("hex"), ...clonedEvent },
nonce
};
}
Insert cell
function serializeEvent(evt) {
return JSON.stringify([
0,
evt.pubkey,
evt.created_at,
evt.kind,
evt.tags,
evt.content
]);
}
Insert cell
function getEventHash(event) {
let eventHash = createHash("sha256")
.update(Buffer.from(serializeEvent(event)))
.digest();
return Buffer.from(eventHash).toString("hex");
}
Insert cell
async function signEvent(event, key = Secret("nostr_test_anon_privkey")) {
return Buffer.from(
await window.schnorrSign(getEventHash(event), key)
).toString("hex");
}
Insert cell
function getPublicKey(privateKey = Secret("nostr_test_anon_privkey")) {
return Buffer.from(window.schnorrGetPublicKey(privateKey)).toString("hex");
}
Insert cell
md`# Imports`
Insert cell
createHash = require("https://bundle.run/create-hash@1.2.0")
Insert cell
require("https://bundle.run/@noble/secp256k1@1.5.5")
Insert cell
Buffer = (await import("https://cdn.skypack.dev/buffer@6.0.3?min")).Buffer
Insert cell
# References
1. https://github.com/fiatjaf/nostr-tools
2. https://en.bitcoin.it/wiki/Difficulty
3. https://coolaj86.com/articles/native-bigints-have-arrived-in-chrome-and-node-js/
4. https://bitcoin.stackexchange.com/a/44580
Insert cell

Purpose-built for displays of data

Observable is your go-to platform for exploring data and creating expressive data visualizations. Use reactive JavaScript notebooks for prototyping and a collaborative canvas for visual data exploration and dashboard creation.
Learn more