|
6 | 6 | * @prop {HDFingerprint} _fingerprint
|
7 | 7 | * @prop {HDFromSeed} fromSeed
|
8 | 8 | * @prop {HDFromXKey} fromXKey
|
| 9 | + * @prop {HDToId} toId |
| 10 | + * @prop {HDToIdBytes} toIdBytes |
9 | 11 | * @prop {HDToAddr} toAddr
|
10 | 12 | * @prop {HDToWif} toWif
|
11 | 13 | * @prop {HDToXPrv} toXPrv
|
|
36 | 38 | * @prop {HDHasher} ripemd160sum
|
37 | 39 | * @prop {HDHasher} sha256sum
|
38 | 40 | * @prop {HDHasher} sha512hmac
|
| 41 | + * @prop {HDBase64Url} bytesToBase64Url |
39 | 42 | * @prop {HDKeyToKey} toPublicKey
|
40 | 43 | */
|
41 | 44 |
|
@@ -219,6 +222,23 @@ var DashHd = ("object" === typeof module && exports) || {};
|
219 | 222 | return new Uint8Array(sig);
|
220 | 223 | };
|
221 | 224 |
|
| 225 | + /** @type {HDBase64Url} */ |
| 226 | + Utils.bytesToBase64Url = function (bytes) { |
| 227 | + let bins = []; |
| 228 | + |
| 229 | + for (let i = 0; i < bytes.length; i += 1) { |
| 230 | + let b = bytes[i]; |
| 231 | + let s = String.fromCodePoint(b); |
| 232 | + bins.push(s); |
| 233 | + } |
| 234 | + |
| 235 | + let str = bins.join(""); |
| 236 | + let b64 = btoa(str); |
| 237 | + let b64url = b64.replace(/\+/g, "-").replace(/\//g, "_").replace(/=/g, ""); |
| 238 | + |
| 239 | + return b64url; |
| 240 | + }; |
| 241 | + |
222 | 242 | /** @type {HDSecureErase} */
|
223 | 243 | Utils.secureErase = function (buf) {
|
224 | 244 | if (!Crypto.getRandomValues) {
|
@@ -652,6 +672,23 @@ var DashHd = ("object" === typeof module && exports) || {};
|
652 | 672 | return hdkey;
|
653 | 673 | };
|
654 | 674 |
|
| 675 | + DashHd.toId = async function (hdkey) { |
| 676 | + let idBytes = await DashHd.toIdBytes(hdkey); |
| 677 | + let id = Utils.bytesToBase64Url(idBytes); |
| 678 | + |
| 679 | + return id; |
| 680 | + }; |
| 681 | + |
| 682 | + DashHd.toIdBytes = async function (hdkey) { |
| 683 | + let xpubBytes = await DashHd.toXPubBytes(hdkey); |
| 684 | + |
| 685 | + let hashBuffer = await Crypto.subtle.digest("SHA-256", xpubBytes); |
| 686 | + let idBuffer = hashBuffer.slice(0, 8); |
| 687 | + let idBytes = new Uint8Array(idBuffer); |
| 688 | + |
| 689 | + return idBytes; |
| 690 | + }; |
| 691 | + |
655 | 692 | DashHd.toPublic = function (_hdkey) {
|
656 | 693 | let hdkey = Object.assign({}, _hdkey);
|
657 | 694 | hdkey.privateKey = null;
|
@@ -835,6 +872,18 @@ if ("object" === typeof module) {
|
835 | 872 | * @returns {Promise<String>}
|
836 | 873 | */
|
837 | 874 |
|
| 875 | +/** |
| 876 | + * @callback HDToId |
| 877 | + * @param {HDKey} hdkey |
| 878 | + * @returns {Promise<String>} |
| 879 | + */ |
| 880 | + |
| 881 | +/** |
| 882 | + * @callback HDToIdBytes |
| 883 | + * @param {HDKey} hdkey |
| 884 | + * @returns {Promise<Uint8Array>} |
| 885 | + */ |
| 886 | + |
838 | 887 | /**
|
839 | 888 | * @callback HDToXKeyBytes
|
840 | 889 | * @param {HDKey} hdkey
|
@@ -878,6 +927,12 @@ if ("object" === typeof module) {
|
878 | 927 | * @returns {Promise<String>}
|
879 | 928 | */
|
880 | 929 |
|
| 930 | +/** |
| 931 | + * @callback HDBase64Url |
| 932 | + * @param {Uint8Array} bytes |
| 933 | + * @returns {String} - URL-Safe Base64 Encoding |
| 934 | + */ |
| 935 | + |
881 | 936 | /**
|
882 | 937 | * @callback HDHasher
|
883 | 938 | * @param {Uint8Array} bytes
|
|
0 commit comments