Skip to content

Commit

Permalink
fully specify retention behavior (#44)
Browse files Browse the repository at this point in the history
* fully specify retention behavior

* update codeowners

* update go version

* remove test
  • Loading branch information
decentralgabe authored Nov 14, 2023
1 parent d698b72 commit 7aba2fb
Show file tree
Hide file tree
Showing 7 changed files with 186 additions and 56 deletions.
4 changes: 2 additions & 2 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ jobs:
- name: Set up Go
uses: actions/setup-go@v3
with:
go-version: 1.21.3
go-version: 1.21.4

- name: Install Mage
run: go install github.com/magefile/mage
Expand All @@ -41,7 +41,7 @@ jobs:
- name: Set up Go
uses: actions/setup-go@v3
with:
go-version: 1.21.3
go-version: 1.21.4

- name: Install Mage
run: go install github.com/magefile/mage
Expand Down
2 changes: 1 addition & 1 deletion CODEOWNERS
Validating CODEOWNERS rules …
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
# The format is described: https://github.blog/2017-07-06-introducing-code-owners/

# These owners will be the default owners for everything in the repo.
* @decentralgabe csuwildcat
* @decentralgabe @csuwildcat


# -----------------------------------------------
Expand Down
2 changes: 1 addition & 1 deletion impl/build/Dockerfile
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
FROM golang:1.21.3-alpine
FROM golang:1.21.4-alpine

# Create directory for our app inside the container
WORKDIR /app
Expand Down
20 changes: 20 additions & 0 deletions impl/internal/did/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,26 @@ func (c *GatewayClient) GetDIDDocument(id string) (*did.Document, []TypeIndex, e
return d.FromDNSPacket(msg)
}

func (c *GatewayClient) GetMessage(id string) (*dns.Msg, error) {
resp, err := http.Get(c.gatewayURL + "/" + id)
if err != nil {
return nil, errors.Wrap(err, "failed to get did document")
}
defer resp.Body.Close()
if resp.StatusCode != http.StatusOK {
return nil, errors.Errorf("failed to get did document, status code: %d", resp.StatusCode)
}
body, err := io.ReadAll(resp.Body)
if err != nil {
return nil, errors.Wrap(err, "failed to read response body")
}
msg := new(dns.Msg)
if err = msg.Unpack(body[72:]); err != nil {
return nil, util.LoggingErrorMsg(err, "failed to unpack records")
}
return msg, nil
}

// PutDocument puts a bep44.Put message to a did:dht Gateway
func (c *GatewayClient) PutDocument(id string, put bep44.Put) error {
d := DHT(id)
Expand Down
42 changes: 42 additions & 0 deletions impl/internal/did/pow.go
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)
}
29 changes: 29 additions & 0 deletions impl/internal/did/pow_test.go
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))
}
Loading

0 comments on commit 7aba2fb

Please sign in to comment.