]> zoso.dev Git - libnemo.git/commitdiff
Got account to work with basically vanilla TweetNaCl, so save work and start pruning...
authorChris Duncan <chris@zoso.dev>
Sun, 24 Nov 2024 19:40:52 +0000 (11:40 -0800)
committerChris Duncan <chris@zoso.dev>
Sun, 24 Nov 2024 19:40:52 +0000 (11:40 -0800)
src/lib/account.ts
src/lib/workers/nano25519.ts

index 3e9be666b4c6dfb4debe98e7e258d4d1dab330b1..b2b1910eb0cd340d75339d4fef419d49c95cf76d 100644 (file)
@@ -4,7 +4,7 @@
 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
@@ -87,7 +87,8 @@ export class Account {
        */\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
index de2536c093d28692921bebb67b8cf4b88ced5852..f97313e0daaf68dee592b928ffe791bbb5595bab 100644 (file)
@@ -17,6 +17,29 @@ import blake2b from 'blake2b'
        // 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
@@ -2177,7 +2200,37 @@ import blake2b from 'blake2b'
        \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
@@ -2233,7 +2286,7 @@ import blake2b from 'blake2b'
                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
@@ -2305,4 +2358,4 @@ import blake2b from 'blake2b'
                }\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