Skip to content

Instantly share code, notes, and snippets.

@michel47
Last active May 12, 2021 07:57
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
Star You must be signed in to star a gist
Save michel47/a73b711bbc48340dec757cad1f6d81d3 to your computer and use it in GitHub Desktop.
token discovery
// running javascript to add the page content to IPFS
// then findo out who has the hash (peers)
var peers = [];
console.log('this app is running from %s!',location.origin);
el = document.getElementsByTagName('h3')[0]; el.innerText = el.innerText.replace(':origin',location.origin)
document.getElementsByTagName('button')[0].addEventListener('click',update,false);
document.getElementsByName('token')[0].addEventListener('change',update,false);
document.getElementsByTagName('ul')[0].addEventListener('click',click,true);
function click(ev) {
let el = ev.target;
console.log('href:', el.dataset.href);
let qm = el.dataset.href.replace(/.*ipfs\//,'')
let token = document.getElementsByName('token')[0];
token.value = qm;
update({target: token});
return qm;
}
function update() {
const api_url = 'http://127.0.0.1:5001/api/v0/';
el = document.getElementsByName('token')[0];
let token = el.value;
console.log('token:', token);
// ipfs dht findprovs /ipfs/QmT67qHTboJQN6VUw5Eo5YFyAAfTgJAMTjWtaiWUpB6kvz
return fetch(api_url+'dht/findprovs?arg='+token, { method: 'POST' }).
then( resp => { console.log(resp.body); return resp.body.getReader(); }).
then( reader => { return readStream(reader,display_peers); }).
catch(console.error);
};
function ipfsPinAdd(hash) {
return fetch(api_url+'pin/add?arg='+token,{ method: 'POST', body: form }).
then(resp => resp.json()).
then(json => {
console.debug('json:',json);
let qm = json.Hash;
let el = document.getElementsByTagName('span')[0];
el.innerHTML = `qm : <a href=http://127.0.0.1:8080/ipfs/${qm}/>/ipfs/${qm}/</a>`;
});
}
function display_peers(streamed_objs) {
let peerids = [];
if (streamed_objs.length > 0) {
peers.push(...streamed_objs.filter( (o) => o.Type == 4 ));
let d = document.getElementById('peers');
peerids = peers.map((o) => o.Responses[0].ID );
let buf = '<ol>';
for (p of peerids) {
if (typeof(p) != 'undefined') {
buf += `<li><a href=https://duckduckgo.com/?q=%2B%22${p}%22>${shortqm(p)}</a> <a href="http://127.0.0.1:8080/ipns/${p}">đź”—</a>`;
}
}
buf += '</ol>';
d.innerHTML = buf;
}
return peerids;
}
function shortqm(qm) {
if (typeof(qm) != 'undefined') {
return qm.substr(0,6)+'...'+qm.substr(-3)
} else {
return 'QmezgbyqFCEybpSxCtGNxfRD9uDxC53aNv5PfhB3fGUhJZ';
}
}
function readStream(reader, callback) {
peers = [];
let read;
var buf = ''
return reader.read().
then(read = ({ value, done }) => {
if (done) return buf;
console.debug('value.buffer:',value.buffer)
if (buf != '') { console.debug('buf:',buf) }
buf += String.fromCharCode.apply(String, value);
// spliting the NDJSON
let lines = buf.replace(/(\n|\r)+$/, '').split("\n")
buf = (lines[lines.length-1].match(/}$/)) ? '' : lines.pop();
let objs = lines.map(JSON.parse)
// console.log('objs:',objs)
callback(objs);
if (objs.length > 0) {
return reader.read().then(read).catch(console.warn); // recursion !
} else {
return Promise.reject(String.fromCharCode.apply(String, value));
}
});
}
true;
<meta charset="utf8"><h3>token discovery (from :origin)</h3>
<input name=token value=QmT67qHTboJQN6VUw5Eo5YFyAAfTgJAMTjWtaiWUpB6kvz placeholder="token hash" size=43>
<button>discover</button>
<p>click on one of the hashes below:
<ul>
<li><a data-href=https://gateway.ipfs.io/ipfs/QmT67qHTboJQN6VUw5Eo5YFyAAfTgJAMTjWtaiWUpB6kvz>QmT67qHTboJQN6VUw5Eo5YFyAAfTgJAMTjWtaiWUpB6kvz</a>
<li><a data-href=https://gateway.ipfs.io/ipfs/QmVXVhiAhbjGczSa6cNSLjsdzYxKK9ji7kaXeP5YEwZM6e>QmVXVhiAhbjGczSa6cNSLjsdzYxKK9ji7kaXeP5YEwZM6e</a>
<li><a data-href=https://ipfs.blockring™.ml/ipfs/QmWbfNsEPS5tU2SY43EjH1qM3PhY2bAiXXaB9gfNmTVo2Q>QmWbfNsEPS5tU2SY43EjH1qM3PhY2bAiXXaB9gfNmTVo2Q</a>
<li><a data-href=https://ipfs.blockring™.ml/ipfs/QmdUt2gqucRaV7UHStZH9iVHaKvkqoBHYHJ4tSjMy6zbL7>QmdUt2gqucRaV7UHStZH9iVHaKvkqoBHYHJ4tSjMy6zbL7</a>
<li><a data-href=https://ipfs.blockring™.ml/ipfs/QmV8vKU8HBb5yQH5kMJm4CJvZJQZaqTWupBhxLG8yFBr3b>QmV8vKU8HBb5yQH5kMJm4CJvZJQZaqTWupBhxLG8yFBr3b</a>
<li><a data-href=https://ipfs.blockring™.ml/ipfs/QmcsxigyokC3AZhz7VJuVDjeLqJuc8EAAmPZ7q9uRG4Snq>QmcsxigyokC3AZhz7VJuVDjeLqJuc8EAAmPZ7q9uRG4Snq</a>
<li><a data-href=https://ipfs.blockring™.ml/ipfs/QmPghZ7K18aq8fCTiNvecorcdxRjvzUpnHRCZgCWpeHBC7>QmPghZ7K18aq8fCTiNvecorcdxRjvzUpnHRCZgCWpeHBC7</a>
<li><a data-href=https://ipfs.blockring™.ml/ipfs/QmTAWYwQcY9biYYDpdcMwzHjTrmMz1Gy8M4MmMok3UpHyc>QmTAWYwQcY9biYYDpdcMwzHjTrmMz1Gy8M4MmMok3UpHyc</a>
<li><a data-href=https://ipfs.blockring™.ml/ipfs/QmejvEPop4D7YUadeGqYWmZxHhLc4JBUCzJJHWMzdcMe2y>QmejvEPop4D7YUadeGqYWmZxHhLc4JBUCzJJHWMzdcMe2y</a>
<li><a data-href=https://ipfs.blockring™.ml/ipfs/Qmb72iqhbixFqzgRJQctFHcgcFwGeH5PbyCFKVL9U5jWVu>Qmb72iqhbixFqzgRJQctFHcgcFwGeH5PbyCFKVL9U5jWVu</a>
<li><a data-href=https://ipfs.blockring™.ml/ipfs/QmbVpHduortCFTYUkrWPRzgGwZAU6aRYFJ82sXs2WFaHn1>QmbVpHduortCFTYUkrWPRzgGwZAU6aRYFJ82sXs2WFaHn1</a>
<li><a data-href=https://ipfs.blockring™.ml/ipfs/QmfJfHEYqKF25nEfzackYTcd25AdYnJ3jRswyS6iDqQpKG>QmfJfHEYqKF25nEfzackYTcd25AdYnJ3jRswyS6iDqQpKG</a>
<li><a data-href=https://ipfs.blockring™.ml/ipfs/QmR9VbiNNxNVkfeN75sxRLPhGcoQtY3c8F4DeXjrsj5gnk>QmR9VbiNNxNVkfeN75sxRLPhGcoQtY3c8F4DeXjrsj5gnk</a>
<li><a data-href=https://ipfs.blockring™.ml/ipfs/bafkrohg4mbrvlhs7tzds4276d27xmjv3fyal7uttr23z3mohal5q>bafkrohg4mbrvlhs7tzds4276d27xmjv3fyal7uttr23z3mohal5q</a>
<li><a data-href=https://ipfs.blockring™.ml/ipfs/Qmc4yrLfmtgF2Nh1cw67yE7vDwSiaJC8dfiRj7RRqyCCnj>Qmc4yrLfmtgF2Nh1cw67yE7vDwSiaJC8dfiRj7RRqyCCnj</a>
<li><a data-href=https://ipfs.blockring™.ml/ipfs/QmcPPv5qQjhBQtrWkWepYyNqNwEX4Jyhi4dGW7hjKAyeDs>QmcPPv5qQjhBQtrWkWepYyNqNwEX4Jyhi4dGW7hjKAyeDs</a>
<li><a data-href=https://ipfs.blockring™.ml/ipfs/QmVHWfUiCDuWGiev4ZYRpFgP4oH7GPiF8vEVoZ6bHCwSd7>QmVHWfUiCDuWGiev4ZYRpFgP4oH7GPiF8vEVoZ6bHCwSd7</a>
<li><a data-href=https://ipfs.blockring™.ml/ipfs/z6CfPs4KMZEFsrvodUfZTtYjMWpxAbPsdZGzv3o1wtYj>z6CfPs4KMZEFsrvodUfZTtYjMWpxAbPsdZGzv3o1wtYj</a>
<li><a data-href=https://ipfs.blockring™.ml/ipfs/v05ah8g38clltu41j8dalkejr2sfc3f852e2bd6kdp6vuoh25t92cu68osp8f1dnb072ljc6vu09m4tarogiij84srnpua8p9jv2rkeg0g260m>v05ah8g38clltu41j8dalkejr2sfc3f852e2bd6kdp6vuoh25t92cu68osp8f1dnb072ljc6vu09m4tarogiij84srnpua8p9jv2rkeg0g260m</a>
<li><a data-href=https://ipfs.blockring™.ml/ipfs/QmSX3f5QM41mJyPwaxECpNcam8XtEhTybkPm7FB71Kybgb>QmSX3f5QM41mJyPwaxECpNcam8XtEhTybkPm7FB71Kybgb</a>
</ul>
<pre><code># running the following command with give us a hint on IPFS gateways...
ipfs dht findprovs /ipfs/QmT67qHTboJQN6VUw5Eo5YFyAAfTgJAMTjWtaiWUpB6kvz
</code></pre>
<div id=peers></div>
<script src=app.js></script>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment