check description
+ key-derivation/getPairFromPrivate() M key-derivation/generateKeypair to use class Key + class Key for interop between different zkl modules
This commit is contained in:
6 files changed
+147
-32
No files matched your search
@@ -0,0 +1,7 @@
|
|||||||
|
{
|
||||||
|
"semi": true,
|
||||||
|
"singleQuote": false,
|
||||||
|
"tabWidth": 2,
|
||||||
|
"bracketSpacing": true,
|
||||||
|
"trailingComma": "none"
|
||||||
|
}
|
||||||
Vendored
+1
-1
@@ -1 +1 @@
|
|||||||
(()=>{var e={724:function(e,t,r){!function(e,t){"use strict";function i(e,t){if(!e)throw new Error(t||"Assertion failed")}function a(e,t){e.super_=t;var r=function(){};r.prototype=t.prototype,e.prototype=new r,e.prototype.constructor=e}function n(e,t,r){if(n.isBN(e))return e;this.negative=0,this.words=null,this.length=0,this.red=null,null!==e&&("le"!==t&&"be"!==t||(r=t,t=10),this._init(e||0,t||10,r||"be"))}var f;"object"==typeof e?e.exports=n:t.BN=n,n.BN=n,n.wordSize=26;try{f="undefined"!=typeof window&&void 0!==window.Buffer?window.Buffer:r(654).Buffer}catch(e){}function d(e,t){var r=e.charCodeAt(t);return r>=65&&r<=70?r-55:r>=97&&r<=102?r-87:r-48&15}function o(e,t,r){var i=d(e,r);return r-1>=t&&(i|=d(e,r-1)<<4),i}function s(e,t,r,i){for(var a=0,n=Math.min(e.length,r),f=t;f<n;f++){var d=e.charCodeAt(f)-48;a*=i,a+=d>=49?d-49+10:d>=17?d-17+10:d}return a}n.isBN=function(e){return e instanceof n||null!==e&&"object"==typeof e&&e.constructor.wordSize===n.wordSize&&Array.isArray(e.words)},n.max=function(e,t){return e.cmp(t)>0?e:t},n.min=function(e,t){return e.cmp(t)<0?e:t},n.prototype._init=function(e,t,r){if("number"==typeof e)return this._initNumber(e,t,r);if("object"==typeof e)return this._initArray(e,t,r);"hex"===t&&(t=16),i(t===(0|t)&&t>=2&&t<=36);var a=0;"-"===(e=e.toString().replace(/\s+/g,""))[0]&&(a++,this.negative=1),a<e.length&&(16===t?this._parseHex(e,a,r):(this._parseBase(e,t,a),"le"===r&&this._initArray(this.toArray(),t,r)))},n.prototype._initNumber=function(e,t,r){e<0&&(this.negative=1,e=-e),e<67108864?(this.words=[67108863&e],this.length=1):e<4503599627370496?(this.words=[67108863&e,e/67108864&67108863],this.length=2):(i(e<9007199254740992),this.words=[67108863&e,e/67108864&67108863,1],this.length=3),"le"===r&&this._initArray(this.toArray(),t,r)},n.prototype._initArray=function(e,t,r){if(i("number"==typeof e.length),e.length<=0)return this.words=[0],this.length=1,this;this.length=Math.ceil(e.length/3),this.words=new Array(this.length);for(var a=0;a<this.length;a++)this.words[a]=0;var n,f,d=0;if("be"===r)for(a=e.length-1,n=0;a>=0;a-=3)f=e[a]|e[a-1]<<8|e[a-2]<<16,this.words[n]|=f<<d&67108863,this.words[n+1]=f>>>26-d&67108863,(d+=24)>=26&&(d-=26,n++);else if("le"===r)for(a=0,n=0;a<e.length;a+=3)f=e[a]|e[a+1]<<8|e[a+2]<<16,this.words[n]|=f<<d&67108863,this.words[n+1]=f>>>26-d&67108863,(d+=24)>=26&&(d-=26,n++);return this.strip()},n.prototype._parseHex=function(e,t,r){this.length=Math.ceil((e.length-t)/6),this.words=new Array(this.length);for(var i=0;i<this.length;i++)this.words[i]=0;var a,n=0,f=0;if("be"===r)for(i=e.length-1;i>=t;i-=2)a=o(e,t,i)<<n,this.words[f]|=67108863&a,n>=18?(n-=18,f+=1,this.words[f]|=a>>>26):n+=8;else for(i=(e.length-t)%2==0?t+1:t;i<e.length;i+=2)a=o(e,t,i)<<n,this.words[f]|=67108863&a,n>=18?(n-=18,f+=1,this.words[f]|=a>>>26):n+=8;this.strip()},n.prototype._parseBase=function(e,t,r){this.words=[0],this.length=1;for(var i=0,a=1;a<=67108863;a*=t)i++;i--,a=a/t|0;for(var n=e.length-r,f=n%i,d=Math.min(n,n-f)+r,o=0,c=r;c<d;c+=i)o=s(e,c,c+i,t),this.imuln(a),this.words[0]+o<67108864?this.words[0]+=o:this._iaddn(o);if(0!==f){var h=1;for(o=s(e,c,e.length,t),c=0;c<f;c++)h*=t;this.imuln(h),this.words[0]+o<67108864?this.words[0]+=o:this._iaddn(o)}this.strip()},n.prototype.copy=function(e){e.words=new Array(this.length);for(var t=0;t<this.length;t++)e.words[t]=this.words[t];e.length=this.length,e.negative=this.negative,e.red=this.red},n.prototype.clone=function(){var e=new n(null);return this.copy(e),e},n.prototype._expand=function(e){for(;this.length<e;)this.words[this.length++]=0;return this},n.prototype.strip=function(){for(;this.length>1&&0===this.words[this.length-1];)this.length--;return this._normSign()},n.prototype._normSign=function(){return 1===this.length&&0===this.words[0]&&(this.negative=0),this},n.prototype.inspect=function(){return(this.red?"<BN-R: ":"<BN: ")+this.toString(16)+">"};var c=["","0","00","000","0000","00000","000000","0000000","00000000","000000000","0000000000","00000000000","000000000000","0000000000000","00000000000000","000000000000000","0000000000000000","00000000000000000","000000000000000000","0000000000000000000","00000000000000000000","000000000000000000000","0000000000000000000000","00000000000000000000000","000000000000000000000000","0000000000000000000000000"],h=[0,0,25,16,12,11,10,9,8,8,7,7,7,7,6,6,6,6,6,6,6,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5],u=[0,0,33554432,43046721,16777216,48828125,60466176,40353607,16777216,43046721,1e7,19487171,35831808,62748517,7529536,11390625,16777216,24137569,34012224,47045881,64e6,4084101,5153632,6436343,7962624,9765625,11881376,14348907,17210368,20511149,243e5,28629151,33554432,39135393,45435424,52521875,60466176];function b(e,t,r){r.negative=t.negative^e.negative;var i=e.length+t.length|0;r.length=i,i=i-1|0;var a=0|e.words[0],n=0|t.words[0],f=a*n,d=67108863&f,o=f/67108864|0;r.words[0]=d;for(var s=1;s<i;s++){for(var c=o>>>26,h=67108863&o,u=Math.min(s,t.length-1),b=Math.max(0,s-e.length+1);b<=u;b++){var l=s-b|0;c+=(f=(a=0|e.woLine truncated
|
(()=>{var e={724:function(e,t,r){!function(e,t){"use strict";function i(e,t){if(!e)throw new Error(t||"Assertion failed")}function f(e,t){e.super_=t;var r=function(){};r.prototype=t.prototype,e.prototype=new r,e.prototype.constructor=e}function d(e,t,r){if(d.isBN(e))return e;this.negative=0,this.words=null,this.length=0,this.red=null,null!==e&&("le"!==t&&"be"!==t||(r=t,t=10),this._init(e||0,t||10,r||"be"))}var n;"object"==typeof e?e.exports=d:t.BN=d,d.BN=d,d.wordSize=26;try{n="undefined"!=typeof window&&void 0!==window.Buffer?window.Buffer:r(654).Buffer}catch(e){}function a(e,t){var r=e.charCodeAt(t);return r>=65&&r<=70?r-55:r>=97&&r<=102?r-87:r-48&15}function c(e,t,r){var i=a(e,r);return r-1>=t&&(i|=a(e,r-1)<<4),i}function s(e,t,r,i){for(var f=0,d=Math.min(e.length,r),n=t;n<d;n++){var a=e.charCodeAt(n)-48;f*=i,f+=a>=49?a-49+10:a>=17?a-17+10:a}return f}d.isBN=function(e){return e instanceof d||null!==e&&"object"==typeof e&&e.constructor.wordSize===d.wordSize&&Array.isArray(e.words)},d.max=function(e,t){return e.cmp(t)>0?e:t},d.min=function(e,t){return e.cmp(t)<0?e:t},d.prototype._init=function(e,t,r){if("number"==typeof e)return this._initNumber(e,t,r);if("object"==typeof e)return this._initArray(e,t,r);"hex"===t&&(t=16),i(t===(0|t)&&t>=2&&t<=36);var f=0;"-"===(e=e.toString().replace(/\s+/g,""))[0]&&(f++,this.negative=1),f<e.length&&(16===t?this._parseHex(e,f,r):(this._parseBase(e,t,f),"le"===r&&this._initArray(this.toArray(),t,r)))},d.prototype._initNumber=function(e,t,r){e<0&&(this.negative=1,e=-e),e<67108864?(this.words=[67108863&e],this.length=1):e<4503599627370496?(this.words=[67108863&e,e/67108864&67108863],this.length=2):(i(e<9007199254740992),this.words=[67108863&e,e/67108864&67108863,1],this.length=3),"le"===r&&this._initArray(this.toArray(),t,r)},d.prototype._initArray=function(e,t,r){if(i("number"==typeof e.length),e.length<=0)return this.words=[0],this.length=1,this;this.length=Math.ceil(e.length/3),this.words=new Array(this.length);for(var f=0;f<this.length;f++)this.words[f]=0;var d,n,a=0;if("be"===r)for(f=e.length-1,d=0;f>=0;f-=3)n=e[f]|e[f-1]<<8|e[f-2]<<16,this.words[d]|=n<<a&67108863,this.words[d+1]=n>>>26-a&67108863,(a+=24)>=26&&(a-=26,d++);else if("le"===r)for(f=0,d=0;f<e.length;f+=3)n=e[f]|e[f+1]<<8|e[f+2]<<16,this.words[d]|=n<<a&67108863,this.words[d+1]=n>>>26-a&67108863,(a+=24)>=26&&(a-=26,d++);return this.strip()},d.prototype._parseHex=function(e,t,r){this.length=Math.ceil((e.length-t)/6),this.words=new Array(this.length);for(var i=0;i<this.length;i++)this.words[i]=0;var f,d=0,n=0;if("be"===r)for(i=e.length-1;i>=t;i-=2)f=c(e,t,i)<<d,this.words[n]|=67108863&f,d>=18?(d-=18,n+=1,this.words[n]|=f>>>26):d+=8;else for(i=(e.length-t)%2==0?t+1:t;i<e.length;i+=2)f=c(e,t,i)<<d,this.words[n]|=67108863&f,d>=18?(d-=18,n+=1,this.words[n]|=f>>>26):d+=8;this.strip()},d.prototype._parseBase=function(e,t,r){this.words=[0],this.length=1;for(var i=0,f=1;f<=67108863;f*=t)i++;i--,f=f/t|0;for(var d=e.length-r,n=d%i,a=Math.min(d,d-n)+r,c=0,h=r;h<a;h+=i)c=s(e,h,h+i,t),this.imuln(f),this.words[0]+c<67108864?this.words[0]+=c:this._iaddn(c);if(0!==n){var o=1;for(c=s(e,h,e.length,t),h=0;h<n;h++)o*=t;this.imuln(o),this.words[0]+c<67108864?this.words[0]+=c:this._iaddn(c)}this.strip()},d.prototype.copy=function(e){e.words=new Array(this.length);for(var t=0;t<this.length;t++)e.words[t]=this.words[t];e.length=this.length,e.negative=this.negative,e.red=this.red},d.prototype.clone=function(){var e=new d(null);return this.copy(e),e},d.prototype._expand=function(e){for(;this.length<e;)this.words[this.length++]=0;return this},d.prototype.strip=function(){for(;this.length>1&&0===this.words[this.length-1];)this.length--;return this._normSign()},d.prototype._normSign=function(){return 1===this.length&&0===this.words[0]&&(this.negative=0),this},d.prototype.inspect=function(){return(this.red?"<BN-R: ":"<BN: ")+this.toString(16)+">"};var h=["","0","00","000","0000","00000","000000","0000000","00000000","000000000","0000000000","00000000000","000000000000","0000000000000","00000000000000","000000000000000","0000000000000000","00000000000000000","000000000000000000","0000000000000000000","00000000000000000000","000000000000000000000","0000000000000000000000","00000000000000000000000","000000000000000000000000","0000000000000000000000000"],o=[0,0,25,16,12,11,10,9,8,8,7,7,7,7,6,6,6,6,6,6,6,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5],u=[0,0,33554432,43046721,16777216,48828125,60466176,40353607,16777216,43046721,1e7,19487171,35831808,62748517,7529536,11390625,16777216,24137569,34012224,47045881,64e6,4084101,5153632,6436343,7962624,9765625,11881376,14348907,17210368,20511149,243e5,28629151,33554432,39135393,45435424,52521875,60466176];function b(e,t,r){r.negative=t.negative^e.negative;var i=e.length+t.length|0;r.length=i,i=i-1|0;var f=0|e.words[0],d=0|t.words[0],n=f*d,a=67108863&n,c=n/67108864|0;r.words[0]=a;for(var s=1;s<i;s++){for(var h=c>>>26,o=67108863&c,u=Math.min(s,t.length-1),b=Math.max(0,s-e.length+1);b<=u;b++){var l=s-b|0;h+=(n=(f=0|e.woLine truncated
|
||||||
@@ -1,11 +1,21 @@
|
|||||||
import { generateKeyPair, generateMnemonic } from "./key-derivation.service.js";
|
import {
|
||||||
|
generateKeyPair,
|
||||||
|
generateMnemonic,
|
||||||
|
newGenKeypair
|
||||||
|
} from "./key-derivation.js";
|
||||||
|
|
||||||
(async () => {
|
(async () => {
|
||||||
const mnemonic = await generateMnemonic();
|
// const mnemonic = await generateMnemonic();
|
||||||
console.log(mnemonic);
|
// console.log(mnemonic);
|
||||||
|
console.log("start");
|
||||||
|
|
||||||
|
const mnemonic =
|
||||||
|
"digital radio analyst fine casino have mass blood potato hat web capital prefer debate fee differ spray cloud";
|
||||||
|
|
||||||
const { publicKey, privateKey } = await generateKeyPair(mnemonic);
|
const { publicKey, privateKey } = await generateKeyPair(mnemonic);
|
||||||
console.log(publicKey, privateKey);
|
console.log(publicKey.keyType, privateKey.keyType);
|
||||||
|
console.log(publicKey.asHexString, privateKey.asHexString);
|
||||||
|
console.log(publicKey.asByteArray, privateKey.asByteArray);
|
||||||
|
|
||||||
console.log("done");
|
console.log("done");
|
||||||
})();
|
})();
|
||||||
@@ -0,0 +1,63 @@
|
|||||||
|
import {
|
||||||
|
generateMnemonic as generateMnemonic_bip39,
|
||||||
|
mnemonicToSeed
|
||||||
|
} from "web-bip39";
|
||||||
|
import wordlist from "web-bip39/wordlists/english";
|
||||||
|
import elliptic from "elliptic";
|
||||||
|
import { Key } from "./key";
|
||||||
|
|
||||||
|
const MNEMONIC_STRENGTH = 192;
|
||||||
|
//const CURVE = "curve25519"
|
||||||
|
const CURVE = "secp256k1";
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Generates a new key pair from a mnemonic.
|
||||||
|
*
|
||||||
|
* @param {string} mnemonic - The mnemonic to generate the key pair from.
|
||||||
|
* @returns {Promise<{ publicKey: Key; privateKey: Key }>} A promise that resolves to an object containing the public and private keys.
|
||||||
|
*/
|
||||||
|
export async function generateKeypair(mnemonic) {
|
||||||
|
const seed = (await mnemonicToSeed(mnemonic)).toString("hex");
|
||||||
|
|
||||||
|
const ec = new elliptic.ec(CURVE);
|
||||||
|
const keypair = ec.genKeyPair({
|
||||||
|
entropy: seed.slice(0, 32)
|
||||||
|
});
|
||||||
|
|
||||||
|
return {
|
||||||
|
publicKey: new Key("public", { fromHexString: keypair.getPublic("hex") }),
|
||||||
|
privateKey: new Key("private", {
|
||||||
|
fromHexString: keypair.getPrivate("hex")
|
||||||
|
})
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Generates a new mnemonic.
|
||||||
|
*
|
||||||
|
* @returns {string} The generated mnemonic.
|
||||||
|
*/
|
||||||
|
export function generateMnemonic() {
|
||||||
|
return generateMnemonic_bip39(wordlist, MNEMONIC_STRENGTH);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Gets a key pair from a private key.
|
||||||
|
*
|
||||||
|
* @param {Key} key - The private key to get the key pair from.
|
||||||
|
* @returns {{ publicKey: Key; privateKey: Key }} An object containing the public and private keys.
|
||||||
|
* @throws {TypeError} If the provided key is not a private key.
|
||||||
|
*/
|
||||||
|
export function getPairFromPrivate(key) {
|
||||||
|
if (key.keyType != "private")
|
||||||
|
throw new TypeError("Required property 'key' must be a private key");
|
||||||
|
const ec = new elliptic.ec(CURVE);
|
||||||
|
const keypair = ec.keyFromPrivate(key.asHexString);
|
||||||
|
|
||||||
|
return {
|
||||||
|
publicKey: new Key("public", { fromHexString: keypair.getPublic("hex") }),
|
||||||
|
privateKey: new Key("private", {
|
||||||
|
fromHexString: keypair.getPrivate("hex")
|
||||||
|
})
|
||||||
|
};
|
||||||
|
}
|
||||||
@@ -1,27 +0,0 @@
|
|||||||
import {
|
|
||||||
generateMnemonic as generateMnemonic_bip39,
|
|
||||||
mnemonicToSeed,
|
|
||||||
} from "web-bip39";
|
|
||||||
import wordlist from "web-bip39/wordlists/english";
|
|
||||||
import elliptic from "elliptic";
|
|
||||||
|
|
||||||
const MNEMONIC_STRENGTH = 192;
|
|
||||||
const CURVE = "curve25519"
|
|
||||||
|
|
||||||
export async function generateKeyPair(mnemonic) {
|
|
||||||
const seed = (await mnemonicToSeed(mnemonic)).toString("hex");
|
|
||||||
|
|
||||||
const ec = new elliptic.ec(CURVE);
|
|
||||||
const keyPair = ec.genKeyPair({
|
|
||||||
entropy: seed.slice(0, 32),
|
|
||||||
});
|
|
||||||
|
|
||||||
return {
|
|
||||||
publicKey: keyPair.getPublic("hex"),
|
|
||||||
privateKey: keyPair.getPrivate("hex"),
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
export function generateMnemonic() {
|
|
||||||
return generateMnemonic_bip39(wordlist, MNEMONIC_STRENGTH);
|
|
||||||
}
|
|
||||||
@@ -0,0 +1,62 @@
|
|||||||
|
/**
|
||||||
|
* Represents a cryptographic key.
|
||||||
|
*/
|
||||||
|
export class Key {
|
||||||
|
#key = new Uint8Array(1);
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Creates a new Key instance.
|
||||||
|
*
|
||||||
|
* @param {string} type - The type of key, either "public" or "private".
|
||||||
|
* @param {Object} details - Details about the key.
|
||||||
|
* @param {Uint8Array} [details.fromByteArray] - The key as a byte array.
|
||||||
|
* @param {string} [details.fromHexString] - The key as a hexadecimal string.
|
||||||
|
* @throws {TypeError} If the type is not "public" or "private", or if details are provided but do not contain a valid key representation.
|
||||||
|
*/
|
||||||
|
constructor(type, details) {
|
||||||
|
if (type.toLowerCase() !== "public" && type.toLowerCase() !== "private")
|
||||||
|
throw new TypeError(
|
||||||
|
"Required property 'type' may only take 'public' or 'private' as values"
|
||||||
|
);
|
||||||
|
else this.keyType = type;
|
||||||
|
|
||||||
|
if (!details || (!details.fromByteArray && !details.fromHexString))
|
||||||
|
throw new TypeError(
|
||||||
|
"Missing required property 'fromByteArray' or 'fromHexString' in parameter 'details'"
|
||||||
|
);
|
||||||
|
|
||||||
|
if (details.fromByteArray && details.fromHexString)
|
||||||
|
console.warn(
|
||||||
|
"Both 'fromByteArray' and 'fromHexString' present. Value of 'fromHexString' will be used."
|
||||||
|
);
|
||||||
|
|
||||||
|
if (details.fromByteArray) {
|
||||||
|
this.#key = details.fromByteArray;
|
||||||
|
} else if (details.fromHexString) {
|
||||||
|
this.#key = Uint8Array.from(
|
||||||
|
details.fromHexString.match(/.{1,2}/g).map((byte) => parseInt(byte, 16))
|
||||||
|
);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Gets the key as a hexadecimal string.
|
||||||
|
*
|
||||||
|
* @returns {string} The key as a hexadecimal string.
|
||||||
|
*/
|
||||||
|
get asHexString() {
|
||||||
|
return this.#key.reduce(
|
||||||
|
(str, byte) => str + byte.toString(16).padStart(2, "0"),
|
||||||
|
""
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Gets the key as a byte array.
|
||||||
|
*
|
||||||
|
* @returns {Uint8Array} The key as a byte array.
|
||||||
|
*/
|
||||||
|
get asByteArray() {
|
||||||
|
return this.#key;
|
||||||
|
}
|
||||||
|
}
|
||||||
Reference in new issue
Block a user