From 01fcbbf3fc0539d1dedbb126afc33c2df749acee Mon Sep 17 00:00:00 2001 From: npub1smartg7jdu7q7vew86qjggq90ncwdnue03d7r32gqe4qn335mtlqpqs7zu <86fa35a3d26f3c0f332e3e812420057cf0e6cf997c5be1c548066a09c634dafe@git.nostr> Date: Wed, 10 Jul 2024 22:21:29 +0200 Subject: [PATCH] genesis --- .txo/txo.txt | 1 + bin/post.sh | 11 ++ bin/u2.sh | 28 +++++ bin/validate.sh | 49 +++++++++ call.html | 216 ++++++++++++++++++++++++++++++++++++++ contract/README.md | 3 + contract/contract.js | 76 ++++++++++++++ contract/contract.lua | 20 ++++ contract/ledgr.json | 1 + contract/state.json | 3 + index.html | 239 ++++++++++++++++++++++++++++++++++++++++++ ledgr.json | 1 + nostr.json | 1 + 13 files changed, 649 insertions(+) create mode 100644 .txo/txo.txt create mode 100755 bin/post.sh create mode 100755 bin/u2.sh create mode 100755 bin/validate.sh create mode 100644 call.html create mode 100644 contract/README.md create mode 100755 contract/contract.js create mode 100644 contract/contract.lua create mode 100644 contract/ledgr.json create mode 100644 contract/state.json create mode 100644 index.html create mode 100644 ledgr.json create mode 100644 nostr.json diff --git a/.txo/txo.txt b/.txo/txo.txt new file mode 100644 index 0000000..a063dfe --- /dev/null +++ b/.txo/txo.txt @@ -0,0 +1 @@ +txo:tbtc4:e0a84ec838712ddec78c57a7dc1ce65b777cbf58bfa861f289e3533a60399493:0 10000 diff --git a/bin/post.sh b/bin/post.sh new file mode 100755 index 0000000..4b411a7 --- /dev/null +++ b/bin/post.sh @@ -0,0 +1,11 @@ +#!/usr/bin/env bash + +HASH=$(rad inspect) + +cd /home/melvin/npub.info/pages/multi-hash/"${HASH:4}" || exit + +git pull + +cd - || exit + +/home/melvin/go/bin/nak event -c "deployed" --kind 613 --sec $(git config nostr.privkey) wss://npub.info/ & diff --git a/bin/u2.sh b/bin/u2.sh new file mode 100755 index 0000000..8ac9340 --- /dev/null +++ b/bin/u2.sh @@ -0,0 +1,28 @@ +#!/usr/bin/env bash + +PUBKEY=$(cat nostr.json | jq -r .pubkey) + +echo "[\"REQ\", \"x\", { \"kinds\" : [611], \"limit\" : 0, \"#p\" : [\"${PUBKEY}\"] }]" | websocat -n --ping-interval 20 wss://npub.info | while read -r line; do + # Process each line received here + echo "Received: $line" + + echo "$line" | jq + + TYPE=$(echo "$line" | jq -r ".[0]") + + echo "type: --${TYPE}--" + + if [ "$TYPE" = "EVENT" ] + then + echo event + bash -x bin/validate.sh + + time bash -x bin/post.sh + + # /home/melvin/go/bin/nak event -c "deployed" --kind 612 --sec $(git config nostr.privkey) wss://npub.info/ & + + fi + + +done + diff --git a/bin/validate.sh b/bin/validate.sh new file mode 100755 index 0000000..656e9e4 --- /dev/null +++ b/bin/validate.sh @@ -0,0 +1,49 @@ +#!/usr/bin/env bash + +EVENT=$(echo '["REQ", "x", { "kinds": [611] }]' | websocat --ping-interval 20 wss://npub.info | tail -2 | head -1) + +ID=$(echo "$EVENT" | jq -r .[2].id) + +echo "$ID" + +CONTENT=$(echo "$EVENT" | jq -r .[2].content) + +echo "$CONTENT" + +echo "$EVENT" | jq -r .[2] > ./contract/calls/"$ID".json + +PROOF=$(echo "$EVENT" | jq -r .[2].tags[0][1]) + +echo "$PROOF" + +# check unspent +TX=$(echo "$PROOF" | cut -d : -f 5) +VOUT=$(echo "$PROOF" | cut -d : -f 6) + +# check keys +SPENT=$(curl -sSL "https://mempool.space/testnet4/api/tx/$TX/outspend/${VOUT}" | jq .spent) + +echo SPENT "$SPENT" + +if [ "$SPENT" = 'false' ] +then + + /home/melvin/go/bin/nak event -c "accepted" --kind 612 --sec $(git config nostr.privkey) wss://npub.info/ & + + contract/contract.js "$CONTENT" + + ~/bin/gitmark/bin/gitmark.sh call-"$ID" + + /home/melvin/go/bin/nak event -c "marked" --kind 612 --sec $(git config nostr.privkey) wss://npub.info/ & + + + # TODO: spend it + +fi + + + +# jq ".hue = $CONTENT" contract/state.json > .git/state +# mv .git/state contract/state.json + + diff --git a/call.html b/call.html new file mode 100644 index 0000000..7608c0e --- /dev/null +++ b/call.html @@ -0,0 +1,216 @@ + + + + + + Ledgr + + + +
+ + + + diff --git a/contract/README.md b/contract/README.md new file mode 100644 index 0000000..1a507b8 --- /dev/null +++ b/contract/README.md @@ -0,0 +1,3 @@ +`sethue` allows you to set a value between 0 and 360 that will be used as the "hue" of the background-color of this website in an HSL formula. + +Be sure to pay at least 1000 sat as the price for having this amazing color-changing opportunity! diff --git a/contract/contract.js b/contract/contract.js new file mode 100755 index 0000000..bd80cae --- /dev/null +++ b/contract/contract.js @@ -0,0 +1,76 @@ +#!/usr/bin/env node + +import { promises as fs } from 'fs' +import path from 'path' + +class SmartContract { + constructor() { + this.stateFilePath = path.resolve('./contract/state.json') + this.state = { hue: 0 } + this.loadState() + } + + async loadState() { + try { + const data = await fs.readFile(this.stateFilePath, 'utf-8') + this.state = JSON.parse(data) + } catch (error) { + console.error('Error loading state:', error) + } + } + + async saveState() { + try { + await fs.writeFile(this.stateFilePath, JSON.stringify(this.state, null, 2)) + } catch (error) { + console.error('Error saving state:', error) + } + } + + async setHue(call) { + if (call.msatoshi < 1000000) { + throw new Error('pay at least 1000 sats!') + } + + if (typeof call.payload.hue !== 'number') { + throw new Error('hue is not a number!') + } + + if (call.payload.hue < 0 || call.payload.hue > 360) { + throw new Error('hue is out of the 0~360 range!') + } + + this.state.hue = call.payload.hue + await this.saveState() + + this.send('86fa35fef8ab4f5906cedfcd966a69e0126973097fd4ea270ddefc505a1a824e', call.msatoshi) + } + + send(address, msatoshi) { + console.log(`Sending ${msatoshi} msatoshi to ${address}`) + // Here you would implement the actual logic to transfer the msatoshi + } +} + +export default SmartContract + +let hue = process.argv[2] || 99 +hue = parseInt(hue) + +const contract = new SmartContract() + +const call = { + msatoshi: 1500000, // Example value + payload: { + hue: hue // Example value within the valid range + } +}; + +(async () => { + try { + await contract.setHue(call) + console.log('Hue set successfully:', contract.state.hue) + } catch (error) { + console.error(error.message) + } +})() diff --git a/contract/contract.lua b/contract/contract.lua new file mode 100644 index 0000000..d3a75bd --- /dev/null +++ b/contract/contract.lua @@ -0,0 +1,20 @@ +function __init__ () + return {hue=0} +end + +function sethue () + if call.msatoshi < 1000000 then + error('pay at least 1000 sats!') + end + + if type(call.payload.hue) ~= 'number' then + error('hue is not a number!') + end + + if call.payload.hue < 0 or call.payload.hue > 360 then + error('hue is out of the 0~360 range!') + end + + contract.state.hue = call.payload.hue + contract.send('86fa35a3d26f3c0f332e3e812420057cf0e6cf997c5be1c548066a09c634dafe', call.msatoshi) +end diff --git a/contract/ledgr.json b/contract/ledgr.json new file mode 100644 index 0000000..9e26dfe --- /dev/null +++ b/contract/ledgr.json @@ -0,0 +1 @@ +{} \ No newline at end of file diff --git a/contract/state.json b/contract/state.json new file mode 100644 index 0000000..52884a3 --- /dev/null +++ b/contract/state.json @@ -0,0 +1,3 @@ +{ + "hue": 130 +} \ No newline at end of file diff --git a/index.html b/index.html new file mode 100644 index 0000000..380d0e5 --- /dev/null +++ b/index.html @@ -0,0 +1,239 @@ + + + + + + + + + + Ledgr + + +
+ + + + diff --git a/ledgr.json b/ledgr.json new file mode 100644 index 0000000..9e26dfe --- /dev/null +++ b/ledgr.json @@ -0,0 +1 @@ +{} \ No newline at end of file diff --git a/nostr.json b/nostr.json new file mode 100644 index 0000000..45592be --- /dev/null +++ b/nostr.json @@ -0,0 +1 @@ +{"id":"100c04247b3881f4c80615c80abb426ed27f803aa7efef39ae9ff9f8970c63a7","pubkey":"86fa35a3d26f3c0f332e3e812420057cf0e6cf997c5be1c548066a09c634dafe","created_at":1720642724,"kind":30617,"tags":[["d","sethue"],["relays","wss://npub.info/"],["c","txo:tbtc4:e0a84ec838712ddec78c57a7dc1ce65b777cbf58bfa861f289e3533a60399493:0"],["clone","http://git.melvincarvalho.com/smart/32.git"]],"content":"","sig":"0fb78e651f8ebe33a864dd7cf6e0de851220da4de062334e654564ef9db7c5235df7a9f95188769e2a4ec4047c71f6c28cdef0049684f631ac894ecfb8f31081"}