-
Notifications
You must be signed in to change notification settings - Fork 14
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(apollo): Implementing PeerDID ED25519 and X25519 algorithms for …
…KeyAgreement and Authentication. (#13)
- Loading branch information
1 parent
0b6ec28
commit f6eabef
Showing
39 changed files
with
1,653 additions
and
195 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
import * as elliptic from "elliptic"; | ||
|
||
const eddsa = new elliptic.eddsa("ed25519"); | ||
export abstract class Ed25519KeyCommon { | ||
public static eddsa = eddsa; | ||
public eddsa = eddsa; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
import elliptic from "elliptic"; | ||
import { base64url } from "multiformats/bases/base64"; | ||
|
||
import { Ed25519KeyCommon } from "./Ed25519KeyCommon"; | ||
import { Ed25519PrivateKey } from "./Ed25519PrivateKey"; | ||
import { Ed25519PublicKey } from "./Ed25519PublicKey"; | ||
|
||
export class Ed25519KeyPair extends Ed25519KeyCommon { | ||
private privateKey: Ed25519PrivateKey; | ||
private publicKey: Ed25519PublicKey; | ||
|
||
constructor() { | ||
super(); | ||
|
||
const secret = Buffer.from(elliptic.rand(32)); | ||
const keyPair = this.eddsa.keyFromSecret(secret); | ||
|
||
const pub = Buffer.from(keyPair.getPublic()); | ||
|
||
this.privateKey = new Ed25519PrivateKey( | ||
Buffer.from(base64url.baseEncode(secret)) | ||
); | ||
|
||
this.publicKey = new Ed25519PublicKey( | ||
Buffer.from(base64url.baseEncode(pub)) | ||
); | ||
} | ||
|
||
public getPrivate(): Buffer { | ||
return this.privateKey.getEncoded(); | ||
} | ||
|
||
public getPublic(): Buffer { | ||
return this.publicKey.getEncoded(); | ||
} | ||
|
||
public sign(message: Buffer) { | ||
return this.privateKey.sign(message); | ||
} | ||
|
||
public verify(message: Buffer, sig: Buffer) { | ||
return this.publicKey.verify(message, sig); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
import * as elliptic from "elliptic"; | ||
import { base64url } from "multiformats/bases/base64"; | ||
|
||
import { Ed25519KeyCommon } from "./Ed25519KeyCommon"; | ||
|
||
export class Ed25519PrivateKey extends Ed25519KeyCommon { | ||
private keyPair: elliptic.eddsa.KeyPair; | ||
|
||
constructor(nativeValue: Buffer) { | ||
super(); | ||
this.keyPair = this.eddsa.keyFromSecret( | ||
Buffer.from(base64url.baseDecode(nativeValue.toString())) | ||
); | ||
} | ||
|
||
getEncoded(): Buffer { | ||
return Buffer.from(base64url.baseEncode(this.keyPair.getSecret())); | ||
} | ||
|
||
sign(message: Buffer) { | ||
const sig = this.keyPair.sign(message); | ||
return sig.toBytes(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
import * as elliptic from "elliptic"; | ||
import { base64url } from "multiformats/bases/base64"; | ||
|
||
import { Ed25519KeyCommon } from "./Ed25519KeyCommon"; | ||
|
||
export class Ed25519PublicKey extends Ed25519KeyCommon { | ||
private keyPair: elliptic.eddsa.KeyPair; | ||
|
||
constructor(nativeValue: Uint8Array) { | ||
super(); | ||
|
||
this.keyPair = this.eddsa.keyFromPublic( | ||
Array.from( | ||
base64url.baseDecode(nativeValue.toString()) | ||
) as unknown as Buffer | ||
); | ||
} | ||
|
||
getEncoded(): Buffer { | ||
return Buffer.from(base64url.baseEncode(this.keyPair.getPublic())); | ||
} | ||
|
||
verify(message: Buffer, sig: Buffer) { | ||
//TODO: Report a bug in elliptic, this method is not expecting a Buffer (bytes) | ||
//Internally it expects to find an array, if not Buffer.slice.concat fails when Array.slice.concat doesn't | ||
//Must keep this... | ||
return this.keyPair.verify(message, Array.from(sig) as unknown as Buffer); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
import * as elliptic from "elliptic"; | ||
|
||
const ec = new elliptic.ec("secp256k1"); | ||
export abstract class Secp256k1KeyCommon { | ||
public static ec = ec; | ||
public ec = ec; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
import BN from "bn.js"; | ||
import * as elliptic from "elliptic"; | ||
import { Secp256k1KeyCommon } from "./Secp256k1KeyCommon"; | ||
|
||
import { Secp256k1PrivateKey } from "./Secp256k1PrivateKey"; | ||
import { Secp256k1PublicKey } from "./Secp256k1PublicKey"; | ||
|
||
export class Secp256k1KeyPair extends Secp256k1KeyCommon { | ||
constructor( | ||
public privateKey: Secp256k1PrivateKey, | ||
public publicKey: Secp256k1PublicKey | ||
) { | ||
super(); | ||
} | ||
|
||
static generateSecp256k1KeyPair(): Secp256k1KeyPair { | ||
const keyPair = this.ec.genKeyPair(); | ||
const bigNumber = keyPair.getPrivate(); | ||
const basePoint = keyPair.getPublic(); | ||
return Secp256k1KeyPair.fromNativeValues(bigNumber, basePoint); | ||
} | ||
|
||
static fromNativeValues( | ||
privateNative: BN, | ||
publicNative: elliptic.curve.base.BasePoint | ||
): Secp256k1KeyPair { | ||
return new Secp256k1KeyPair( | ||
new Secp256k1PrivateKey(privateNative), | ||
new Secp256k1PublicKey(publicNative) | ||
); | ||
} | ||
} |
Oops, something went wrong.