Skip to content

Commit

Permalink
test(key-manager): add extra tests using testvectors for `keyManagerS…
Browse files Browse the repository at this point in the history
…ign()`
  • Loading branch information
mirceanis committed Jun 1, 2021
1 parent b5931ce commit a95c124
Show file tree
Hide file tree
Showing 2 changed files with 95 additions and 1 deletion.
49 changes: 48 additions & 1 deletion __tests__/shared/keyManager.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
import { TAgent, IDIDManager, IKeyManager, IAgentOptions, TKeyType } from '../../packages/core/src'
import { TKeyType } from '@veramo/core'
import { TAgent, IDIDManager, IKeyManager, IAgentOptions } from '../../packages/core/src'
import { ICredentialIssuer } from '@veramo/credential-w3c/src'
import { serialize } from '@ethersproject/transactions'

type ConfiguredAgent = TAgent<IDIDManager & IKeyManager>
Expand Down Expand Up @@ -260,6 +262,39 @@ export default (testContext: {
'0xf869018504a817c800840291882094ce31a19193d4b23f4e9d6163d7247243baf801c3830493e0801ba0f16e2206290181c3feaa04051dad19089105c24339dbdf0d80147b48a59fa152a0770e8751ec77ccc78e8b207023f168444f7cfb67055c55c70ef75234458a3d51',
)
})

it('should sign JWT using generic signer', async () => {
const signature = await agent.keyManagerSign({
kid: importedKey.kid,
data: 'bla.bla',
alg: 'ES256K',
enc: 'utf-8',
})
expect(signature).toEqual(
'pNAFkgmuKhqMbb_6Km--ZmY7UCkWunWUuNajSfF6rv5lEa5nNXCU7cnZBZVptU7u8h150qetqkqUaahAf-Cepw',
)
})

it('should sign EthTX using generic signer', async () => {
const txData = serialize({
to: '0xce31a19193d4b23f4e9d6163d7247243bAF801c3',
value: 300000,
gasLimit: 43092000,
gasPrice: 20000000000,
nonce: 1,
})

const rawTx = await agent.keyManagerSign({
alg: 'eth_signTransaction',
data: txData,
enc: 'hex',
kid: importedKey.kid,
})

expect(rawTx).toEqual(
'0xf869018504a817c800840291882094ce31a19193d4b23f4e9d6163d7247243baf801c3830493e0801ba0f16e2206290181c3feaa04051dad19089105c24339dbdf0d80147b48a59fa152a0770e8751ec77ccc78e8b207023f168444f7cfb67055c55c70ef75234458a3d51',
)
})
})

describe('using Ed25519 testvectors', () => {
Expand All @@ -285,6 +320,18 @@ export default (testContext: {
'_2P0iukN2CPH1nQ6LeBm1zQHHp3U4wSYDrpeWTWkp7yuzJex6O60Z4OhdfD5I9WPHV734US8n5vyD2VDbT1UCg',
)
})

it('should sign JWT using generic signer', async () => {
const signature = await agent.keyManagerSign({
kid: importedKey.kid,
data: 'bla.bla',
alg: 'EdDSA',
enc: 'utf-8',
})
expect(signature).toEqual(
'_2P0iukN2CPH1nQ6LeBm1zQHHp3U4wSYDrpeWTWkp7yuzJex6O60Z4OhdfD5I9WPHV734US8n5vyD2VDbT1UCg',
)
})
})
})
}
47 changes: 47 additions & 0 deletions __tests__/shared/verifiableData.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import { TAgent, IDIDManager, IIdentifier, IDataStore } from '../../packages/cor
import { IDataStoreORM } from '../../packages/data-store/src'
import { ICredentialIssuer } from '../../packages/credential-w3c/src'
import { decodeJWT } from 'did-jwt'
import { TKeyType } from '@veramo/core'

type ConfiguredAgent = TAgent<IDIDManager & ICredentialIssuer & IDataStore & IDataStoreORM>

Expand Down Expand Up @@ -239,5 +240,51 @@ export default (testContext: {
}),
).rejects.toThrow('Verifiable presentation not found')
})

describe('using testvectors', () => {
const importedDID = {
did: 'did:ethr:rinkeby:0x03155ee0cbefeecd80de63a62b4ed8f0f97ac22a58f76a265903b9acab79bf018c',
provider: 'did:ethr:rinkeby',
controllerKeyId:
'04155ee0cbefeecd80de63a62b4ed8f0f97ac22a58f76a265903b9acab79bf018c7037e2bd897812170c92a4c978d6a10481491a37299d74c4bd412a111a4ac875',
keys: [
{
kid:
'04155ee0cbefeecd80de63a62b4ed8f0f97ac22a58f76a265903b9acab79bf018c7037e2bd897812170c92a4c978d6a10481491a37299d74c4bd412a111a4ac875',
kms: 'local',
type: <TKeyType>'Secp256k1',
publicKeyHex:
'04155ee0cbefeecd80de63a62b4ed8f0f97ac22a58f76a265903b9acab79bf018c7037e2bd897812170c92a4c978d6a10481491a37299d74c4bd412a111a4ac875',
privateKeyHex: '31d1ec15ff8110442012fef0d1af918c0e09b2e2ab821bba52ecc85f8655ec63',
},
],
services: [],
}

beforeAll(async () => {
const imported = await agent.didManagerImport(importedDID)
})

it('signs JWT with ES256K', async () => {
const credentialInput = {
credentialSubject: { id: 'did:example:subject', name: 'Alice' },
issuer: { id: importedDID.did },
}
const { proof, issuanceDate, ...comparableOutput } = await agent.createVerifiableCredential({
credential: credentialInput,
proofFormat: 'jwt',
save: false,
removeOriginalFields: true,
})
expect(comparableOutput).toEqual({
credentialSubject: { name: 'Alice', id: 'did:example:subject' },
issuer: {
id: 'did:ethr:rinkeby:0x03155ee0cbefeecd80de63a62b4ed8f0f97ac22a58f76a265903b9acab79bf018c',
},
type: ['VerifiableCredential'],
'@context': ['https://www.w3.org/2018/credentials/v1'],
})
})
})
})
}

0 comments on commit a95c124

Please sign in to comment.