import blake2b from 'blake2b'\r
import { ACCOUNT_KEY_LENGTH, ALPHABET, PREFIX, PREFIX_LEGACY } from './constants.js'\r
import { base32, bytes, hex } from './convert.js'\r
-import { keyPair, secretKeyLength } from './workers/nano25519.js'\r
+import { getPublicKey, keyPair } from './workers/nano25519.js'\r
import { Rpc } from './rpc.js'\r
import { Safe } from './safe.js'\r
\r
*/\r
static async fromPrivateKey (key: string, index?: number): Promise<Account> {\r
Account.#validateKey(key)\r
- const { publicKey } = keyPair.fromSecretKey(hex.toBytes(key))\r
+ // const publicKey = getPublicKey(hex.toBytes(key))\r
+ const { publicKey } = keyPair.fromSeed(hex.toBytes(key))\r
const account = await Account.fromPublicKey(bytes.toHex(publicKey), index)\r
account.#prv = key.toUpperCase()\r
return account\r
// See for details: https://docs.nano.org/integration-guides/the-basics/\r
// Original source commit: https://github.com/dchest/tweetnacl-js/blob/71df1d6a1d78236ca3e9f6c788786e21f5a651a6/nacl-fast.js\r
\r
+/**\r
+* Generate a public key from a private key using the Ed25519 algorithm. The key\r
+* should be a cryptographically strong random value.\r
+*\r
+* @param {string} privateKey - 32-byte private key\r
+* @returns {string} 32-byte public key\r
+*/\r
+function getPublicKey (privateKey: Uint8Array): Uint8Array {\r
+ const h = blake2b(64).update(privateKey).digest().slice(0, 32)\r
+ return scalarMult.base(h)\r
+ // const pk = new Uint8Array(32)\r
+ // const p = [gf(),gf(),gf(),gf()]\r
+ // const h = blake2b(64).update(privateKey).digest().slice(0, 32)\r
+\r
+ // h[0] &= 0xf8\r
+ // h[31] &= 0x7f\r
+ // h[31] |= 0x40\r
+\r
+ // scalarbase(p, h)\r
+ // pack(pk, p)\r
+\r
+ // return pk\r
+}\r
var gf = function(init?) {\r
var i, r = new Float64Array(16);\r
if (init) for (i = 0; i < init.length; i++) r[i] = init[i];\r
\r
scalarMult.scalarLength = crypto_scalarmult_SCALARBYTES;\r
scalarMult.groupElementLength = crypto_scalarmult_BYTES;\r
- \r
+\r
+ const box = {\r
+ before: function(publicKey, secretKey) {\r
+ checkArrayTypes(publicKey, secretKey);\r
+ checkBoxLengths(publicKey, secretKey);\r
+ var k = new Uint8Array(crypto_box_BEFORENMBYTES);\r
+ crypto_box_beforenm(k, publicKey, secretKey);\r
+ return k;\r
+ },\r
+ keyPair: {\r
+ create: () => {\r
+ var pk = new Uint8Array(crypto_box_PUBLICKEYBYTES);\r
+ var sk = new Uint8Array(crypto_box_SECRETKEYBYTES);\r
+ crypto_box_keypair(pk, sk);\r
+ return {publicKey: pk, secretKey: sk};\r
+ },\r
+ fromSecretKey: (secretKey) => {\r
+ checkArrayTypes(secretKey);\r
+ if (secretKey.length !== crypto_box_SECRETKEYBYTES)\r
+ throw new Error('bad secret key size');\r
+ var pk = new Uint8Array(crypto_box_PUBLICKEYBYTES);\r
+ crypto_scalarmult_base(pk, secretKey);\r
+ return {publicKey: pk, secretKey: new Uint8Array(secretKey)};\r
+ }\r
+ },\r
+ publicKeyLength: crypto_box_PUBLICKEYBYTES,\r
+ secretKeyLength: crypto_box_SECRETKEYBYTES,\r
+ sharedKeyLength: crypto_box_BEFORENMBYTES,\r
+ nonceLength: crypto_box_NONCEBYTES\r
+ }\r
+\r
const message = {\r
sign: (msg, secretKey) => {\r
checkArrayTypes(msg, secretKey);\r
fromSecretKey: (secretKey) => {\r
checkArrayTypes(secretKey);\r
if (secretKey.length !== crypto_sign_SECRETKEYBYTES)\r
- throw new Error('bad secret key size');\r
+ throw new Error(`bad secret key size ${secretKey.length}`);\r
var pk = new Uint8Array(crypto_sign_PUBLICKEYBYTES);\r
for (var i = 0; i < pk.length; i++) pk[i] = secretKey[32+i];\r
return {publicKey: pk, secretKey: new Uint8Array(secretKey)};\r
}\r
})();\r
\r
- export { keyPair, hash, message, randomBytes, scalarMult }
\ No newline at end of file
+ export { box, keyPair, getPublicKey, hash, message, randomBytes, scalarMult }
\ No newline at end of file