commit e0059388e8d76147fa192953ff5be7b0aa1d6086
parent 1f046ac021ed5ea18d7d13a95cca7576c8c0bb3e
Author: William Casarin <jb55@jb55.com>
Date: Mon, 11 Apr 2022 11:32:30 -0700
calculate proof of work on events
Signed-off-by: William Casarin <jb55@jb55.com>
Diffstat:
4 files changed, 101 insertions(+), 0 deletions(-)
diff --git a/damus.xcodeproj/project.pbxproj b/damus.xcodeproj/project.pbxproj
@@ -17,6 +17,7 @@
4C75EFB528049D790006080F /* Relay.swift in Sources */ = {isa = PBXBuildFile; fileRef = 4C75EFB428049D790006080F /* Relay.swift */; };
4C75EFB728049D990006080F /* RelayPool.swift in Sources */ = {isa = PBXBuildFile; fileRef = 4C75EFB628049D990006080F /* RelayPool.swift */; };
4C75EFB92804A2740006080F /* EventView.swift in Sources */ = {isa = PBXBuildFile; fileRef = 4C75EFB82804A2740006080F /* EventView.swift */; };
+ 4C75EFBB2804A34C0006080F /* ProofOfWork.swift in Sources */ = {isa = PBXBuildFile; fileRef = 4C75EFBA2804A34C0006080F /* ProofOfWork.swift */; };
4CE6DEE727F7A08100C66700 /* damusApp.swift in Sources */ = {isa = PBXBuildFile; fileRef = 4CE6DEE627F7A08100C66700 /* damusApp.swift */; };
4CE6DEE927F7A08100C66700 /* ContentView.swift in Sources */ = {isa = PBXBuildFile; fileRef = 4CE6DEE827F7A08100C66700 /* ContentView.swift */; };
4CE6DEEB27F7A08200C66700 /* Assets.xcassets in Resources */ = {isa = PBXBuildFile; fileRef = 4CE6DEEA27F7A08200C66700 /* Assets.xcassets */; };
@@ -56,6 +57,7 @@
4C75EFB428049D790006080F /* Relay.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = Relay.swift; sourceTree = "<group>"; };
4C75EFB628049D990006080F /* RelayPool.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = RelayPool.swift; sourceTree = "<group>"; };
4C75EFB82804A2740006080F /* EventView.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = EventView.swift; sourceTree = "<group>"; };
+ 4C75EFBA2804A34C0006080F /* ProofOfWork.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ProofOfWork.swift; sourceTree = "<group>"; };
4CE6DEE327F7A08100C66700 /* damus.app */ = {isa = PBXFileReference; explicitFileType = wrapper.application; includeInIndex = 0; path = damus.app; sourceTree = BUILT_PRODUCTS_DIR; };
4CE6DEE627F7A08100C66700 /* damusApp.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = damusApp.swift; sourceTree = "<group>"; };
4CE6DEE827F7A08100C66700 /* ContentView.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ContentView.swift; sourceTree = "<group>"; };
@@ -116,6 +118,7 @@
4C75EFB228049D640006080F /* NostrEvent.swift */,
4C75EFB428049D790006080F /* Relay.swift */,
4C75EFB628049D990006080F /* RelayPool.swift */,
+ 4C75EFBA2804A34C0006080F /* ProofOfWork.swift */,
);
path = Nostr;
sourceTree = "<group>";
@@ -329,6 +332,7 @@
4CE6DEE727F7A08100C66700 /* damusApp.swift in Sources */,
4C75EFA427FA577B0006080F /* PostView.swift in Sources */,
4C75EFB528049D790006080F /* Relay.swift in Sources */,
+ 4C75EFBB2804A34C0006080F /* ProofOfWork.swift in Sources */,
);
runOnlyForDeploymentPostprocessing = 0;
};
diff --git a/damus/Nostr/NostrEvent.swift b/damus/Nostr/NostrEvent.swift
@@ -25,6 +25,7 @@ struct NostrEvent: Decodable, Identifiable {
let tags: [[String]]
let content: String
let sig: String
+ var pow: Int?
}
func decode_nostr_event(txt: String) -> NostrResponse? {
diff --git a/damus/Nostr/NostrResponse.swift b/damus/Nostr/NostrResponse.swift
@@ -25,6 +25,7 @@ enum NostrResponse: Decodable {
print(error)
throw error
}
+ ev.pow = count_hash_leading_zero_bits(ev.id)
self = .event(sub_id, ev)
return
} else if typ == "NOTICE" {
diff --git a/damus/Nostr/ProofOfWork.swift b/damus/Nostr/ProofOfWork.swift
@@ -0,0 +1,95 @@
+//
+// ProofOfWork.swift
+// damus
+//
+// Created by William Casarin on 2022-04-11.
+//
+
+import Foundation
+
+
+func zero_bits(_ argb: UInt8) -> Int
+{
+ var b = argb
+ var n: Int = 0;
+
+ if b == 0 {
+ return 8;
+ }
+
+ while true {
+ b >>= 1;
+ if b != 0 {
+ n += 1;
+ } else {
+ break
+ }
+ }
+
+ return 7-n;
+}
+
+func count_hash_leading_zero_bits(_ hash: String) -> Int?
+{
+ guard let decoded = hex_decode(hash) else {
+ return nil
+ }
+ return count_leading_zero_bits(decoded)
+}
+
+/* find the number of leading zero bits in a hash */
+func count_leading_zero_bits(_ hash: [UInt8]) -> Int
+{
+ var bits: Int = 0
+ var total: Int = 0
+
+ for c in hash {
+ bits = zero_bits(c)
+ total += bits
+ if (bits != 8) {
+ break
+ }
+ }
+
+ return total
+}
+
+
+func char_to_hex(_ c: UInt8) -> UInt8?
+{
+ // 0 && 9
+ if (c >= 48 && c <= 57) {
+ return c - 48 // 0
+ }
+ // a && f
+ if (c >= 97 && c <= 102) {
+ return c - 97 + 10;
+ }
+ // A && F
+ if (c >= 65 && c <= 70) {
+ return c - 65 + 10;
+ }
+ return nil;
+}
+
+
+func hex_decode(_ str: String) -> [UInt8]?
+{
+ var ret: [UInt8] = []
+ let chars = Array(str.utf8)
+ for c in zip(chars, chars[1...]) {
+ guard let c1 = char_to_hex(c.0) else {
+ return nil
+ }
+
+ guard let c2 = char_to_hex(c.1) else {
+ return nil
+ }
+
+ ret.append((c1 << 4) | c2)
+ }
+
+ return ret
+}
+
+