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

feat: add common signature encoding #107

Merged
merged 5 commits into from
Jul 5, 2024
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
44 changes: 44 additions & 0 deletions features/keychain/module/__tests__/ecdsa.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -93,3 +93,47 @@ describe.each([
expect(signature.toString('hex')).toBe(expected)
})
})

describe('EcDSA Signer Signature Encoding', () => {
const keychain = createKeychain({ seed })
const data = Buffer.from('I really love keychains')
const expected = {
default:
'30460221009288b22525674d76b0d5b8b20f333d4de4f4f88340a7d0a4cadd54b719e6162d022100f63e7591ce6b3bc0bf66fa2948d220e74ea2a74b63fc9dcb20e0f53191550b67',
binary:
'9288b22525674d76b0d5b8b20f333d4de4f4f88340a7d0a4cadd54b719e6162df63e7591ce6b3bc0bf66fa2948d220e74ea2a74b63fc9dcb20e0f53191550b6700',
}

it('Default encoding', async () => {
const signature = await keychain.secp256k1.signBuffer({ seedId, keyId, data })
expect(signature instanceof Buffer).toBe(true)
expect(signature.toString('hex')).toBe(expected.default)
})

it('DER encoding', async () => {
const signature = await keychain.secp256k1.signBuffer({ seedId, keyId, data, enc: 'der' })
expect(signature instanceof Buffer).toBe(true)
expect(signature.toString('hex')).toBe(expected.default)
})

it('Binary encoding', async () => {
const signature = await keychain.secp256k1.signBuffer({ seedId, keyId, data, enc: 'binary' })
expect(signature instanceof Buffer).toBe(true)
expect(signature.toString('hex')).toBe(expected.binary)
})

it('Raw encoding', async () => {
const signature = await keychain.secp256k1.signBuffer({ seedId, keyId, data, enc: 'raw' })
expect(typeof signature === 'object')
expect(Object.getOwnPropertyNames(signature)).toStrictEqual(['r', 's', 'recoveryParam'])
const r = Buffer.from(signature.r.toArray('be', 32))
const s = Buffer.from(signature.s.toArray('be', 32))
const binary = Buffer.concat([r, s, Buffer.from([signature.recoveryParam])])
expect(binary.toString('hex')).toBe(expected.binary)
})

it('Invalid encoding', async () => {
const sign = () => keychain.secp256k1.signBuffer({ seedId, keyId, data, enc: 'other' })
await expect(sign()).rejects.toThrow(/signBuffer: invalid enc/)
})
})
14 changes: 12 additions & 2 deletions features/keychain/module/crypto/secp256k1.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,16 @@ import { tweakPrivateKey } from './tweak'
const isValidEcOptions = (ecOptions) =>
!ecOptions || Object.keys(ecOptions).every((key) => ['canonical'].includes(key))

const encodeSignature = ({ signature, enc }) => {
if (enc === 'der') return Buffer.from(signature.toDER())

if (enc === 'raw') return { ...signature }

const r = Buffer.from(signature.r.toArray('be', 32))
const s = Buffer.from(signature.s.toArray('be', 32))
return Buffer.concat([r, s, Buffer.from([signature.recoveryParam])])
}

export const create = ({ getPrivateHDKey }) => {
const EC = elliptic.ec
const curve = new EC('secp256k1')
Expand All @@ -18,11 +28,11 @@ export const create = ({ getPrivateHDKey }) => {
keyId.keyType === 'secp256k1',
`ECDSA signatures are not supported for ${keyId.keyType}`
)
assert(['der', 'raw'].includes(enc), 'signBuffer: invalid enc')
assert(['der', 'raw', 'binary'].includes(enc), 'signBuffer: invalid enc')
assert(isValidEcOptions(ecOptions), 'signBuffer: invalid EC option')
const { privateKey } = getPrivateHDKey({ seedId, keyId })
const signature = curve.sign(data, privateKey, pick(ecOptions, ['canonical']))
return enc === 'der' ? Buffer.from(signature.toDER()) : { ...signature }
return encodeSignature({ signature, enc })
},
signSchnorr: async ({ seedId, keyId, data, tweak, extraEntropy }) => {
assert(
Expand Down
Loading