on c9bf94f help sign in
bridge/src/lib/keys.js
224 lines 5.5 kB history raw
1import BN from 'bn.js';
2import { Just, Nothing } from 'folktale/maybe';
4import * as noun from '../nockjs/noun';
5import * as serial from '../nockjs/serial';
6import * as kg from 'urbit-key-generation';
8import { addHexPrefix, eqAddr } from './utils/address';
9import { shas } from './networkCode';
11// the curve param for the network keys
12export const NETWORK_KEY_CURVE_PARAMETER = '42';
13// the current crypto suite version
14export const CRYPTO_SUITE_VERSION = 1;
16export const CURVE_ZERO_ADDR =
17 '0x0000000000000000000000000000000000000000000000000000000000000000';
19// ctsy joemfb
20const b64 = buf => {
21 let hex = buf.reverse().toString('hex');
22 let n = new BN(hex, 'hex');
23 let c = [];
24 while (1 === n.cmpn(0)) {
25 c.push(n.andln(0x3f));
26 n = n.shrn(6);
27 }
29 // prettier-ignore
30 const trans = j =>
31 10 > j
32 ? j + 48
33 : 36 > j
34 ? j + 87
35 : 62 > j
36 ? j + 29
37 : 62 === j
38 ? 45
39 : 126;
41 return (
42 '0w' +
43 c.reduce(
44 (a, b, i) =>
45 String.fromCharCode(trans(b)) + (i && 0 === i % 5 ? '.' : '') + a,
46 ''
47 )
48 );
49};
51const jam = seed => {
52 const hex = serial
53 .jam(seed)
54 .toString()
55 .slice(2);
56 const pad = hex.length % 2 === 0 ? hex : '0' + hex;
57 return Buffer.from(pad, 'hex').reverse();
58};
60// simple alias to avoid importing kg into component scope
61export const deriveNetworkKeys = seed => kg.deriveNetworkKeys(seed);
63export const createRing = pair =>
64 pair.crypt.private + pair.auth.private + NETWORK_KEY_CURVE_PARAMETER;
66/**
67 * @param {object} pair
68 * @param {number} point
69 * @param {number} revision
70 * @return {string}
71 */
72export const compileNetworkingKey = (pair, point, revision) => {
73 const bnsec = new BN(createRing(pair), 'hex');
75 const sed = noun.dwim(
76 noun.Atom.fromInt(point),
77 noun.Atom.fromInt(revision),
78 noun.Atom.fromString(bnsec.toString()),
79 noun.Atom.yes
80 );
82 return b64(jam(sed));
83};
85/**
86 * @param {object} urbitWallet
87 * @param {number} revision
88 * @return {Promise<Maybe<string>>}
89 */
90export const deriveNetworkSeedFromUrbitWallet = async (
91 urbitWallet,
92 revision = 1
93) => {
94 return await deriveNetworkSeedFromMnemonic(
95 urbitWallet.management.seed,
96 urbitWallet.meta.passphrase,
97 revision
98 );
99};
101/**
102 * @param {Maybe<any>} wallet
103 * @param {string} authMnemonic
104 * @param {object} details
105 * @param {number} revision
106 * @return {Promise<Maybe<string>>}
107 */
108export const deriveNetworkSeedFromManagementMnemonic = async (
109 wallet,
110 authMnemonic,
111 details,
112 revision = 1
113) => {
114 const isManagementProxy = eqAddr(wallet.address, details.managementProxy);
116 // the network seed is derivable iff this mnemonic is the management proxy
117 if (isManagementProxy) {
118 return await deriveNetworkSeedFromMnemonic(
119 authMnemonic,
120 wallet.passphrase,
121 revision
122 );
123 }
125 return Nothing();
126};
128/**
129 * @param {string} mnemonic
130 * @param {string} passphrase
131 * @param {number} revision
132 * @return {Promise<Maybe<string>>}
133 */
134const deriveNetworkSeedFromMnemonic = async (
135 mnemonic,
136 passphrase,
137 revision
138) => {
139 //NOTE revision is the point's on-chain revision number. since common uhdw
140 // usage derives the first key at revision/index 0, we need to decrement
141 // the on-chain revision number by one to get the number to derive with.
142 return Just(await kg.deriveNetworkSeed(mnemonic, passphrase, revision - 1));
143};
145/**
146 * @param {number} point
147 * @param {string} authToken
148 * @param {number} revision
149 * @return {Maybe<string>}
150 */
151export const deriveNetworkSeedFromAuthToken = (point, authToken, revision) => {
152 //NOTE revision is the point's on-chain revision number.
153 // since deriveNetworkSeedFromMnemonic does this too, we decrement the
154 // revision number by one before deriving from it.
155 const salt = Buffer.from(`revision-${point}-${revision - 1}`);
156 const networkSeed = shas(authToken, salt)
157 .toString('hex')
158 .slice(0, 32);
159 return Just(networkSeed);
160};
162/**
163 * Derives from either a full Urbit Wallet, a current management mnemonic,
164 * or an auth token, in that order of preference.
165 * @return {Promise<Maybe<string>>}
166 */
167export const attemptNetworkSeedDerivation = async ({
168 urbitWallet,
169 wallet,
170 authMnemonic,
171 details,
172 point,
173 authToken,
174 revision,
175}) => {
176 if (Just.hasInstance(urbitWallet)) {
177 return await deriveNetworkSeedFromUrbitWallet(urbitWallet.value, revision);
178 }
180 if (Just.hasInstance(wallet) && Just.hasInstance(authMnemonic)) {
181 const managementSeed = await deriveNetworkSeedFromManagementMnemonic(
182 wallet.value,
183 authMnemonic.value,
184 details,
185 revision
186 );
187 if (Just.hasInstance(managementSeed)) {
188 return managementSeed;
189 }
190 }
192 if (Just.hasInstance(authToken)) {
193 return deriveNetworkSeedFromAuthToken(point, authToken.value, revision);
194 }
196 return Nothing();
197};
199/**
200 *
201 * @param {object} pair
202 * @param {object} details
203 * @return {boolean}
204 */
205export const keysMatchChain = (pair, details) => {
206 const { crypt, auth } = pair;
207 const { encryptionKey, authenticationKey } = details;
209 return (
210 encryptionKey === addHexPrefix(crypt.public) &&
211 authenticationKey === addHexPrefix(auth.public)
212 );
213};
215export const segmentNetworkKey = hex => {
216 if (hex === CURVE_ZERO_ADDR) {
217 return null;
218 }
220 const sl = i => hex.slice(i, i + 4);
221 const rowFrom = i => `${sl(i)}.${sl(i + 4)}.${sl(i + 8)}.${sl(i + 12)}`;
223 return [rowFrom(2), rowFrom(18), rowFrom(34), rowFrom(50)];
224};