Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

generateKeyPair: return different type depending on raw value #1545

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 6 additions & 7 deletions src/utils/crypto.ts
Original file line number Diff line number Diff line change
Expand Up @@ -122,17 +122,16 @@ export function generateKeyPairFromSecret (secret: Uint8Array): SignKeyPair {

/**
* Generate a random ED25519 keypair
* @rtype (raw: Boolean) => {publicKey: String | Buffer, secretKey: String | Buffer}
* @param {Boolean} raw - Whether to return raw (binary) keys
* @return {Object} Key pair
* @param raw Whether to return raw (binary) keys
* @return Key pair
*/
export function generateKeyPair (raw = false): {
publicKey: string | Buffer
export function generateKeyPair (raw?: true): { publicKey: Buffer, secretKey: Buffer }
export function generateKeyPair (raw: false): { publicKey: EncodedData<'ak'>, secretKey: string }
export function generateKeyPair (raw: boolean = false): {
publicKey: EncodedData<'ak'> | Buffer
secretKey: string | Buffer
} {
// <node>/apps/aens/test/aens_test_utils.erl
const keyPair = nacl.sign.keyPair()

const publicBuffer = Buffer.from(keyPair.publicKey)
const secretBuffer = Buffer.from(keyPair.secretKey)

Expand Down
4 changes: 3 additions & 1 deletion src/utils/encoder.ts
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,9 @@ export function decode (data: EncodedData<EncodingType>): Buffer {
* @param {string} type Prefix of Transaction
* @return {EncodedData<type>>} Encoded string Base58check or Base64check data
*/
export function encode (data: Uint8Array, type: EncodingType): EncodedData<typeof type> {
export function encode<Type extends EncodingType> (
data: Uint8Array, type: Type
): EncodedData<Type> {
const [, { encode }] = parseType(type)
ensureValidLength(data, type)
return `${type}_${encode(data)}`
Expand Down