Files
damus/damus/Core/Nostr/ProofOfWork.swift
ericholguin 65a22813a3 refactor: Adding structure
Huge refactor to add better structure to the project.
Separating features with their associated view and model structure.
This should be better organization and will allow us to improve the
overall architecture in the future.

I forsee many more improvements that can follow this change. e.g. MVVM Arch
As well as cleaning up duplicate, unused, functionality.
Many files have global functions that can also be moved or be renamed.

damus/
├── Features/
│   ├── <Feature>/
│   │   ├── Views/
│   │   └── Models/
├── Shared/
│   ├── Components/
│   ├── Media/
│   ├── Buttons/
│   ├── Extensions/
│   ├── Empty Views/
│   ├── ErrorHandling/
│   ├── Modifiers/
│   └── Utilities/
├── Core/
│   ├── Nostr/
│   ├── NIPs/
│   ├── DIPs/
│   ├── Types/
│   ├── Networking/
│   └── Storage/

Signed-off-by: ericholguin <ericholguin@apache.org>
2025-08-06 10:24:00 -07:00

77 lines
1.4 KiB
Swift

//
// ProofOfWork.swift
// damus
//
// Created by William Casarin on 2022-04-11.
//
import Foundation
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;
}
@discardableResult
func hex_decode(_ str: String) -> [UInt8]?
{
if str.count == 0 {
return nil
}
var ret: [UInt8] = []
let chars = Array(str.utf8)
var i: Int = 0
for c in zip(chars, chars[1...]) {
i += 1
if i % 2 == 0 {
continue
}
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
}
func hex_decode_id(_ str: String) -> Data? {
guard str.utf8.count == 64, let decoded = hex_decode(str) else {
return nil
}
return Data(decoded)
}
func hex_decode_noteid(_ str: String) -> NoteId? {
return hex_decode_id(str).map(NoteId.init)
}
func hex_decode_pubkey(_ str: String) -> Pubkey? {
return hex_decode_id(str).map(Pubkey.init)
}
func hex_decode_privkey(_ str: String) -> Privkey? {
return hex_decode_id(str).map(Privkey.init)
}