Skip to content

Commit 690d400

Browse files
author
SeungMin Lee
committed
Replace ECDSA and Schnorr with Ed25519
1 parent 1d30b6d commit 690d400

File tree

9 files changed

+83
-418
lines changed

9 files changed

+83
-418
lines changed

README.md

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -28,12 +28,13 @@ import { blake256, H256 } from "foundry-primitives";
2828
- blake256
2929
- blake256WithKey
3030
- ripemd160
31-
- signEcdsa
32-
- verifyEcdsa
33-
- recoverEcdsa
31+
- signEd25519
32+
- verifyEd25519
33+
- recoverEd25519
3434
- generatePrivateKey
3535
- getPublicFromPrivate
3636
- toHex
37+
- toArray
3738
- getAccountIdFromPrivate
3839
- getAccountIdFromPublic
3940

package.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,7 @@
5959
"hmac-drbg": "^1.0.1",
6060
"lodash": "^4.17.14",
6161
"node-forge": "^0.7.6",
62-
"rlp": "^2.1.0"
62+
"rlp": "^2.1.0",
63+
"tweetnacl": "^1.0.3"
6364
}
6465
}

src/index.ts

Lines changed: 1 addition & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -25,19 +25,7 @@ export {
2525

2626
export { generatePrivateKey, getPublicFromPrivate } from "./key/key";
2727

28-
export {
29-
EcdsaSignature,
30-
signEcdsa,
31-
verifyEcdsa,
32-
recoverEcdsa
33-
} from "./key/ecdsa";
34-
35-
export {
36-
SchnorrSignature,
37-
signSchnorr,
38-
verifySchnorr,
39-
recoverSchnorr
40-
} from "./key/schnorr";
28+
export { Ed25519Signature, signEd25519, verifyEd25519 } from "./key/ed25519";
4129

4230
export {
4331
toHex,

src/key/ecdsa.ts

Lines changed: 0 additions & 105 deletions
This file was deleted.

src/key/ed25519.ts

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
import * as _ from "lodash";
2+
import nacl = require("tweetnacl");
3+
import { toArray, toHex } from "../utility";
4+
5+
export type Ed25519Signature = string;
6+
7+
/**
8+
* Gets EdDSA(Ed25519) signature for message from private key.
9+
* @param message 32 byte hexadecimal string
10+
* @param priv 64 byte hexadecimal string of private key
11+
* @returns 64 byte hexadecimal string of Ed25519 signature
12+
*/
13+
export const signEd25519 = (
14+
message: string,
15+
priv: string
16+
): Ed25519Signature => {
17+
return toHex(
18+
Buffer.from(nacl.sign.detached(toArray(message), toArray(priv)))
19+
);
20+
};
21+
22+
/**
23+
* Checks if the signature from signEd25519 is valid.
24+
* @param message 32 byte hexadecimal string
25+
* @param signature 64 byte hexadecimal string of Ed25519 signature
26+
* @param pub 32 byte hexadecimal string of public key
27+
* @returns if signature is valid, true. Else false.
28+
*/
29+
export const verifyEd25519 = (
30+
message: string,
31+
signature: Ed25519Signature,
32+
pub: string
33+
): boolean => {
34+
return nacl.sign.detached.verify(
35+
toArray(message),
36+
toArray(signature),
37+
toArray(pub)
38+
);
39+
};

src/key/key.ts

Lines changed: 9 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,32 +1,22 @@
11
import * as _ from "lodash";
2-
3-
/**
4-
* @hidden
5-
*/
6-
const EC = require("elliptic").ec;
7-
/**
8-
* @hidden
9-
*/
10-
const secp256k1 = new EC("secp256k1");
2+
import nacl = require("tweetnacl");
3+
import { toArray, toHex } from "../utility";
114

125
/**
136
* Generates a private key.
14-
* @returns 32 byte hexadecimal string of private key
7+
* @returns 64 byte hexadecimal string of private key
158
*/
169
export const generatePrivateKey = (): string => {
17-
return _.padStart(secp256k1.genKeyPair().priv.toString("hex"), 64, "0");
10+
return toHex(Buffer.from(nacl.sign.keyPair().secretKey));
1811
};
1912

2013
/**
2114
* Gets public key from private key.
22-
* @param priv 32 byte hexadecimal string of private key
23-
* @returns 64 byte hexadecimal string of public key
15+
* @param priv 64 byte hexadecimal string of private key
16+
* @returns 32 byte hexadecimal string of public key
2417
*/
2518
export const getPublicFromPrivate = (priv: string): string => {
26-
const key = secp256k1.keyFromPrivate(priv);
27-
// Remove prefix "04" which represents it's uncompressed form.
28-
return key
29-
.getPublic()
30-
.encode("hex")
31-
.slice(2);
19+
return toHex(
20+
Buffer.from(nacl.sign.keyPair.fromSecretKey(toArray(priv)).publicKey)
21+
);
3222
};

0 commit comments

Comments
 (0)