Skip to content

Commit

Permalink
refactor!: single DID namespace (#640)
Browse files Browse the repository at this point in the history
* refactor!: removed Web3Names namespace

* refactor!: removed AccountLinks namespace

* refactor!: removed Did.Chain namespace

* refactor!: removed Did.Utils namespace

* refactor!: getEndpoint -> getService

* refactor!: resolveServiceEndpoint -> resolveService

* refactor!: didToChain -> toChain

* refactor!: didFromChain -> documentFromChain

* refactor!: parseDidUri -> parse

* refactor!: validateKiltDidUri -> validateUri

* refactor!: checkServiceEndpointSyntax -> validateService

* refactor!: uriFromChain -> fromChain

* chore: merge Web3Names.chain into Did.chain

* chore: merge full DID utils into full DID

* chore: merge light DID utils into light DID

* chore: move validateService in Did.chain

* refactor!: replace stripFragment with resourceIdToChain

* refactor!: do not export versions

* refactor!: remove KILT_DID_PREFIX

* refactor!: do not export IDidParsingResult

* fix: merge

* refactor!: do not export getNextNonce

* chore: removed unused isKiltDidUri

* chore: removed unused alg:SigningAlgorithms maps

* chore: removed unused alg:EncryptionAlgorithms maps

* refactor!: removed makeLinkingSignCallback

* refactor!: do not export verifyDidSignatureDataStructure

* fixup! chore: removed unused alg:SigningAlgorithms maps
  • Loading branch information
arty-name authored Sep 22, 2022
1 parent cb77bb7 commit b059eec
Show file tree
Hide file tree
Showing 42 changed files with 683 additions and 912 deletions.
2 changes: 0 additions & 2 deletions jest.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,6 @@ module.exports = {
'!**/*.chain.ts',
'!did/src/Did.chain.ts',
'!did/src/Did.utils.ts',
'!did/src/DidDetails/LightDidDetails.utils.ts',
'!did/src/DidDetails/FullDidDetails.utils.ts',
'!utils/src/jsonabc.ts',
],
resolver: "ts-jest-resolver",
Expand Down
68 changes: 30 additions & 38 deletions packages/core/src/__integrationtests__/AccountLinking.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@
* @group integration/accountLinking
*/

import { AccountLinks, Web3Names } from '@kiltprotocol/did'
import * as Did from '@kiltprotocol/did'
import {
createFullDidFromSeed,
Expand Down Expand Up @@ -37,13 +36,12 @@ import { disconnect } from '../kilt'
let paymentAccount: KiltKeyringPair
let paymentAccountChain: string
let linkDeposit: BN
let sign: AccountLinks.LinkingSignCallback
let api: ApiPromise

beforeAll(async () => {
api = await initializeApi()
paymentAccount = await createEndowedTestAccount()
paymentAccountChain = AccountLinks.accountToChain(paymentAccount.address)
paymentAccountChain = Did.accountToChain(paymentAccount.address)
linkDeposit = api.consts.didLookup.deposit.toBn()
}, 40_000)

Expand All @@ -60,9 +58,9 @@ describe('When there is an on-chain DID', () => {
didKey = makeSigningKeyTool()
newDidKey = makeSigningKeyTool()
did = await createFullDidFromSeed(paymentAccount, didKey.keypair)
didChain = Did.Chain.didToChain(did.uri)
didChain = Did.toChain(did.uri)
newDid = await createFullDidFromSeed(paymentAccount, newDidKey.keypair)
newDidChain = Did.Chain.didToChain(newDid.uri)
newDidChain = Did.toChain(newDid.uri)
}, 40_000)
it('should be possible to associate the tx sender', async () => {
// Check that no links exist
Expand Down Expand Up @@ -106,12 +104,12 @@ describe('When there is an on-chain DID', () => {
).toMatchInlineSnapshot('"0"')
// Check that the link has been created correctly
expect(
AccountLinks.connectedDidFromChain(
Did.connectedDidFromChain(
await api.query.didLookup.connectedDids(paymentAccountChain)
).did
).toStrictEqual(did.uri)
const encoded = await api.query.didLookup.connectedAccounts.keys(didChain)
expect(AccountLinks.connectedAccountsFromChain(encoded)).toStrictEqual([
expect(Did.connectedAccountsFromChain(encoded)).toStrictEqual([
paymentAccount.address,
])
expect(
Expand Down Expand Up @@ -145,7 +143,7 @@ describe('When there is an on-chain DID', () => {
).toMatchInlineSnapshot('"0"')
// Check that account is linked to new DID
expect(
AccountLinks.connectedDidFromChain(
Did.connectedDidFromChain(
await api.query.didLookup.connectedDids(paymentAccountChain)
).did
).toStrictEqual(newDid.uri)
Expand All @@ -165,7 +163,7 @@ describe('When there is an on-chain DID', () => {
const encoded = await api.query.didLookup.connectedAccounts.keys(
newDidChain
)
expect(AccountLinks.connectedAccountsFromChain(encoded)).toStrictEqual([
expect(Did.connectedAccountsFromChain(encoded)).toStrictEqual([
paymentAccount.address,
])
expect(
Expand Down Expand Up @@ -220,25 +218,22 @@ describe('When there is an on-chain DID', () => {
// TODO: remove this line to test against ethereum linking enabled chains
if (keyType === 'ethereum') return

const keyTool = makeSigningKeyTool(
Did.Utils.signatureAlgForKeyType[keyType]
)
const keyTool = makeSigningKeyTool(keyType as KiltKeyringPair['type'])
keypair = keyTool.keypair
keypairChain = AccountLinks.accountToChain(keypair.address)
sign = AccountLinks.makeLinkingSignCallback(keypair)
keypairChain = Did.accountToChain(keypair.address)
didKey = makeSigningKeyTool()
newDidKey = makeSigningKeyTool()
did = await createFullDidFromSeed(paymentAccount, didKey.keypair)
didChain = Did.Chain.didToChain(did.uri)
didChain = Did.toChain(did.uri)
newDid = await createFullDidFromSeed(paymentAccount, newDidKey.keypair)
newDidChain = Did.Chain.didToChain(newDid.uri)
newDidChain = Did.toChain(newDid.uri)
}, 40_000)

it('should be possible to associate the account while the sender pays the deposit', async () => {
const args = await AccountLinks.associateAccountToChainArgs(
const args = await Did.associateAccountToChainArgs(
keypair.address,
did.uri,
sign
async (payload) => keypair.sign(payload, { withType: false })
)
const signedTx = await Did.authorizeExtrinsic(
did.uri,
Expand All @@ -263,7 +258,7 @@ describe('When there is an on-chain DID', () => {
.toString()
).toMatchInlineSnapshot('"0"')
expect(
AccountLinks.connectedDidFromChain(
Did.connectedDidFromChain(
await api.query.didLookup.connectedDids(keypairChain)
).did
).toStrictEqual(did.uri)
Expand All @@ -273,7 +268,7 @@ describe('When there is an on-chain DID', () => {
const encoded = await api.query.didLookup.connectedAccounts.keys(
didChain
)
expect(AccountLinks.connectedAccountsFromChain(encoded)).toStrictEqual([
expect(Did.connectedAccountsFromChain(encoded)).toStrictEqual([
keypair.address,
])
expect(
Expand All @@ -290,10 +285,10 @@ describe('When there is an on-chain DID', () => {
).toBe(true)
})
it('should be possible to associate the account to a new DID while the sender pays the deposit', async () => {
const args = await AccountLinks.associateAccountToChainArgs(
const args = await Did.associateAccountToChainArgs(
keypair.address,
newDid.uri,
sign
async (payload) => keypair.sign(payload, { withType: false })
)
const signedTx = await Did.authorizeExtrinsic(
newDid.uri,
Expand All @@ -317,7 +312,7 @@ describe('When there is an on-chain DID', () => {
(await api.query.didLookup.connectedDids(paymentAccountChain)).isNone
).toBe(true)
expect(
AccountLinks.connectedDidFromChain(
Did.connectedDidFromChain(
await api.query.didLookup.connectedDids(keypairChain)
).did
).toStrictEqual(newDid.uri)
Expand All @@ -340,7 +335,7 @@ describe('When there is an on-chain DID', () => {
const encoded = await api.query.didLookup.connectedAccounts.keys(
newDidChain
)
expect(AccountLinks.connectedAccountsFromChain(encoded)).toStrictEqual([
expect(Did.connectedAccountsFromChain(encoded)).toStrictEqual([
keypair.address,
])
expect(
Expand Down Expand Up @@ -415,24 +410,21 @@ describe('When there is an on-chain DID', () => {
genericAccount = new Keyring({ type: 'ecdsa' }).addFromMnemonic(
mnemonicGenerate()
)
genericAccountChain = AccountLinks.accountToChain(genericAccount.address)
// also testing that signing with type bitflag works, like the polkadot extension does it
sign = async (payload) => genericAccount.sign(payload, { withType: true })

genericAccountChain = Did.accountToChain(genericAccount.address)
await fundAccount(genericAccount.address, convertToTxUnit(new BN(10), 1))
didKey = makeSigningKeyTool()
newDidKey = makeSigningKeyTool()
did = await createFullDidFromSeed(paymentAccount, didKey.keypair)
didChain = Did.Chain.didToChain(did.uri)
didChain = Did.toChain(did.uri)
newDid = await createFullDidFromSeed(paymentAccount, newDidKey.keypair)
newDidChain = Did.Chain.didToChain(newDid.uri)
newDidChain = Did.toChain(newDid.uri)
}, 40_000)

it('should be possible to associate the account while the sender pays the deposit', async () => {
const args = await AccountLinks.associateAccountToChainArgs(
const args = await Did.associateAccountToChainArgs(
genericAccount.address,
did.uri,
sign
async (payload) => genericAccount.sign(payload, { withType: true })
)
const signedTx = await Did.authorizeExtrinsic(
did.uri,
Expand All @@ -457,7 +449,7 @@ describe('When there is an on-chain DID', () => {
.toString()
).toMatchInlineSnapshot('"0"')
expect(
AccountLinks.connectedDidFromChain(
Did.connectedDidFromChain(
await api.query.didLookup.connectedDids(genericAccountChain)
).did
).toStrictEqual(did.uri)
Expand All @@ -467,7 +459,7 @@ describe('When there is an on-chain DID', () => {
const encoded = await api.query.didLookup.connectedAccounts.keys(didChain)
expect(
// Wildcard substrate encoding. Account should match the generated one.
AccountLinks.connectedAccountsFromChain(encoded, 42)
Did.connectedAccountsFromChain(encoded, 42)
).toStrictEqual([genericAccount.address])
expect(
(
Expand Down Expand Up @@ -498,14 +490,14 @@ describe('When there is an on-chain DID', () => {
await submitExtrinsic(signedTx, paymentAccount)

// Check that the Web3 name has been linked to the DID
const { owner } = Web3Names.web3NameOwnerFromChain(
const { owner } = Did.web3NameOwnerFromChain(
await api.query.web3Names.owner('test-name')
)
expect(owner).toStrictEqual(did.uri)
// Check that it is possible to retrieve the web3 name from the account linked to the DID
expect(
await AccountLinks.queryWeb3Name(genericAccount.address)
).toStrictEqual('test-name')
expect(await Did.queryWeb3Name(genericAccount.address)).toStrictEqual(
'test-name'
)
})

it('should be possible for the sender to remove the link', async () => {
Expand Down
19 changes: 8 additions & 11 deletions packages/core/src/__integrationtests__/Deposit.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ async function checkDeleteFullDid(
sign: SignCallback
): Promise<boolean> {
storedEndpointsCount = await api.query.did.didEndpointsCount(
Did.Chain.didToChain(fullDid.uri)
Did.toChain(fullDid.uri)
)
const deleteDid = api.tx.did.delete(storedEndpointsCount)

Expand All @@ -69,8 +69,8 @@ async function checkDeleteFullDid(
await api.query.system.account(identity.address)
).data

const didResult = Did.Chain.didFromChain(
await api.query.did.did(Did.Chain.didToChain(fullDid.uri))
const didResult = Did.documentFromChain(
await api.query.did.did(Did.toChain(fullDid.uri))
)
const didDeposit = didResult.deposit

Expand All @@ -90,19 +90,16 @@ async function checkReclaimFullDid(
fullDid: DidDocument
): Promise<boolean> {
storedEndpointsCount = await api.query.did.didEndpointsCount(
Did.Chain.didToChain(fullDid.uri)
)
tx = api.tx.did.reclaimDeposit(
Did.Chain.didToChain(fullDid.uri),
storedEndpointsCount
Did.toChain(fullDid.uri)
)
tx = api.tx.did.reclaimDeposit(Did.toChain(fullDid.uri), storedEndpointsCount)

const balanceBeforeRevoking = (
await api.query.system.account(identity.address)
).data

const didResult = Did.Chain.didFromChain(
await api.query.did.did(Did.Chain.didToChain(fullDid.uri))
const didResult = Did.documentFromChain(
await api.query.did.did(Did.toChain(fullDid.uri))
)
const didDeposit = didResult.deposit

Expand Down Expand Up @@ -240,7 +237,7 @@ async function checkDeletedDidReclaimAttestation(
await submitExtrinsic(authorizedTx, identity)

storedEndpointsCount = await api.query.did.didEndpointsCount(
Did.Chain.didToChain(fullDid.uri)
Did.toChain(fullDid.uri)
)

attestation = Attestation.fromCredentialAndDid(credential, fullDid.uri)
Expand Down
Loading

0 comments on commit b059eec

Please sign in to comment.