Last active
June 15, 2023 04:34
-
-
Save SparK-Cruz/7dba2abf74e6ad0bd0489fee12d47d75 to your computer and use it in GitHub Desktop.
Nano Address Stamps
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
<!DOCTYPE html> | |
<html> | |
<head> | |
<title>Nano Stamp</title> | |
<style> | |
img { | |
image-rendering: pixelated; | |
border: 1px solid black; | |
} | |
</style> | |
</head> | |
<body> | |
<img id="main" width="135" height="135" /> | |
<img id="four" width="36" height="36" /> | |
<img id="two" width="18" height="18" /> | |
<img id="original" width="9" height="9" /> | |
<div> | |
<input type="text" id="address" /> | |
</div> | |
<div style="margin-top: 15px; font-family: sans-serif;"> | |
<span id="presented"></span> | |
<img id="presented-img" width="27", height="27" /> | |
</div> | |
<div> | |
<p>Examples:</p> | |
<dl> | |
<dd> | |
<a href="javascript:fillAddress('nano_3ntf6crkan6114rfb39d51udqdw4mrbt1x7n8uphx44ojhxcjo3exhk6dsme')">Spark</a> | |
</dd> | |
<dd> | |
<a href="javascript:fillAddress('nano_1ookerz3adg5rxc4zwwoshim5yyyihf6dpogjihwwq6ksjpq7ea4fuam5mmc')">NanoLooker</a> | |
</dd> | |
<dd> | |
<a href="javascript:fillAddress('nano_3n7ky76t4g57o9skjawm8pprooz1bminkbeegsyt694xn6d31c6s744fjzzz')">HumbleNano.io</a> | |
</dd> | |
<dd> | |
<a href="javascript:fillAddress('nano_3msc38fyn67pgio16dj586pdrceahtn75qgnx7fy19wscixrc8dbb3abhbw6')">Gr0vity</a> | |
</dd> | |
<dd> | |
<a href="javascript:fillAddress('nano_1b9wguhh39at8qtm93oghd6r4f4ubk7zmqc9oi5ape6yyz4s1gamuwn3jjit')">Trustable - NN1</a> | |
</dd> | |
<dd> | |
<a href="javascript:fillAddress('nano_1j78msn5omp8jrjge8txwxm4x3smusa1cojg7nuk8fdzoux41fqeeogg5aa1')">NanoBrasil</a> | |
</dd> | |
</dl> | |
</div> | |
<script src="nano_stamp.js"></script> | |
<script> | |
const addrInput = document.getElementById('address'); | |
const main = document.getElementById('main'); | |
const four = document.getElementById('four'); | |
const two = document.getElementById('two'); | |
const original = document.getElementById('original'); | |
const presented = document.getElementById('presented'); | |
const presentedImg = document.getElementById('presented-img'); | |
const draw = () => { | |
generateStamp(addrInput.value) | |
.then(stamp => { | |
main.src = stamp; | |
four.src = stamp; | |
two.src = stamp; | |
original.src = stamp; | |
presented.innerText = addrInput.value; | |
presentedImg.src = stamp; | |
}); | |
} | |
addrInput.addEventListener('blur', draw); | |
function fillAddress(str) { | |
addrInput.value = str; | |
draw(); | |
} | |
</script> | |
</body> | |
</html> |
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
const generateStamp = (function(){ | |
function generate(address) { | |
return new Promise((resolve, reject) => { | |
truncated = address.replace(/^(?:nano_|xrb_)/, ''); | |
if (truncated.length != 60) { | |
reject("Invalid address!"); | |
return; | |
} | |
sequence = normalize(truncated) | |
rgb = sequence.splice(-3); | |
extra = sequence.splice(-8); | |
points = sequence.map((p, i) => ({ | |
color: particle(rgb, p), | |
x: i % 7 + 1, | |
y: Math.floor(i / 7) + 1, | |
})); | |
[0, 7, 14, 21, 28, 35, 42, 48].forEach((v, i) => { | |
points[v].color = points[v].color.map((c, j) => j <= 0 ? c * extra[i] : c); | |
}); | |
const buffer = document.createElement("canvas"); | |
buffer.width = 135; | |
buffer.height = 135; | |
const ctx = buffer.getContext('2d'); | |
ctx.scale(15, 15); | |
ctx.fillStyle = '#fbfbfc'; | |
ctx.fillRect(0, 0, 9, 9); | |
ctx.imageSmoothingEnabled = false; | |
points.push(...drawX(extra[7])); | |
for (point of points) { | |
ctx.fillStyle = `rgba(${point.color[0]}, ${point.color[1]}, ${point.color[2]}, ${point.color[3] || 1})`; | |
ctx.fillRect(point.x, point.y, 1, 1); | |
} | |
resolve(buffer.toDataURL("image/png")); | |
}); | |
} | |
function normalize(str) { | |
return str.split('').map(c => Math.ceil(((n = c.charCodeAt(0) - 96) > 0 ? n + 9 : n + 48) / 35 * 255) / 255) | |
} | |
function particle(rgb, value) { | |
return rgb.map(c => Math.ceil(((1 - c) * value + c) * 255)); | |
} | |
function drawX(value) { | |
const index = Math.floor(value * 49); | |
const center = { | |
x: index % 7 + 1, | |
y: Math.floor(index / 7) + 1, | |
}; | |
const color = [0, 0, 0, .9]; | |
const points = [[-2,-3],[2,-3],[-1,-2],[1,-2],[0,-1],[-1,0],[0,0],[1,0],[0,1],[-1,2],[1,2],[-2,3],[2,3]]; | |
return points.map(p => ({ | |
color: color, | |
x: p[0] + center.x, | |
y: p[1] + center.y, | |
})); | |
} | |
return generate; | |
})(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment