pof

Proof of Freshness: collate proof of an authorship date.

Log | Files | << Repositories


tree 06513e3f68c20e75db363c6d1f00a5ac0c4166e6
parent 79aefa42b04b40d480838f871989caaaa8d50d1a
author esote <esote.net@gmail.com> 1563331974 -0500
committer esote <esote.net@gmail.com> 1563331974 -0500
gpgsig -----BEGIN PGP SIGNATURE-----
 
 iHUEABYIAB0WIQTXAxYDuIzimYoNSPuhTmRAjzzC8gUCXS6NiAAKCRChTmRAjzzC
 8qHvAP4gNhSq4qYbV/aRU4D0wurRZw5eIBNmNU5I4pvR6V3PKAD/acpKszaLLvR5
 fgrDEPxU77e4YnW1Dn0K8117ZNsAiAM=
 =iLxg
 -----END PGP SIGNATURE-----

Programmatically fetch block hash from provided chain height

Fixes reliance on flaky response length for 'blocks' endpoint.

 pof.go | 47 ++++++++++++++++++++++++++++++++++++-----------
 1 file changed, 36 insertions(+), 11 deletions(-)

diff --git a/pof.go b/pof.go
index b41440a..28c7041 100644
--- a/pof.go
+++ b/pof.go
@@ -8,6 +8,7 @@ import (
 	"log"
 	"net/http"
 	"regexp"
+	"strconv"
 	"time"
 )
 
@@ -134,15 +135,16 @@ func nist() {
 }
 
 func btc() {
-	btcURL := "https://blockchain.info/blocks/?format=json"
+	btcHeightURL := "https://blockchain.info/q/getblockcount"
+	btcBlockURL := "https://blockchain.info/block-height/%d?format=json"
 
-	resp, err := http.Get(btcURL)
+	resp, err := http.Get(btcHeightURL)
 
 	if err != nil {
 		log.Fatal(err)
 	}
 
-	btcJSON, err := ioutil.ReadAll(resp.Body)
+	btcHeight, err := ioutil.ReadAll(resp.Body)
 
 	if err != nil {
 		log.Fatal(err)
@@ -152,25 +154,48 @@ func btc() {
 		log.Fatal(err)
 	}
 
-	var btc struct {
+	height, err := strconv.ParseInt(string(btcHeight), 10, 64)
+
+	if err != nil {
+		log.Fatal(err)
+	}
+
+	const depth = 10
+
+	resp, err = http.Get(fmt.Sprintf(btcBlockURL, height-depth))
+
+	if err != nil {
+		log.Fatal(err)
+	}
+
+	btcBlockJSON, err := ioutil.ReadAll(resp.Body)
+
+	if err != nil {
+		log.Fatal(err)
+	}
+
+	if err := resp.Body.Close(); err != nil {
+		log.Fatal(err)
+	}
+
+	var btcBlock struct {
 		Blocks []struct {
 			Hash string
 		}
 	}
 
-	if err := json.Unmarshal(btcJSON, &btc); err != nil {
+	if err := json.Unmarshal(btcBlockJSON, &btcBlock); err != nil {
 		log.Fatal(err)
 	}
 
-	depth := 10
-
-	if len(btc.Blocks) < depth {
-		log.Fatalf("len(btc.Blocks) < %d", depth)
+	if len(btcBlock.Blocks) == 0 {
+		log.Fatal("no blocks found")
 	}
 
 	fmt.Printf("Src: Blockchain.Info [block depth %d] (%s)\n ---\n",
-		depth, btcURL)
-	fmt.Printf("%s\n\n", btc.Blocks[depth].Hash)
+		depth, fmt.Sprintf(btcBlockURL, height-depth))
+
+	fmt.Printf("%s\n\n", btcBlock.Blocks[0].Hash)
 }
 
 func monero() {