@@ -0,0 +1,49 @@
1+import { hex2patp } from 'urbit-ob';
2+
3+import jssha256 from 'js-sha256';
4+
5+function shax(buf) {
6+ const hashed = jssha256.sha256.array(buf);
7+ return Buffer.from(hashed);
8+}
9+
10+function hexToArrayBuffer(hex) {
11+ return Buffer.from(hex, 'hex').reverse();
12+}
13+
14+function buf2hex(buf) {
15+ return Buffer.from(buf)
16+ .reverse()
17+ .toString('hex');
18+}
19+
20+function shas(buf, salt) {
21+ return shax(xor(salt, shax(buf)));
22+}
23+
24+function xor(a, b) {
25+ const length = Math.max(a.byteLength, b.byteLength);
26+ const result = new Uint8Array(length);
27+ for (let i = 0; i < length; i++) {
28+ result[i] = a[i] ^ b[i];
29+ }
30+ return result;
31+}
32+
33+function shaf(buf, salt) {
34+ const result = shas(buf, salt);
35+ const halfway = result.length / 2;
36+ const front = result.slice(0, halfway);
37+ const back = result.slice(halfway, result.length);
38+ return xor(front, back);
39+}
40+
41+export const generateCode = async privKeyHex => {
42+ const privKey = hexToArrayBuffer(privKeyHex);
43+ const salt = hexToArrayBuffer('73736170');
44+ const hash = shax(privKey);
45+ const result = shaf(hash, salt);
46+ const half = result.slice(0, result.length / 2);
47+
48+ return hex2patp(buf2hex(half));
49+};