Release 1.0.0 (#94)

* Add some types
* Add prepublish script
* Ignore linting certain files
  * In the future we should add types and re-enable linting
* Use export = syntax
* Allow consumers to use `import keypairs from 'ripple-keypairs'`
  * See: https://www.typescriptlang.org/docs/handbook/modules.html#export--and-import--require
This commit is contained in:
Elliot Lee
2020-02-05 16:34:53 -08:00
parent 4d0c280996
commit 01e94ad4d6
8 changed files with 89 additions and 70 deletions

View File

@@ -16,16 +16,16 @@ const { bytesToHex } = utils
function generateSeed(
options: {
entropy?: Uint8Array
algorithm?: 'ed25519' | 'secp256k1'
algorithm?: 'ed25519' | 'ecdsa-secp256k1'
} = {},
) {
): string {
assert(!options.entropy || options.entropy.length >= 16, 'entropy too short')
const entropy = options.entropy ? options.entropy.slice(0, 16) : brorand(16)
const type = options.algorithm === 'ed25519' ? 'ed25519' : 'secp256k1'
return addressCodec.encodeSeed(entropy, type)
}
function hash(message) {
function hash(message): number[] {
return hashjs
.sha512()
.update(message)
@@ -34,7 +34,13 @@ function hash(message) {
}
const secp256k1 = {
deriveKeypair(entropy, options) {
deriveKeypair(
entropy: Uint8Array,
options?: object,
): {
privateKey: string
publicKey: string
} {
const prefix = '00'
const privateKey =
@@ -51,7 +57,7 @@ const secp256k1 = {
return { privateKey, publicKey }
},
sign(message, privateKey) {
sign(message, privateKey): string {
return bytesToHex(
Secp256k1.sign(hash(message), hexToBytes(privateKey), {
canonical: true,
@@ -59,13 +65,18 @@ const secp256k1 = {
)
},
verify(message, signature, publicKey) {
verify(message, signature, publicKey): boolean {
return Secp256k1.verify(hash(message), signature, hexToBytes(publicKey))
},
}
const ed25519 = {
deriveKeypair(entropy) {
deriveKeypair(
entropy: Uint8Array,
): {
privateKey: string
publicKey: string
} {
const prefix = 'ED'
const rawPrivateKey = hash(entropy)
const privateKey = prefix + bytesToHex(rawPrivateKey)
@@ -74,7 +85,7 @@ const ed25519 = {
return { privateKey, publicKey }
},
sign(message, privateKey) {
sign(message, privateKey): string {
// caution: Ed25519.sign interprets all strings as hex, stripping
// any non-hex characters without warning
assert(Array.isArray(message), 'message must be array of octets')
@@ -83,7 +94,7 @@ const ed25519 = {
)
},
verify(message, signature, publicKey) {
verify(message, signature, publicKey): boolean {
return Ed25519.verify(
message,
hexToBytes(signature),
@@ -92,12 +103,19 @@ const ed25519 = {
},
}
function select(algorithm) {
// eslint-disable-next-line @typescript-eslint/no-explicit-any
function select(algorithm): any {
const methods = { 'ecdsa-secp256k1': secp256k1, ed25519 }
return methods[algorithm]
}
function deriveKeypair(seed, options) {
function deriveKeypair(
seed: string,
options?: object,
): {
publicKey: string
privateKey: string
} {
const decoded = addressCodec.decodeSeed(seed)
const algorithm = decoded.type === 'ed25519' ? 'ed25519' : 'ecdsa-secp256k1'
const method = select(algorithm)
@@ -111,34 +129,34 @@ function deriveKeypair(seed, options) {
return keypair
}
function getAlgorithmFromKey(key) {
function getAlgorithmFromKey(key): 'ed25519' | 'ecdsa-secp256k1' {
const bytes = hexToBytes(key)
return bytes.length === 33 && bytes[0] === 0xed
? 'ed25519'
: 'ecdsa-secp256k1'
}
function sign(messageHex, privateKey) {
function sign(messageHex, privateKey): string {
const algorithm = getAlgorithmFromKey(privateKey)
return select(algorithm).sign(hexToBytes(messageHex), privateKey)
}
function verify(messageHex, signature, publicKey) {
function verify(messageHex, signature, publicKey): boolean {
const algorithm = getAlgorithmFromKey(publicKey)
return select(algorithm).verify(hexToBytes(messageHex), signature, publicKey)
}
function deriveAddressFromBytes(publicKeyBytes: Buffer) {
function deriveAddressFromBytes(publicKeyBytes: Buffer): string {
return addressCodec.encodeAccountID(
utils.computePublicKeyHash(publicKeyBytes),
)
}
function deriveAddress(publicKey) {
return deriveAddressFromBytes(hexToBytes(publicKey))
function deriveAddress(publicKey): string {
return deriveAddressFromBytes(Buffer.from(hexToBytes(publicKey)))
}
function deriveNodeAddress(publicKey) {
function deriveNodeAddress(publicKey): string {
const generatorBytes = addressCodec.decodeNodePublic(publicKey)
const accountPublicBytes = accountPublicFromPublicGenerator(generatorBytes)
return deriveAddressFromBytes(accountPublicBytes)
@@ -146,7 +164,7 @@ function deriveNodeAddress(publicKey) {
const { decodeSeed } = addressCodec
module.exports = {
export = {
generateSeed,
deriveKeypair,
sign,

View File

@@ -1,3 +1,5 @@
/* eslint-disable */
import * as elliptic from 'elliptic'
import Sha512 from './sha512'

View File

@@ -1,3 +1,5 @@
/* eslint-disable */
import * as hashjs from 'hash.js'
import * as BigNum from 'bn.js'

View File

@@ -2,7 +2,7 @@ import * as assert from 'assert'
import * as hashjs from 'hash.js'
import * as BN from 'bn.js'
function bytesToHex(a) {
function bytesToHex(a): string {
return a
.map((byteValue) => {
const hex = byteValue.toString(16).toUpperCase()
@@ -11,7 +11,7 @@ function bytesToHex(a) {
.join('')
}
function hexToBytes(a) {
function hexToBytes(a): number[] {
assert(a.length % 2 === 0)
return new BN(a, 16).toArray(null, a.length / 2)
}