on d760cbf help sign in
bridge/src/lib/keys.js
168 lines 3.8 kB history raw
1import BN from 'bn.js';
2import { Just, Nothing } from 'folktale/maybe';
3import * as need from './need';
5import * as noun from '../nockjs/noun';
6import * as serial from '../nockjs/serial';
7import * as kg from 'urbit-key-generation/dist';
9import { eqAddr, addHexPrefix } from './wallet';
11const NETWORK_KEY_CURVE_PARAMETER = '42';
13// ctsy joemfb
14const b64 = buf => {
15 let hex = buf.reverse().toString('hex');
16 let n = new BN(hex, 'hex');
17 let c = [];
18 while (1 === n.cmpn(0)) {
19 c.push(n.andln(0x3f));
20 n = n.shrn(6);
21 }
23 // prettier-ignore
24 const trans = j =>
25 10 > j
26 ? j + 48
27 : 36 > j
28 ? j + 87
29 : 62 > j
30 ? j + 29
31 : 62 === j
32 ? 45
33 : 126;
35 return (
36 '0w' +
37 c.reduce(
38 (a, b, i) =>
39 String.fromCharCode(trans(b)) + (i && 0 === i % 5 ? '.' : '') + a,
40 ''
41 )
42 );
43};
45const jam = seed => {
46 const hex = serial
47 .jam(seed)
48 .toString()
49 .slice(2);
50 const pad = hex.length % 2 === 0 ? hex : '0' + hex;
51 return Buffer.from(pad, 'hex').reverse();
52};
54// simple alias to avoid importing kg into component scope
55export const deriveNetworkKeys = seed => kg.deriveNetworkKeys(seed);
57/**
58 * @param {object} pair
59 * @param {number} point
60 * @param {number} revision
61 * @return {string}
62 */
63export const compileNetworkingKey = (pair, point, revision) => {
64 const bnsec = new BN(
65 pair.crypt.private + pair.auth.private + NETWORK_KEY_CURVE_PARAMETER,
66 'hex'
67 );
69 const sed = noun.dwim(
70 noun.Atom.fromInt(point),
71 noun.Atom.fromInt(revision),
72 noun.Atom.fromString(bnsec.toString()),
73 noun.Atom.yes
74 );
76 return b64(jam(sed));
77};
79/**
80 * @param {Maybe<any>} urbitWallet
81 * @param {number} revision
82 * @return {Promise<Maybe<string>>}
83 */
84export const deriveNetworkSeedFromUrbitWallet = async (
85 urbitWallet,
86 revision = 1
87) => {
88 const _urbitWallet = need.wallet(urbitWallet);
89 return deriveNetworkSeedFromSeed(_urbitWallet.management.seed, revision);
90};
92/**
93 * @param {Maybe<any>} wallet
94 * @param {Maybe<string>} authMnemonic
95 * @param {object} details
96 * @param {number} revision
97 * @return {Promise<Maybe<string>>}
98 */
99export const deriveNetworkSeedFromMnemonic = async (
100 wallet,
101 authMnemonic,
102 details,
103 revision = 1
104) => {
105 const _wallet = need.wallet(wallet);
106 const _mnemonic = need.authMnemonic(authMnemonic);
107 const isManagementProxy = eqAddr(_wallet.address, details.managementProxy);
109 if (isManagementProxy) {
110 return await deriveNetworkSeedFromSeed(_mnemonic, revision);
111 }
113 return Nothing();
114};
116/**
117 * @param {string} seed
118 * @param {number} revision
119 * @return {Promise<Maybe<string>>}
120 */
121const deriveNetworkSeedFromSeed = async (seed, revision) => {
122 const networkSeed = await kg.deriveNetworkSeed(seed, '', revision);
123 // apparently deriveNetworkSeed can return empty string
124 // and perhaps even malformed
125 return networkSeed === '' ? Nothing() : Just(networkSeed);
126};
128/**
129 * @return {Promise<Maybe<string>>}
130 */
131export const attemptNetworkSeedDerivation = async ({
132 urbitWallet,
133 wallet,
134 authMnemonic,
135 details,
136 revision,
137}) => {
138 if (Just.hasInstance(urbitWallet)) {
139 return await deriveNetworkSeedFromUrbitWallet(urbitWallet, revision);
140 }
142 if (Just.hasInstance(wallet) && Just.hasInstance(authMnemonic)) {
143 return await deriveNetworkSeedFromMnemonic(
144 wallet,
145 authMnemonic,
146 details,
147 revision
148 );
149 }
151 return Nothing();
152};
154/**
155 *
156 * @param {object} pair
157 * @param {object} details
158 * @return {boolean}
159 */
160export const keysMatchChain = (pair, details) => {
161 const { crypt, auth } = pair;
162 const { encryptionKey, authenticationKey } = details;
164 return (
165 encryptionKey === addHexPrefix(crypt.public) &&
166 authenticationKey === addHexPrefix(auth.public)
167 );
168};