|
| 1 | +import DashHd from "dashhd"; |
| 2 | + |
| 3 | +/** @typedef {Number} Uint32 */ |
| 4 | + |
| 5 | +// https://github.com/dashpay/dips/blob/master/dip-0009.md |
| 6 | +const PURPOSE_FEATURE_PATHS = "9'"; // DIP9 |
| 7 | +// https://github.com/dashpay/dips/blob/master/dip-0013.md |
| 8 | +const FEATURE_IDENTITY = "5'"; // DIP13 |
| 9 | +// https://github.com/satoshilabs/slips/blob/master/slip-0044.md |
| 10 | +const COIN_TYPE_DASH = "5'"; // SLIP44 |
| 11 | +const COIN_TYPE_TESTNET = "1'"; // SLIP44 |
| 12 | +const SUB_FEATURE_AUTH = "0'"; // DIP13 |
| 13 | +const KEY_TYPE_ECDSA = "0'"; // DIP13 |
| 14 | +const KEY_TYPE_BLS = "1'"; // DIP13 |
| 15 | +const SUB_FEATURE_REG = "1'"; // DIP13 |
| 16 | +const SUB_FEATURE_TOPUP = "2'"; // DIP13 |
| 17 | + |
| 18 | +// m/9'/<coin-type>/5'/<sub-feature> |
| 19 | +// const PREFIX_IDENT = `m/${PURPOSE_FEATURE_PATHS}/${COIN_TYPE_DASH}/${FEATURE_IDENTITY}`; |
| 20 | +// const PREFIX_IDENT_TESTNET = `m/${PURPOSE_FEATURE_PATHS}/${COIN_TYPE_TESTNET}/${FEATURE_IDENTITY}`; |
| 21 | +// const PREFIX_AUTH = `${PREFIX_IDENT}/${SUB_FEATURE_AUTH}/${KEY_TYPE_ECDSA}`; |
| 22 | +// const PREFIX_AUTH_TESTNET = `${PREFIX_IDENT_TESTNET}/${SUB_FEATURE_AUTH}/${KEY_TYPE_ECDSA}`; |
| 23 | +// const PREFIX_REG = `${PREFIX_IDENT}/${SUB_FEATURE_REG}`; |
| 24 | +// const PREFIX_REG_TESTNET = `${PREFIX_IDENT_TESTNET}/${SUB_FEATURE_REG}`; |
| 25 | +// const PREFIX_TOPUP = `${PREFIX_IDENT}/${SUB_FEATURE_TOPUP}`; |
| 26 | +// const PREFIX_TOPUP_TESTNET = `${PREFIX_IDENT_TESTNET}/${SUB_FEATURE_TOPUP}`; |
| 27 | + |
| 28 | +/** |
| 29 | + * Returns the Identity Auth Wallet, which can be used to derive Identity Auth Keys |
| 30 | + * @param {import('dashhd').HDWallet} walletKey |
| 31 | + * @param {String} path |
| 32 | + */ |
| 33 | +//@ts-expect-error - monkey-patch |
| 34 | +DashHd.deriveIdentAuthWalletPath = async function (walletKey, path) { |
| 35 | + let hdpath = _parseIdentityWallet(path); |
| 36 | + let identAuthKey = await _deriveIdentAuthPath(walletKey, hdpath); |
| 37 | + |
| 38 | + return identAuthKey; |
| 39 | +}; |
| 40 | + |
| 41 | +/** |
| 42 | + * Returns a fully-derived Identity Auth Key |
| 43 | + * @param {import('dashhd').HDWallet} walletKey |
| 44 | + * @param {String} path |
| 45 | + */ |
| 46 | +//@ts-expect-error - monkey-patch |
| 47 | +DashHd.deriveIdentAuthKeyPath = async function (walletKey, path) { |
| 48 | + const INDEX_AUTH_IDENTITY_KEY = 7; |
| 49 | + |
| 50 | + let hdpath = _parseIdentityWallet(path); |
| 51 | + let hasAuthKey = |
| 52 | + hdpath.paths.length > INDEX_AUTH_IDENTITY_KEY && |
| 53 | + hdpath.paths[INDEX_AUTH_IDENTITY_KEY].endsWith("'"); |
| 54 | + if (!hasAuthKey) { |
| 55 | + throw new Error( |
| 56 | + `an auth wallet key path must be in the form \`m/9'/<coin-type>/5'/0'/<key-type>/<identity-index>/<key-index>' where the key index must have a trailing apostrophe`, |
| 57 | + ); |
| 58 | + } |
| 59 | + |
| 60 | + let authWalletKey = await _deriveIdentAuthPath(walletKey, hdpath); |
| 61 | + let authKey = await authWalletKey.deriveAuthKey(hdpath); |
| 62 | + |
| 63 | + return authKey; |
| 64 | +}; |
| 65 | + |
| 66 | +/** |
| 67 | + * @typedef HDPathPlatform |
| 68 | + * @prop {String} path |
| 69 | + * @prop {Array<String>} paths |
| 70 | + * @prop {"m"|"m'"} m |
| 71 | + * @prop {"9'"} purpose |
| 72 | + * @prop {"5'"|"1'"} coinType |
| 73 | + * @prop {"5'"} feature |
| 74 | + * @prop {"0'"|"1'"|"2'"} subFeature |
| 75 | + * @prop {"0'"|"1'"} [keyType] |
| 76 | + * @prop {String} [identityIndex] |
| 77 | + * @prop {String} [keyIndex] |
| 78 | + * @prop {String} [topupIndex] |
| 79 | + * @prop {String} [fundRegIndex] |
| 80 | + */ |
| 81 | + |
| 82 | +/** |
| 83 | + * @param {import('dashhd').HDWallet} walletKey |
| 84 | + * @param {HDPathPlatform} hdpath |
| 85 | + */ |
| 86 | +async function _deriveIdentAuthPath(walletKey, hdpath) { |
| 87 | + const INDEX_KEY_TYPE = 5; |
| 88 | + const INDEX_AUTH_IDENTITY = 6; |
| 89 | + |
| 90 | + let keyType = hdpath.paths[INDEX_KEY_TYPE]; |
| 91 | + let isValidKeyType = keyType === KEY_TYPE_ECDSA || keyType === KEY_TYPE_BLS; |
| 92 | + let hasIdentity = |
| 93 | + hdpath.paths.length > INDEX_AUTH_IDENTITY && |
| 94 | + hdpath.paths[INDEX_AUTH_IDENTITY].endsWith("'"); |
| 95 | + let isIdentity = isValidKeyType && hasIdentity; |
| 96 | + if (!isIdentity) { |
| 97 | + throw new Error( |
| 98 | + `an identity wallet identity auth path must be in the form \`m/9'/<coin-type>/5'/0'/<key-type>/<identity-index>' where coin-type is 5' (DASH mainnet) or 1' (testnet), key-type is 0' (ECDSA) or 1' (BLS), and identity index must have a trailing apostrophe`, |
| 99 | + ); |
| 100 | + } |
| 101 | + |
| 102 | + // 0=m/1=purpose'/2=coin-type'/3=feature'/4=sub-feature'/5=key-type'/6=identity-index' |
| 103 | + let authWalletPath = `m/9'/${hdpath.coinType}/5'/0'/${keyType}/${hdpath.paths[INDEX_AUTH_IDENTITY]}`; |
| 104 | + let _authWalletKey = await DashHd.derivePath(walletKey, authWalletPath); |
| 105 | + |
| 106 | + let authWalletKey = Object.assign(_authWalletKey, { |
| 107 | + /** @param {Uint32} authKeyIndex */ |
| 108 | + deriveAuthKey: async function (authKeyIndex) { |
| 109 | + let authKey = await DashHd.deriveChild( |
| 110 | + _authWalletKey, |
| 111 | + authKeyIndex, |
| 112 | + DashHd.HARDENED, |
| 113 | + ); |
| 114 | + return authKey; |
| 115 | + }, |
| 116 | + }); |
| 117 | + |
| 118 | + return authWalletKey; |
| 119 | +} |
| 120 | + |
| 121 | +/** |
| 122 | + * Returns a fully-derived Identity Registration Funding Key |
| 123 | + * @param {import('dashhd').HDWallet} walletKey |
| 124 | + * @param {String} path |
| 125 | + */ |
| 126 | +//@ts-expect-error - monkey-patch |
| 127 | +DashHd.deriveIdentRegFundKeyPath = async function (walletKey, path) { |
| 128 | + const INDEX_REG_FUND_KEY = 5; |
| 129 | + |
| 130 | + let hdpath = _parseIdentityWallet(path); |
| 131 | + let hasIdentity = |
| 132 | + hdpath.paths.length > INDEX_REG_FUND_KEY && |
| 133 | + !hdpath.paths[INDEX_REG_FUND_KEY].endsWith("'"); |
| 134 | + if (!hasIdentity) { |
| 135 | + throw new Error( |
| 136 | + `an identity wallet identity reg path must be in the form \`m/9'/<coin-type>/5'/1'/<identity-index>' where the identity index MUST NOT have a trailing apostrophe`, |
| 137 | + ); |
| 138 | + } |
| 139 | + |
| 140 | + // 0=m/1=purpose'/2=coin-type'/3=feature'/4=sub-feature'/5=identity-index |
| 141 | + let identRegFundPath = `m/9'/${hdpath.coinType}/5'/1'/${hdpath.paths[INDEX_REG_FUND_KEY]}`; |
| 142 | + let identRegFundKey = await DashHd.derivePath(walletKey, identRegFundPath); |
| 143 | + |
| 144 | + return identRegFundKey; |
| 145 | +}; |
| 146 | + |
| 147 | +// async function deriveIdentTopupWallet(walletKey, path) { |
| 148 | +// } |
| 149 | + |
| 150 | +/** |
| 151 | + * Returns a fully-derived Topup Funding Key |
| 152 | + * @param {import('dashhd').HDWallet} walletKey |
| 153 | + * @param {String} path |
| 154 | + */ |
| 155 | +//@ts-expect-error - monkey-patch |
| 156 | +DashHd.deriveIdentTopupKeyPath = async function (walletKey, path) { |
| 157 | + const INDEX_TOPUP_KEY = 5; |
| 158 | + |
| 159 | + let hdpath = _parseIdentityWallet(path); |
| 160 | + let hasIdentity = |
| 161 | + hdpath.paths.length > INDEX_TOPUP_KEY && |
| 162 | + !hdpath.paths[INDEX_TOPUP_KEY].endsWith("'"); |
| 163 | + if (!hasIdentity) { |
| 164 | + throw new Error( |
| 165 | + `an identity wallet identity reg path must be in the form \`m/9'/<coin-type>/5'/2'/<topup-index>' where the topup index MUST NOT have a trailing apostrophe`, |
| 166 | + ); |
| 167 | + } |
| 168 | + |
| 169 | + // 0=m/1=purpose'/2=coin-type'/3=feature'/4=sub-feature'/5=topup-index |
| 170 | + let identTopupPath = `m/9'/${hdpath.coinType}/5'/2'/${hdpath.paths[INDEX_TOPUP_KEY]}`; |
| 171 | + let identTopupKey = await DashHd.derivePath(walletKey, identTopupPath); |
| 172 | + |
| 173 | + return identTopupKey; |
| 174 | +}; |
| 175 | + |
| 176 | +/** |
| 177 | + * @param {String} path |
| 178 | + * @returns {HDPathPlatform} |
| 179 | + */ |
| 180 | +function _parseIdentityWallet(path) { |
| 181 | + let paths = path.split("/"); |
| 182 | + let [m, purpose, coinType, feature, subFeature] = paths; |
| 183 | + let hasMaster = m === "m" || m === "m'"; |
| 184 | + let hasPurpose = purpose === PURPOSE_FEATURE_PATHS; |
| 185 | + let hasCoinType = |
| 186 | + coinType === COIN_TYPE_DASH || coinType === COIN_TYPE_TESTNET; |
| 187 | + let hasFeature = feature === FEATURE_IDENTITY; |
| 188 | + let hasSubFeature = [ |
| 189 | + SUB_FEATURE_AUTH, |
| 190 | + SUB_FEATURE_REG, |
| 191 | + SUB_FEATURE_TOPUP, |
| 192 | + ].includes(subFeature); |
| 193 | + |
| 194 | + let hasValidPrefix = |
| 195 | + hasMaster && hasPurpose && hasCoinType && hasFeature && hasSubFeature; |
| 196 | + if (!hasValidPrefix) { |
| 197 | + throw new Error( |
| 198 | + `identity wallet paths must be in the form \`m/9'/<coin-type>/5'/<sub-feature>' where coin-type is 5' (DASH mainnet) or 1' (testnet) and sub-feature is 0' (auth), 1' (registration), or 2' (topup)`, |
| 199 | + ); |
| 200 | + } |
| 201 | + |
| 202 | + //@ts-expect-error - these have been type checked |
| 203 | + return { path, paths, m, purpose, coinType, feature, subFeature }; |
| 204 | +} |
| 205 | + |
| 206 | +export default DashHd; |
| 207 | +//@ts-expect-error - monkey patch |
| 208 | +export let deriveIdentTopupKeyPath = DashHd.deriveIdentTopupKeyPath; |
| 209 | +//@ts-expect-error - monkey patch |
| 210 | +export let deriveIdentRegFundKeyPath = DashHd.deriveIdentRegFundKeyPath; |
| 211 | +//@ts-expect-error - monkey patch |
| 212 | +export let deriveIdentAuthKeyPath = DashHd.deriveIdentAuthKeyPath; |
| 213 | +//@ts-expect-error - monkey patch |
| 214 | +export let deriveIdentAuthWalletPath = DashHd.deriveIdentAuthWalletPath; |
0 commit comments