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';
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 {number} point
147 * @param {string} authToken
148 * @param {number} revision
149 * @return {Maybe<string>}
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)
159 return Just(networkSeed);
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>>}
167export const attemptNetworkSeedDerivation = async ({
176 if (Just.hasInstance(urbitWallet)) {
177 return await deriveNetworkSeedFromUrbitWallet(urbitWallet.value, revision);
180 if (Just.hasInstance(wallet) && Just.hasInstance(authMnemonic)) {
181 const managementSeed = await deriveNetworkSeedFromManagementMnemonic(
187 if (Just.hasInstance(managementSeed)) {
188 return managementSeed;
192 if (Just.hasInstance(authToken)) {
193 return deriveNetworkSeedFromAuthToken(point, authToken.value, revision);
201 * @param {object} pair
202 * @param {object} details
205export const keysMatchChain = (pair, details) => {
206 const { crypt, auth } = pair;
207 const { encryptionKey, authenticationKey } = details;
210 encryptionKey === addHexPrefix(crypt.public) &&
211 authenticationKey === addHexPrefix(auth.public)
215export const segmentNetworkKey = hex => {
216 if (hex === CURVE_ZERO_ADDR) {
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)];
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