generated from TBD54566975/tbd-project-template
-
Notifications
You must be signed in to change notification settings - Fork 18
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fully specify retention behavior (#44)
* fully specify retention behavior * update codeowners * update go version * remove test
- Loading branch information
1 parent
d698b72
commit 7aba2fb
Showing
7 changed files
with
186 additions
and
56 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Validating CODEOWNERS rules …
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
package did | ||
|
||
import ( | ||
"crypto/sha256" | ||
"encoding/hex" | ||
"fmt" | ||
"math/big" | ||
"strings" | ||
) | ||
|
||
// computeSHA256Hash computes the SHA-256 hash of a string and returns it as a hexadecimal string. | ||
func computeSHA256Hash(text string) string { | ||
hash := sha256.Sum256([]byte(text)) | ||
return hex.EncodeToString(hash[:]) | ||
} | ||
|
||
// has26LeadingZeros checks if the binary representation of the hash has 26 leading zeros. | ||
func hasLeadingZeros(hash string, difficulty int) bool { | ||
// Convert hex hash to big.Int to handle binary conversion | ||
hashInt := new(big.Int) | ||
hashInt.SetString(hash, 16) | ||
|
||
// Convert to binary string | ||
binaryHash := fmt.Sprintf("%0256b", hashInt) | ||
|
||
target := strings.Repeat("0", difficulty) | ||
|
||
// Check if the first 26 characters are all zeros | ||
return strings.HasPrefix(binaryHash, target) | ||
} | ||
|
||
// computeRetentionProof generates the Retention Proof Hash and checks if it meets the criteria. | ||
func computeRetentionProof(didIdentifier, bitcoinBlockHash string, difficulty, nonce int) (string, bool) { | ||
// Concatenating the DID identifier with the retention value | ||
retentionValue := didIdentifier + (bitcoinBlockHash + fmt.Sprintf("%d", nonce)) | ||
|
||
// Computing the SHA-256 hash | ||
hash := computeSHA256Hash(retentionValue) | ||
|
||
// Checking for the required number of leading zeros according to the difficulty | ||
return hash, hasLeadingZeros(hash, difficulty) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
package did | ||
|
||
import ( | ||
"fmt" | ||
"math" | ||
"testing" | ||
"time" | ||
) | ||
|
||
func TestPOW(t *testing.T) { | ||
// Example usage of computeRetentionProof | ||
didIdentifier := "did:dht:test" | ||
bitcoinBlockHash := "000000000000000000022be0c55caae4152d023dd57e8d63dc1a55c1f6de46e7" | ||
|
||
// 26 leading zeros | ||
difficulty := 26 | ||
|
||
timer := time.Now() | ||
for nonce := 0; nonce < math.MaxInt; nonce++ { | ||
hash, isValid := computeRetentionProof(didIdentifier, bitcoinBlockHash, difficulty, nonce) | ||
if isValid { | ||
fmt.Printf("Hash: %s\n", hash) | ||
fmt.Printf("Valid Retention Proof: %v\n", isValid) | ||
fmt.Printf("Nonce: %d\n", nonce) | ||
break | ||
} | ||
} | ||
fmt.Printf("Time taken: %s\n", time.Since(timer)) | ||
} |
Oops, something went wrong.