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 { eqAddr, addHexPrefix } from './wallet';
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';
21 let hex = buf.reverse().toString('hex');
22 let n = new BN(hex, 'hex');
24 while (1 === n.cmpn(0)) {
25 c.push(n.andln(0x3f));
45 String.fromCharCode(trans(b)) + (i && 0 === i % 5 ? '.' : '') + a,
56 const pad = hex.length % 2 === 0 ? hex : '0' + hex;
57 return Buffer.from(pad, 'hex').reverse();
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;
67 * @param {object} pair
68 * @param {number} point
69 * @param {number} revision
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()),
86 * @param {object} urbitWallet
87 * @param {number} revision
88 * @return {Promise<Maybe<string>>}
90export const deriveNetworkSeedFromUrbitWallet = async (
94 return await deriveNetworkSeedFromMnemonic(
95 urbitWallet.management.seed,
96 urbitWallet.meta.passphrase,
102 * @param {Maybe<any>} wallet
103 * @param {string} authMnemonic
104 * @param {object} details
105 * @param {number} revision
106 * @return {Promise<Maybe<string>>}
108export const deriveNetworkSeedFromManagementMnemonic = async (
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(
129 * @param {string} mnemonic
130 * @param {string} passphrase
131 * @param {number} revision
132 * @return {Promise<Maybe<string>>}
134const deriveNetworkSeedFromMnemonic = async (
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));
146 * @param {string} authToken
147 * @param {number} number
148 * @return {Maybe<string>}
150export const deriveNetworkSeedFromAuthToken = (authToken, revision, point) => {
151 //NOTE revision is the point's on-chain revision number.
152 // since deriveNetworkSeedFromMnemonic does this too, we decrement the
153 // revision number by one before deriving from it.
154 const salt = Buffer.from(`revision-${point}-${revision - 1}`);
155 const networkSeed = shas(authToken, salt)
158 return Just(networkSeed);
162 * @return {Promise<Maybe<string>>}
164export const attemptNetworkSeedDerivation = async ({
170 if (Just.hasInstance(urbitWallet)) {
171 return await deriveNetworkSeedFromUrbitWallet(urbitWallet.value, revision);
174 if (Just.hasInstance(authToken)) {
175 return deriveNetworkSeedFromAuthToken(authToken.value, point, revision);
183 * @param {object} pair
184 * @param {object} details
187export const keysMatchChain = (pair, details) => {
188 const { crypt, auth } = pair;
189 const { encryptionKey, authenticationKey } = details;
192 encryptionKey === addHexPrefix(crypt.public) &&
193 authenticationKey === addHexPrefix(auth.public)
197export const segmentNetworkKey = hex => {
198 if (hex === CURVE_ZERO_ADDR) {
202 const sl = i => hex.slice(i, i + 4);
203 const rowFrom = i => `${sl(i)}.${sl(i + 4)}.${sl(i + 8)}.${sl(i + 12)}`;
205 return [rowFrom(2), rowFrom(18), rowFrom(34), rowFrom(50)];
keysesc to close
- j k
- move down, up
- enter
- open
- esc
- back, or up a level
- 1 2 3 4
- readme, files, commits, refs
- /
- filter
- y
- copy the clone command
- c
- compare the chosen commit with another (or shift-click both)
- ?
- show or hide this list