Skip to content

Commit

Permalink
Merge branch 'main' into key-rotate
Browse files Browse the repository at this point in the history
  • Loading branch information
TimoGlastra authored Mar 28, 2022
2 parents 88ebf81 + 0dd9a5a commit 34b1a70
Show file tree
Hide file tree
Showing 33 changed files with 290 additions and 235 deletions.
5 changes: 2 additions & 3 deletions packages/core/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -39,15 +39,13 @@
"lru_map": "^0.4.1",
"luxon": "^1.27.0",
"make-error": "^1.3.6",
"multibase": "^4.0.4",
"multiformats": "^9.4.14",
"multihashes": "^4.0.2",
"object-inspect": "^1.10.3",
"query-string": "^7.0.1",
"reflect-metadata": "^0.1.13",
"rxjs": "^7.1.0",
"tsyringe": "^4.5.0",
"uuid": "^8.3.2",
"varint": "^6.0.0",
"web-did-resolver": "^2.0.8"
},
"devDependencies": {
Expand All @@ -56,6 +54,7 @@
"@types/luxon": "^1.27.0",
"@types/object-inspect": "^1.8.0",
"@types/uuid": "^8.3.0",
"@types/varint": "^6.0.0",
"rimraf": "~3.0.2",
"tslog": "^3.2.0",
"typescript": "~4.3.0"
Expand Down
18 changes: 9 additions & 9 deletions packages/core/src/crypto/JwsService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import { inject, Lifecycle, scoped } from 'tsyringe'

import { InjectionSymbols } from '../constants'
import { AriesFrameworkError } from '../error'
import { JsonEncoder, BufferEncoder } from '../utils'
import { JsonEncoder, TypedArrayEncoder } from '../utils'
import { Wallet } from '../wallet'
import { WalletError } from '../wallet/error'

Expand All @@ -23,11 +23,11 @@ export class JwsService {
}

public async createJws({ payload, verkey, header }: CreateJwsOptions): Promise<JwsGeneralFormat> {
const base64Payload = BufferEncoder.toBase64URL(payload)
const base64Payload = TypedArrayEncoder.toBase64URL(payload)
const base64Protected = JsonEncoder.toBase64URL(this.buildProtected(verkey))

const signature = BufferEncoder.toBase64URL(
await this.wallet.sign(BufferEncoder.fromString(`${base64Protected}.${base64Payload}`), verkey)
const signature = TypedArrayEncoder.toBase64URL(
await this.wallet.sign(TypedArrayEncoder.fromString(`${base64Protected}.${base64Payload}`), verkey)
)

return {
Expand All @@ -41,7 +41,7 @@ export class JwsService {
* Verify a a JWS
*/
public async verifyJws({ jws, payload }: VerifyJwsOptions): Promise<VerifyJwsResult> {
const base64Payload = BufferEncoder.toBase64URL(payload)
const base64Payload = TypedArrayEncoder.toBase64URL(payload)
const signatures = 'signatures' in jws ? jws.signatures : [jws]

if (signatures.length === 0) {
Expand All @@ -60,10 +60,10 @@ export class JwsService {
throw new AriesFrameworkError('Invalid protected header')
}

const data = BufferEncoder.fromString(`${jws.protected}.${base64Payload}`)
const signature = BufferEncoder.fromBase64(jws.signature)
const data = TypedArrayEncoder.fromString(`${jws.protected}.${base64Payload}`)
const signature = TypedArrayEncoder.fromBase64(jws.signature)

const verkey = BufferEncoder.toBase58(BufferEncoder.fromBase64(protectedJson?.jwk?.x))
const verkey = TypedArrayEncoder.toBase58(TypedArrayEncoder.fromBase64(protectedJson?.jwk?.x))
signerVerkeys.push(verkey)

try {
Expand Down Expand Up @@ -102,7 +102,7 @@ export class JwsService {
jwk: {
kty: 'OKP',
crv: 'Ed25519',
x: BufferEncoder.toBase64URL(BufferEncoder.fromBase58(verkey)),
x: TypedArrayEncoder.toBase64URL(TypedArrayEncoder.fromBase58(verkey)),
},
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import type { Wallet } from '../../wallet/Wallet'

import { AriesFrameworkError } from '../../error'
import { BufferEncoder } from '../../utils/BufferEncoder'
import { JsonEncoder } from '../../utils/JsonEncoder'
import { TypedArrayEncoder } from '../../utils/TypedArrayEncoder'
import { Buffer } from '../../utils/buffer'
import timestamp from '../../utils/timestamp'

Expand All @@ -23,8 +23,8 @@ export async function unpackAndVerifySignatureDecorator(
const signerVerkey = decorator.signer

// first 8 bytes are for 64 bit integer from unix epoch
const signedData = BufferEncoder.fromBase64(decorator.signatureData)
const signature = BufferEncoder.fromBase64(decorator.signature)
const signedData = TypedArrayEncoder.fromBase64(decorator.signatureData)
const signature = TypedArrayEncoder.fromBase64(decorator.signature)

const isValid = await wallet.verify(signerVerkey, signedData, signature)

Expand Down Expand Up @@ -52,8 +52,8 @@ export async function signData(data: unknown, wallet: Wallet, signerKey: string)

const signatureDecorator = new SignatureDecorator({
signatureType: 'https://didcomm.org/signature/1.0/ed25519Sha512_single',
signature: BufferEncoder.toBase64URL(signatureBuffer),
signatureData: BufferEncoder.toBase64URL(dataBuffer),
signature: TypedArrayEncoder.toBase64URL(signatureBuffer),
signatureData: TypedArrayEncoder.toBase64URL(dataBuffer),
signer: signerKey,
})

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ describe('BasicMessageService', () => {
payload: {
basicMessageRecord: expect.objectContaining({
connectionId: mockConnectionRecord.id,
id: basicMessage.id,
id: expect.any(String),
sentTime: basicMessage.sentTime.toISOString(),
content: basicMessage.content,
role: BasicMessageRole.Receiver,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@ export class BasicMessageService {
const basicMessage = new BasicMessage({ content: message })

const basicMessageRecord = new BasicMessageRecord({
id: basicMessage.id,
sentTime: basicMessage.sentTime.toISOString(),
content: basicMessage.content,
connectionId: connectionRecord.id,
Expand All @@ -47,7 +46,6 @@ export class BasicMessageService {
*/
public async save({ message }: InboundMessageContext<BasicMessage>, connection: ConnectionRecord) {
const basicMessageRecord = new BasicMessageRecord({
id: message.id,
sentTime: message.sentTime.toISOString(),
content: message.content,
connectionId: connection.id,
Expand Down
4 changes: 2 additions & 2 deletions packages/core/src/modules/credentials/CredentialUtils.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
import type { LinkedAttachment } from '../../utils/LinkedAttachment'
import type { CredValues, Schema } from 'indy-sdk'

import { hash as sha256 } from '@stablelib/sha256'
import BigNumber from 'bn.js'

import { AriesFrameworkError } from '../../error/AriesFrameworkError'
import { Hasher } from '../../utils'
import { encodeAttachment } from '../../utils/attachment'
import { Buffer } from '../../utils/buffer'
import { isBoolean, isNumber, isString } from '../../utils/type'
Expand Down Expand Up @@ -165,7 +165,7 @@ export class CredentialUtils {
value = 'None'
}

return new BigNumber(sha256(Buffer.from(value as string))).toString()
return new BigNumber(Hasher.hash(Buffer.from(value as string), 'sha2-256')).toString()
}

private static isInt32(number: number) {
Expand Down
16 changes: 7 additions & 9 deletions packages/core/src/modules/dids/domain/Key.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
import type { KeyType } from '../../../crypto'

import { varint } from 'multiformats'

import { Buffer, BufferEncoder, MultiBaseEncoder } from '../../../utils'
import { Buffer, TypedArrayEncoder, MultiBaseEncoder, VarintEncoder } from '../../../utils'

import { getKeyTypeByMultiCodecPrefix, getMultiCodecPrefixByKeytype } from './key-type/multiCodecKey'

Expand All @@ -20,14 +18,14 @@ export class Key {
}

public static fromPublicKeyBase58(publicKey: string, keyType: KeyType) {
const publicKeyBytes = BufferEncoder.fromBase58(publicKey)
const publicKeyBytes = TypedArrayEncoder.fromBase58(publicKey)

return Key.fromPublicKey(publicKeyBytes, keyType)
}

public static fromFingerprint(fingerprint: string) {
const { data } = MultiBaseEncoder.decode(fingerprint)
const [code, byteLength] = varint.decode(data)
const [code, byteLength] = VarintEncoder.decode(data)

const publicKey = Buffer.from(data.slice(byteLength))
const keyType = getKeyTypeByMultiCodecPrefix(code)
Expand All @@ -38,18 +36,18 @@ export class Key {
public get prefixedPublicKey() {
const multiCodecPrefix = getMultiCodecPrefixByKeytype(this.keyType)

// Create Uint8Array with length of the prefix bytes, then use varint to fill the prefix bytes
const prefixBytes = varint.encodeTo(multiCodecPrefix, new Uint8Array(varint.encodingLength(multiCodecPrefix)))
// Create Buffer with length of the prefix bytes, then use varint to fill the prefix bytes
const prefixBytes = VarintEncoder.encode(multiCodecPrefix)

// Combine prefix with public key
return Buffer.concat([prefixBytes, this.publicKey])
}

public get fingerprint() {
return `z${BufferEncoder.toBase58(this.prefixedPublicKey)}`
return `z${TypedArrayEncoder.toBase58(this.prefixedPublicKey)}`
}

public get publicKeyBase58() {
return BufferEncoder.toBase58(this.publicKey)
return TypedArrayEncoder.toBase58(this.publicKey)
}
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { KeyType } from '../../../../../crypto'
import { JsonTransformer, BufferEncoder, Buffer } from '../../../../../utils'
import { JsonTransformer, TypedArrayEncoder, Buffer } from '../../../../../utils'
import keyBls12381g1Fixture from '../../../__tests__/__fixtures__/didKeyBls12381g1.json'
import { Key } from '../../Key'
import { VerificationMethod } from '../../verificationMethod'
Expand All @@ -10,12 +10,12 @@ const TEST_BLS12381G1_FINGERPRINT = 'z3tEFALUKUzzCAvytMHX8X4SnsNsq6T5tC5Zb18oQEt
const TEST_BLS12381G1_DID = `did:key:${TEST_BLS12381G1_FINGERPRINT}`
const TEST_BLS12381G1_PREFIX_BYTES = Buffer.concat([
new Uint8Array([234, 1]),
BufferEncoder.fromBase58(TEST_BLS12381G1_BASE58_KEY),
TypedArrayEncoder.fromBase58(TEST_BLS12381G1_BASE58_KEY),
])

describe('bls12381g1', () => {
it('creates a Key instance from public key bytes and bls12381g1 key type', async () => {
const publicKeyBytes = BufferEncoder.fromBase58(TEST_BLS12381G1_BASE58_KEY)
const publicKeyBytes = TypedArrayEncoder.fromBase58(TEST_BLS12381G1_BASE58_KEY)

const key = Key.fromPublicKey(publicKeyBytes, KeyType.Bls12381g1)

Expand All @@ -39,7 +39,7 @@ describe('bls12381g1', () => {

expect(key.fingerprint).toBe(TEST_BLS12381G1_FINGERPRINT)
expect(key.publicKeyBase58).toBe(TEST_BLS12381G1_BASE58_KEY)
expect(key.publicKey).toEqual(BufferEncoder.fromBase58(TEST_BLS12381G1_BASE58_KEY))
expect(key.publicKey).toEqual(TypedArrayEncoder.fromBase58(TEST_BLS12381G1_BASE58_KEY))
expect(key.keyType).toBe(KeyType.Bls12381g1)
expect(key.prefixedPublicKey.equals(TEST_BLS12381G1_PREFIX_BYTES)).toBe(true)
})
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { KeyType } from '../../../../../crypto'
import { JsonTransformer, BufferEncoder, Buffer } from '../../../../../utils'
import { JsonTransformer, TypedArrayEncoder, Buffer } from '../../../../../utils'
import keyBls12381g1g2Fixture from '../../../__tests__/__fixtures__/didKeyBls12381g1g2.json'
import { Key } from '../../Key'
import { VerificationMethod } from '../../verificationMethod'
Expand All @@ -21,12 +21,12 @@ const TEST_BLS12381G2_FINGERPRINT =

const TEST_BLS12381G1G2_PREFIX_BYTES = Buffer.concat([
new Uint8Array([238, 1]),
BufferEncoder.fromBase58(TEST_BLS12381G1G2_BASE58_KEY),
TypedArrayEncoder.fromBase58(TEST_BLS12381G1G2_BASE58_KEY),
])

describe('bls12381g1g2', () => {
it('creates a Key instance from public key bytes and bls12381g1g2 key type', async () => {
const publicKeyBytes = BufferEncoder.fromBase58(TEST_BLS12381G1G2_BASE58_KEY)
const publicKeyBytes = TypedArrayEncoder.fromBase58(TEST_BLS12381G1G2_BASE58_KEY)

const key = Key.fromPublicKey(publicKeyBytes, KeyType.Bls12381g1g2)

Expand All @@ -50,7 +50,7 @@ describe('bls12381g1g2', () => {

expect(key.fingerprint).toBe(TEST_BLS12381G1G2_FINGERPRINT)
expect(key.publicKeyBase58).toBe(TEST_BLS12381G1G2_BASE58_KEY)
expect(key.publicKey).toEqual(BufferEncoder.fromBase58(TEST_BLS12381G1G2_BASE58_KEY))
expect(key.publicKey).toEqual(TypedArrayEncoder.fromBase58(TEST_BLS12381G1G2_BASE58_KEY))
expect(key.keyType).toBe(KeyType.Bls12381g1g2)
expect(key.prefixedPublicKey.equals(TEST_BLS12381G1G2_PREFIX_BYTES)).toBe(true)
})
Expand Down Expand Up @@ -93,7 +93,7 @@ describe('bls12381g1g2', () => {

expect(g1DidKey.fingerprint).toBe(TEST_BLS12381G1_FINGERPRINT)
expect(g1DidKey.publicKeyBase58).toBe(TEST_BLS12381G1_BASE58_KEY)
expect(g1DidKey.publicKey).toEqual(BufferEncoder.fromBase58(TEST_BLS12381G1_BASE58_KEY))
expect(g1DidKey.publicKey).toEqual(TypedArrayEncoder.fromBase58(TEST_BLS12381G1_BASE58_KEY))
expect(g1DidKey.keyType).toBe(KeyType.Bls12381g1)
})

Expand All @@ -105,7 +105,7 @@ describe('bls12381g1g2', () => {

expect(g2DidKey.fingerprint).toBe(TEST_BLS12381G2_FINGERPRINT)
expect(g2DidKey.publicKeyBase58).toBe(TEST_BLS12381G2_BASE58_KEY)
expect(g2DidKey.publicKey).toEqual(BufferEncoder.fromBase58(TEST_BLS12381G2_BASE58_KEY))
expect(g2DidKey.publicKey).toEqual(TypedArrayEncoder.fromBase58(TEST_BLS12381G2_BASE58_KEY))
expect(g2DidKey.keyType).toBe(KeyType.Bls12381g2)
})
})
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { KeyType } from '../../../../../crypto'
import { JsonTransformer, BufferEncoder, Buffer } from '../../../../../utils'
import { JsonTransformer, TypedArrayEncoder, Buffer } from '../../../../../utils'
import keyBls12381g2Fixture from '../../../__tests__/__fixtures__/didKeyBls12381g2.json'
import { Key } from '../../Key'
import { VerificationMethod } from '../../verificationMethod'
Expand All @@ -12,12 +12,12 @@ const TEST_BLS12381G2_FINGERPRINT =
const TEST_BLS12381G2_DID = `did:key:${TEST_BLS12381G2_FINGERPRINT}`
const TEST_BLS12381G2_PREFIX_BYTES = Buffer.concat([
new Uint8Array([235, 1]),
BufferEncoder.fromBase58(TEST_BLS12381G2_BASE58_KEY),
TypedArrayEncoder.fromBase58(TEST_BLS12381G2_BASE58_KEY),
])

describe('bls12381g2', () => {
it('creates a Key instance from public key bytes and bls12381g2 key type', async () => {
const publicKeyBytes = BufferEncoder.fromBase58(TEST_BLS12381G2_BASE58_KEY)
const publicKeyBytes = TypedArrayEncoder.fromBase58(TEST_BLS12381G2_BASE58_KEY)

const key = Key.fromPublicKey(publicKeyBytes, KeyType.Bls12381g2)

Expand All @@ -41,7 +41,7 @@ describe('bls12381g2', () => {

expect(key.fingerprint).toBe(TEST_BLS12381G2_FINGERPRINT)
expect(key.publicKeyBase58).toBe(TEST_BLS12381G2_BASE58_KEY)
expect(key.publicKey).toEqual(BufferEncoder.fromBase58(TEST_BLS12381G2_BASE58_KEY))
expect(key.publicKey).toEqual(TypedArrayEncoder.fromBase58(TEST_BLS12381G2_BASE58_KEY))
expect(key.keyType).toBe(KeyType.Bls12381g2)
expect(key.prefixedPublicKey.equals(TEST_BLS12381G2_PREFIX_BYTES)).toBe(true)
})
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { KeyType } from '../../../../../crypto'
import { JsonTransformer, BufferEncoder, Buffer } from '../../../../../utils'
import { JsonTransformer, TypedArrayEncoder, Buffer } from '../../../../../utils'
import didKeyEd25519Fixture from '../../../__tests__/__fixtures__//didKeyEd25519.json'
import { Key } from '../../../domain/Key'
import { VerificationMethod } from '../../../domain/verificationMethod'
Expand All @@ -10,12 +10,12 @@ const TEST_ED25519_FINGERPRINT = 'z6MkmjY8GnV5i9YTDtPETC2uUAW6ejw3nk5mXF5yci5ab7
const TEST_ED25519_DID = `did:key:${TEST_ED25519_FINGERPRINT}`
const TEST_ED25519_PREFIX_BYTES = Buffer.concat([
new Uint8Array([237, 1]),
BufferEncoder.fromBase58(TEST_ED25519_BASE58_KEY),
TypedArrayEncoder.fromBase58(TEST_ED25519_BASE58_KEY),
])

describe('ed25519', () => {
it('creates a Key instance from public key bytes and ed25519 key type', async () => {
const publicKeyBytes = BufferEncoder.fromBase58(TEST_ED25519_BASE58_KEY)
const publicKeyBytes = TypedArrayEncoder.fromBase58(TEST_ED25519_BASE58_KEY)

const didKey = Key.fromPublicKey(publicKeyBytes, KeyType.Ed25519)

Expand All @@ -39,7 +39,7 @@ describe('ed25519', () => {

expect(didKey.fingerprint).toBe(TEST_ED25519_FINGERPRINT)
expect(didKey.publicKeyBase58).toBe(TEST_ED25519_BASE58_KEY)
expect(didKey.publicKey).toEqual(BufferEncoder.fromBase58(TEST_ED25519_BASE58_KEY))
expect(didKey.publicKey).toEqual(TypedArrayEncoder.fromBase58(TEST_ED25519_BASE58_KEY))
expect(didKey.keyType).toBe(KeyType.Ed25519)
expect(didKey.prefixedPublicKey.equals(TEST_ED25519_PREFIX_BYTES)).toBe(true)
})
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { KeyType } from '../../../../../crypto'
import { JsonTransformer, BufferEncoder, Buffer } from '../../../../../utils'
import { JsonTransformer, TypedArrayEncoder, Buffer } from '../../../../../utils'
import didKeyX25519Fixture from '../../../__tests__/__fixtures__/didKeyX25519.json'
import { Key } from '../../Key'
import { VerificationMethod } from '../../verificationMethod'
Expand All @@ -10,12 +10,12 @@ const TEST_X25519_FINGERPRINT = 'z6LShLeXRTzevtwcfehaGEzCMyL3bNsAeKCwcqwJxyCo63y
const TEST_X25519_DID = `did:key:${TEST_X25519_FINGERPRINT}`
const TEST_X25519_PREFIX_BYTES = Buffer.concat([
new Uint8Array([236, 1]),
BufferEncoder.fromBase58(TEST_X25519_BASE58_KEY),
TypedArrayEncoder.fromBase58(TEST_X25519_BASE58_KEY),
])

describe('x25519', () => {
it('creates a Key instance from public key bytes and x25519 key type', async () => {
const publicKeyBytes = BufferEncoder.fromBase58(TEST_X25519_BASE58_KEY)
const publicKeyBytes = TypedArrayEncoder.fromBase58(TEST_X25519_BASE58_KEY)

const didKey = Key.fromPublicKey(publicKeyBytes, KeyType.X25519)

Expand All @@ -39,7 +39,7 @@ describe('x25519', () => {

expect(didKey.fingerprint).toBe(TEST_X25519_FINGERPRINT)
expect(didKey.publicKeyBase58).toBe(TEST_X25519_BASE58_KEY)
expect(didKey.publicKey).toEqual(BufferEncoder.fromBase58(TEST_X25519_BASE58_KEY))
expect(didKey.publicKey).toEqual(TypedArrayEncoder.fromBase58(TEST_X25519_BASE58_KEY))
expect(didKey.keyType).toBe(KeyType.X25519)
expect(didKey.prefixedPublicKey.equals(TEST_X25519_PREFIX_BYTES)).toBe(true)
})
Expand Down
Loading

0 comments on commit 34b1a70

Please sign in to comment.