Skip to content

Commit

Permalink
feat: add support for base58 key encoding (uport-project#62)
Browse files Browse the repository at this point in the history
also removed direct dependency on 'buffer' and 'elliptic' and enforced linting during build

fixes uport-project#60
  • Loading branch information
mirceanis authored Apr 15, 2021
1 parent 4834295 commit d0b26af
Show file tree
Hide file tree
Showing 5 changed files with 194 additions and 101 deletions.
43 changes: 3 additions & 40 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

12 changes: 8 additions & 4 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@
"format": "prettier --write \"src/**/*.[jt]s\"",
"lint": "eslint \"src/**/*.[jt]s\"",
"build:js": "microbundle",
"build": "npm run build:js && npm run test",
"build": "npm run lint && npm run build:js && npm run test",
"prepare": "npm run build",
"release": "semantic-release --debug"
},
Expand Down Expand Up @@ -55,13 +55,13 @@
"@ethersproject/contracts": "5.1.0",
"@semantic-release/changelog": "5.0.1",
"@semantic-release/git": "9.0.0",
"@types/elliptic": "6.4.12",
"@types/jest": "26.0.22",
"@typescript-eslint/eslint-plugin": "4.22.0",
"@typescript-eslint/parser": "4.22.0",
"codecov": "3.8.1",
"eslint": "7.24.0",
"eslint-config-prettier": "8.2.0",
"ethr-did-registry": "0.0.3",
"ganache-cli": "6.12.2",
"jest": "26.6.3",
"microbundle": "0.13.0",
Expand All @@ -73,12 +73,16 @@
"dependencies": {
"@ethersproject/abstract-signer": "^5.1.0",
"@ethersproject/address": "^5.1.0",
"@ethersproject/base64": "^5.1.0",
"@ethersproject/basex": "^5.1.0",
"@ethersproject/bytes": "^5.1.0",
"@ethersproject/providers": "^5.1.0",
"@ethersproject/signing-key": "^5.1.0",
"@ethersproject/strings": "^5.1.0",
"@ethersproject/transactions": "^5.1.0",
"@ethersproject/wallet": "^5.1.0",
"buffer": "^6.0.3",
"did-jwt": "^5.1.2",
"did-resolver": "^3.1.0",
"ethr-did-registry": "0.0.3",
"ethr-did-resolver": "^4.1.0"
}
}
91 changes: 71 additions & 20 deletions src/__tests__/index-test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@ describe('EthrDID', () => {
describe('add signing delegate', () => {
beforeAll(async () => {
const txHash = await ethrDid.addDelegate(delegate1, {
expiresIn: 100,
expiresIn: 86400,
})
await provider.waitForTransaction(txHash)
})
Expand Down Expand Up @@ -280,7 +280,7 @@ describe('EthrDID', () => {
await ethrDid.setAttribute(
'did/pub/Secp256k1/veriKey',
'0x02b97c30de767f084ce3080168ee293053ba33b235d7116a3263d29f1450936b71',
10
86400
)
})

Expand Down Expand Up @@ -321,7 +321,7 @@ describe('EthrDID', () => {
await ethrDid.setAttribute(
'did/pub/Ed25519/veriKey/base64',
'Arl8MN52fwhM4wgBaO4pMFO6M7I11xFqMmPSnxRQk2tx',
10
86400
)
})

Expand Down Expand Up @@ -368,7 +368,7 @@ describe('EthrDID', () => {
await ethrDid.setAttribute(
'did/pub/Ed25519/veriKey/base64',
Buffer.from('f2b97c30de767f084ce3080168ee293053ba33b235d7116a3263d29f1450936b72', 'hex'),
10
86400
)
})

Expand Down Expand Up @@ -420,7 +420,7 @@ describe('EthrDID', () => {
describe('service endpoints', () => {
describe('HubService', () => {
beforeAll(async () => {
await ethrDid.setAttribute('did/svc/HubService', 'https://hubs.uport.me', 100)
await ethrDid.setAttribute('did/svc/HubService', 'https://hubs.uport.me', 86400)
})
it('resolves document', async () => {
return expect((await resolver.resolve(did)).didDocument).toEqual({
Expand Down Expand Up @@ -604,13 +604,11 @@ describe('EthrDID', () => {

describe('plain vanilla keypair account', () => {
it('should sign valid jwt', async () => {
const kp: KeyPair = EthrDID.createKeyPair()
const kp: KeyPair = EthrDID.createKeyPair('dev')
plainDid = new EthrDID({
identifier: kp.publicKey,
privateKey: kp.privateKey,
...kp,
provider,
registry: registry,
chainNameOrId: 'dev',
})
const jwt = await plainDid.signJWT({ hello: 'world' })
const { payload } = await verifyJWT(jwt, { resolver })
Expand All @@ -620,36 +618,31 @@ describe('EthrDID', () => {
})

describe('verifyJWT', () => {
const kp: KeyPair = EthrDID.createKeyPair()
const ethrDid = new EthrDID({
identifier: kp.publicKey,
privateKey: kp.privateKey,
chainNameOrId: 'dev',
})
const did = ethrDid.did
const ethrDidAsIssuer = new EthrDID(EthrDID.createKeyPair('dev'))
const did = ethrDidAsIssuer.did

it('verifies the signature of the JWT', async () => {
return ethrDid
return ethrDidAsIssuer
.signJWT({ hello: 'friend' })
.then((jwt) => plainDid.verifyJWT(jwt, resolver))
.then(({ issuer }) => expect(issuer).toEqual(did))
})

describe('uses did for verifying aud claim', () => {
it('verifies the signature of the JWT', () => {
return ethrDid
return ethrDidAsIssuer
.signJWT({ hello: 'friend', aud: plainDid.did })
.then((jwt) => plainDid.verifyJWT(jwt, resolver))
.then(({ issuer }) => expect(issuer).toEqual(did))
})

it('fails if wrong did', () => {
return ethrDid
return ethrDidAsIssuer
.signJWT({ hello: 'friend', aud: plainDid.did })
.then((jwt) => plainDid.verifyJWT(jwt, resolver))
.catch((error) =>
expect(error.message).toEqual(
`JWT audience does not match your DID: aud: ${ethrDid.did} !== yours: ${plainDid.did}`
`JWT audience does not match your DID: aud: ${ethrDidAsIssuer.did} !== yours: ${plainDid.did}`
)
)
})
Expand Down Expand Up @@ -679,9 +672,67 @@ describe('EthrDID', () => {
it('should create add the large RSA key in the hex format', async () => {
const didDocument = (await resolver.resolve(did)).didDocument
const pk = didDocument?.verificationMethod?.find((pk) => {
// eslint-disable-next-line @typescript-eslint/no-explicit-any
return typeof (<any>pk).publicKeyPem !== 'undefined'
})
// eslint-disable-next-line @typescript-eslint/no-explicit-any
expect((<any>pk).publicKeyPem).toEqual(rsa4096PublicKey)
})
})

describe('base58 key', () => {
const publicKeyBase58 = 'SYnSwQmBmVwrHoGo6mnqFCX28sr3UzAZw9yyiBTLaf2foDfxDTgNdpn3MPD4gUGi4cgunK8cnGbPS5yjVh5uAXGr'

it('supports base58 keys as hexstring', async () => {
const publicKeyHex =
'04fdd57adec3d438ea237fe46b33ee1e016eda6b585c3e27ea66686c2ea535847946393f8145252eea68afe67e287b3ed9b31685ba6c3b00060a73b9b1242d68f7'
const did = `did:ethr:dev:${delegate1}`
const didController = new EthrDID({
identifier: did,
provider,
registry,
})
await didController.setAttribute('did/pub/Secp256k1/veriKey/base58', `0x${publicKeyHex}`, 86400)
const doc = (await resolver.resolve(did)).didDocument
expect(doc?.verificationMethod).toEqual([
{
blockchainAccountId: `${delegate1}@eip155:1337`,
controller: did,
id: `${did}#controller`,
type: 'EcdsaSecp256k1RecoveryMethod2020',
},
{
controller: did,
id: `${did}#delegate-1`,
publicKeyBase58,
type: 'EcdsaSecp256k1VerificationKey2019',
},
])
})

it('supports base58 keys as string', async () => {
const did = `did:ethr:dev:${delegate2}`
const didController = new EthrDID({
identifier: did,
provider,
registry,
})
await didController.setAttribute('did/pub/Secp256k1/veriKey/base58', publicKeyBase58, 86400)
const doc = (await resolver.resolve(did)).didDocument
expect(doc?.verificationMethod).toEqual([
{
blockchainAccountId: `${delegate2}@eip155:1337`,
controller: did,
id: `${did}#controller`,
type: 'EcdsaSecp256k1RecoveryMethod2020',
},
{
controller: did,
id: `${did}#delegate-1`,
publicKeyBase58,
type: 'EcdsaSecp256k1VerificationKey2019',
},
])
})
})
})
60 changes: 59 additions & 1 deletion src/__tests__/multinet-test.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,70 @@
import { EthrDID } from '..'

describe('other networks', () => {
it('rsk - github #50', () => {
it('supports rsk - github #50', () => {
const ethrDid = new EthrDID({
identifier: '0x02b97c30de767f084ce3080168ee293053ba33b235d7116a3263d29f1450936b71',
chainNameOrId: 'rsk',
})
expect(ethrDid.did).toEqual('did:ethr:rsk:0x02b97c30de767f084ce3080168ee293053ba33b235d7116a3263d29f1450936b71')
expect(ethrDid.address).toEqual('0xC662e6c5F91B9FcD22D7FcafC80Cf8b640aed247')
})

it('supports rsk:testnet - github #50', () => {
const ethrDid = new EthrDID({
identifier: '0x02b97c30de767f084ce3080168ee293053ba33b235d7116a3263d29f1450936b71',
chainNameOrId: 'rsk:testnet',
})
expect(ethrDid.did).toEqual(
'did:ethr:rsk:testnet:0x02b97c30de767f084ce3080168ee293053ba33b235d7116a3263d29f1450936b71'
)
expect(ethrDid.address).toEqual('0xC662e6c5F91B9FcD22D7FcafC80Cf8b640aed247')
})

it('supports rsk as did string', () => {
const ethrDid = new EthrDID({
identifier: 'did:ethr:rsk:0x02b97c30de767f084ce3080168ee293053ba33b235d7116a3263d29f1450936b71',
})
expect(ethrDid.did).toEqual('did:ethr:rsk:0x02b97c30de767f084ce3080168ee293053ba33b235d7116a3263d29f1450936b71')
expect(ethrDid.address).toEqual('0xC662e6c5F91B9FcD22D7FcafC80Cf8b640aed247')
})

it('supports rsk:testnet as did string', () => {
const ethrDid = new EthrDID({
identifier: 'did:ethr:rsk:testnet:0x02b97c30de767f084ce3080168ee293053ba33b235d7116a3263d29f1450936b71',
})
expect(ethrDid.did).toEqual(
'did:ethr:rsk:testnet:0x02b97c30de767f084ce3080168ee293053ba33b235d7116a3263d29f1450936b71'
)
expect(ethrDid.address).toEqual('0xC662e6c5F91B9FcD22D7FcafC80Cf8b640aed247')
})

it('supports rsk:testnet:custom:params as did string', () => {
const ethrDid = new EthrDID({
identifier:
'did:ethr:rsk:testnet:custom:params:0x02b97c30de767f084ce3080168ee293053ba33b235d7116a3263d29f1450936b71',
})
expect(ethrDid.did).toEqual(
'did:ethr:rsk:testnet:custom:params:0x02b97c30de767f084ce3080168ee293053ba33b235d7116a3263d29f1450936b71'
)
expect(ethrDid.address).toEqual('0xC662e6c5F91B9FcD22D7FcafC80Cf8b640aed247')
})

it('supports hexstring chainId', () => {
const ethrDid = new EthrDID({
identifier: '0x02b97c30de767f084ce3080168ee293053ba33b235d7116a3263d29f1450936b71',
chainNameOrId: '0x3',
})
expect(ethrDid.did).toEqual('did:ethr:0x3:0x02b97c30de767f084ce3080168ee293053ba33b235d7116a3263d29f1450936b71')
expect(ethrDid.address).toEqual('0xC662e6c5F91B9FcD22D7FcafC80Cf8b640aed247')
})

it('supports numbered chainId', () => {
const ethrDid = new EthrDID({
identifier: '0x02b97c30de767f084ce3080168ee293053ba33b235d7116a3263d29f1450936b71',
chainNameOrId: 42,
})
expect(ethrDid.did).toEqual('did:ethr:0x2a:0x02b97c30de767f084ce3080168ee293053ba33b235d7116a3263d29f1450936b71')
expect(ethrDid.address).toEqual('0xC662e6c5F91B9FcD22D7FcafC80Cf8b640aed247')
})
})
Loading

0 comments on commit d0b26af

Please sign in to comment.