Skip to content

Commit

Permalink
Yarn lint
Browse files Browse the repository at this point in the history
  • Loading branch information
ntn-x2 committed May 2, 2024
1 parent 42c7d0b commit fe0faa0
Show file tree
Hide file tree
Showing 16 changed files with 188 additions and 117 deletions.
4 changes: 2 additions & 2 deletions src/dipProof/extensions/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,6 @@
* found in the LICENSE file in the root directory of this source tree.
*/

export type * from './types.js'
export type * from "./types.js"

export * as timeBoundDidSignature from './timeBoundDidSignature.js'
export * as timeBoundDidSignature from "./timeBoundDidSignature.js"
28 changes: 19 additions & 9 deletions src/dipProof/extensions/timeBoundDidSignature.ts
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ export type TimeBoundDidSignatureRes = {
* Generate a DID signature to be used in conjunction with a DIP proof to DID-authorize a cross-chain operation.
*
* @param params The signature generation parameters.
*
*
* @returns The generated cross-chain DID signature.
*/
export async function generateDidSignature({
Expand All @@ -101,14 +101,22 @@ export async function generateDidSignature({
.toBn()
.add(await defaultValues.validUntilOffset())
const genesis = genesisHash ?? (await api.query.system.blockHash(0))
const actualIdentityDetailsRuntimeType = identityDetailsRuntimeType ?? await defaultValues.identityDetailsRuntimeType()
const actualIdentityDetailsRuntimeType =
identityDetailsRuntimeType ??
(await defaultValues.identityDetailsRuntimeType())
const identityDetails = (
await api.query.dipConsumer.identityEntries<Option<Codec>>(didToChain(didUri))
await api.query.dipConsumer.identityEntries<Option<Codec>>(
didToChain(didUri),
)
).unwrapOr(api.createType(actualIdentityDetailsRuntimeType, null))

const signaturePayload = api
.createType(
`(Call, ${identityDetailsRuntimeType}, ${accountIdRuntimeType ?? await defaultValues.accountIdRuntimeType()}, ${blockNumberRuntimeType ?? await defaultValues.blockNumberRuntimeType()}, Hash)`,
`(Call, ${identityDetailsRuntimeType}, ${
accountIdRuntimeType ?? (await defaultValues.accountIdRuntimeType())
}, ${
blockNumberRuntimeType ?? (await defaultValues.blockNumberRuntimeType())
}, Hash)`,
[call, identityDetails, submitterAddress, blockNumber, genesis],
)
.toU8a()
Expand All @@ -128,17 +136,19 @@ export async function generateDidSignature({
* Transform a [[TimeBoundDidSignatureRes]] into an object that can be used to extend the basic DIP proof generated by [[generateDipSiblingBaseProof]].
*
* @param params The cross-chain DID signature as generated by [[generateDidSignature]].
*
*
* @returns The CODEC-ready version of the signature.
*/
export function toChain(signature: TimeBoundDidSignatureRes): Record<string, Codec> {
export function toChain(
signature: TimeBoundDidSignatureRes,
): Record<string, Codec> {
const encodedSignature = {
signature: {
[signature.type]: u8aToHex(signature.signature)
[signature.type]: u8aToHex(signature.signature),
},
validUntil: signature.validUntil
validUntil: signature.validUntil,
} as any as Codec
return {
signature: encodedSignature
signature: encodedSignature,
}
}
2 changes: 1 addition & 1 deletion src/dipProof/extensions/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,4 @@
* found in the LICENSE file in the root directory of this source tree.
*/

export type * from './timeBoundDidSignature.js'
export type * from "./timeBoundDidSignature.js"
6 changes: 3 additions & 3 deletions src/dipProof/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
* found in the LICENSE file in the root directory of this source tree.
*/

export type * from './types.js'
export type * from "./types.js"

export * from './subjectIdentity.js'
export * as extensions from './extensions/index.js'
export * from "./subjectIdentity.js"
export * as extensions from "./extensions/index.js"
7 changes: 2 additions & 5 deletions src/dipProof/subjectIdentity.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,7 @@
import { toChain } from "@kiltprotocol/did"

import type { PalletDidLookupLinkableAccountLinkableAccountId } from "@kiltprotocol/augment-api"
import type {
DidUri,
DidKey,
} from "@kiltprotocol/types"
import type { DidUri, DidKey } from "@kiltprotocol/types"
import type { ApiPromise } from "@polkadot/api"
import type { Hash } from "@polkadot/types/interfaces"
import type { Codec } from "@polkadot/types-codec/types"
Expand Down Expand Up @@ -49,7 +46,7 @@ export type DipIdentityProofRes = {
}
/**
* Generate a DIP proof that reveals the specified information about the DID subject.
*
*
* @param params The DIP proof params.
*
* @returns The generated basic DIP proof that reveals the specified parts of the DID Document, optionally revealing its web3name and any linked accounts as specified.
Expand Down
4 changes: 2 additions & 2 deletions src/dipProof/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,5 +5,5 @@
* found in the LICENSE file in the root directory of this source tree.
*/

export type * from './subjectIdentity.js'
export type * from './extensions/types.js'
export type * from "./subjectIdentity.js"
export type * from "./extensions/types.js"
6 changes: 3 additions & 3 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@
* @module @kiltprotocol/dip-sdk
*/

export type * from './types.js'
export type * from "./types.js"

export * as stateProof from './stateProof/index.js'
export * as dipProof from './dipProof/index.js'
export * as stateProof from "./stateProof/index.js"
export * as dipProof from "./dipProof/index.js"
export * from "./sibling.js"
70 changes: 37 additions & 33 deletions src/sibling.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,19 +9,15 @@ import { toChain } from "@kiltprotocol/did"
import { ApiPromise } from "@polkadot/api"
import { BN } from "@polkadot/util"

import { generateDipIdentityProof } from './dipProof/subjectIdentity.js'
import { generateProviderStateRootProof } from './stateProof/providerStateRoot.js'
import { generateDipCommitmentProof } from './stateProof/subjectDipCommitment.js'
import { generateDipIdentityProof } from "./dipProof/subjectIdentity.js"
import { generateProviderStateRootProof } from "./stateProof/providerStateRoot.js"
import { generateDipCommitmentProof } from "./stateProof/subjectDipCommitment.js"

import type { DipIdentityProofRes } from './dipProof/subjectIdentity.js'
import type { ProviderStateRootProofRes } from './stateProof/providerStateRoot.js'
import type { DipCommitmentProofRes } from './stateProof/subjectDipCommitment.js'
import type { DipIdentityProofRes } from "./dipProof/subjectIdentity.js"
import type { ProviderStateRootProofRes } from "./stateProof/providerStateRoot.js"
import type { DipCommitmentProofRes } from "./stateProof/subjectDipCommitment.js"
import type { PalletDidLookupLinkableAccountLinkableAccountId } from "@kiltprotocol/augment-api"
import type {
DidUri,
DidKey,
SubmittableExtrinsic,
} from "@kiltprotocol/types"
import type { DidUri, DidKey, SubmittableExtrinsic } from "@kiltprotocol/types"
import type { Call } from "@polkadot/types/interfaces"

const defaultValues = {
Expand All @@ -33,7 +29,7 @@ const defaultValues = {
return providerApi.rpc.chain
.getHeader(providerLastFinalizedBlockHash)
.then((h) => h.number.toBn())
}
},
}

/** The DIP proof params. */
Expand All @@ -58,15 +54,15 @@ export type DipSiblingBaseProofInput = {

/** The DIP proof result. */
export type DipSiblingBaseProofRes = {
providerHeadProof: ProviderStateRootProofRes,
dipCommitmentProof: DipCommitmentProofRes,
dipProof: DipIdentityProofRes,
proofVersion: number,
providerHeadProof: ProviderStateRootProofRes
dipCommitmentProof: DipCommitmentProofRes
dipProof: DipIdentityProofRes
proofVersion: number
}

/**
* Generate a base DIP proof according to the parameters provided, to be used on a consumer chain of which the provider chain is a sibling.
*
*
* The generated proof only contains parts of the DID Document of the subject.
* Any additional components that the consumer chain requires, e.g., a cross-chain DID signature, or the presentation of some claims about the subject, are not part of the generated proof.
* This SDK contains an `extensions` section in which chain-specific proof formats could be added, if needed.
Expand All @@ -83,9 +79,11 @@ export async function generateDipSiblingBaseProof({
relayApi,
providerBlockHeight,
includeWeb3Name,
linkedAccounts
linkedAccounts,
}: DipSiblingBaseProofInput): Promise<DipSiblingBaseProofRes> {
const actualProviderBlockHeight = providerBlockHeight ?? await defaultValues.providerBlockHeight(providerApi)
const actualProviderBlockHeight =
providerBlockHeight ??
(await defaultValues.providerBlockHeight(providerApi))
const providerHeadProof = await generateProviderStateRootProof({
relayApi,
providerApi,
Expand All @@ -108,9 +106,9 @@ export async function generateDipSiblingBaseProof({
didUri,
providerApi,
keyIds,
linkedAccounts: linkedAccounts || await defaultValues.linkedAccounts(),
linkedAccounts: linkedAccounts || (await defaultValues.linkedAccounts()),
version: proofVersion,
includeWeb3Name: includeWeb3Name || await defaultValues.includeWeb3Name(),
includeWeb3Name: includeWeb3Name || (await defaultValues.includeWeb3Name()),
})

return {
Expand All @@ -124,25 +122,31 @@ export async function generateDipSiblingBaseProof({
/** The params to create an extrinsic containing a cross-chain DIP proof and operation. */
export type GenerateDipSubmittableExtrinsicInput = {
/** Any consumer-specific pieces of information to be included in the DIP proof beyond proof-of-DID. */
additionalProofElements: Record<any, any>,
additionalProofElements: Record<any, any>
/** The [[ApiPromise]] instance of the consumer chain. */
api: ApiPromise,
api: ApiPromise
/** [[DipSiblingBaseProofRes]] as generated by [[generateDipSiblingBaseProof]]. */
baseDipProof: DipSiblingBaseProofRes,
baseDipProof: DipSiblingBaseProofRes
/** The [[Call]] on the consumer chain that requires a DIP origin to be authorized. */
call: Call
/** The [[DidUri]] of the subject performing the cross-chain operation. */
didUri: DidUri,
didUri: DidUri
}

/**
* Extend a [[DipSiblingBaseProofRes]] proof with consumer-specific components, and compiles the `dispatchAs` extrinsic following the consumer's type registry.
*
*
* @param params The consumer information.
*
*
* @returns A [[SubmittableExtrinsic]] that refers to a [[Call]] on the consumer chain being dispatched by the specified [[DidUri]].
*/
export function generateDipSubmittableExtrinsic({ additionalProofElements, api, baseDipProof, call, didUri }: GenerateDipSubmittableExtrinsicInput): SubmittableExtrinsic {
export function generateDipSubmittableExtrinsic({
additionalProofElements,
api,
baseDipProof,
call,
didUri,
}: GenerateDipSubmittableExtrinsicInput): SubmittableExtrinsic {
const { proofVersion, ...dipProof } = baseDipProof

return api.tx.dipConsumer.dispatchAs(
Expand All @@ -151,16 +155,16 @@ export function generateDipSubmittableExtrinsic({ additionalProofElements, api,
[`V${proofVersion}`]: {
providerHeadProof: {
relayBlockNumber: dipProof.providerHeadProof.relayBlockHeight,
proof: dipProof.providerHeadProof.proof.proof
proof: dipProof.providerHeadProof.proof.proof,
},
dipCommitmentProof: dipProof.dipCommitmentProof.proof.proof,
dipProof: {
blinded: dipProof.dipProof.proof.blinded,
revealed: dipProof.dipProof.proof.revealed
revealed: dipProof.dipProof.proof.revealed,
},
...additionalProofElements
}
...additionalProofElements,
},
},
call
call,
)
}
6 changes: 3 additions & 3 deletions src/stateProof/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
* found in the LICENSE file in the root directory of this source tree.
*/

export type * from './types.js'
export type * from "./types.js"

export * from './providerStateRoot.js'
export * from './subjectDipCommitment.js'
export * from "./providerStateRoot.js"
export * from "./subjectDipCommitment.js"
15 changes: 8 additions & 7 deletions src/stateProof/providerStateRoot.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import type { ReadProof } from "@polkadot/types/interfaces"

/**
* The options object provided when generating a proof for the provider state.
*
*
* Proof generation logic depends on the value of `proofVersion`.
* For more details about what each `proofVersion` provides, please refer to our docs.
*/
Expand All @@ -37,7 +37,7 @@ export type ProviderStateRootProofRes = {
}
/**
* Generate a proof for the state root of the provider.
*
*
* The value and type of the proof depends on the version specified.
* For more details about what each `proofVersion` provides, please refer to our docs.
*
Expand All @@ -49,10 +49,11 @@ export async function generateProviderStateRootProof({
providerApi,
relayApi,
providerBlockHeight,
// `proofVersion` is not used, for now, but it's added to avoid introducing unnecessary breaking changes
// proofVersion,
}: ProviderStateRootProofOpts): Promise<ProviderStateRootProofRes> {
const providerBlockHash = await providerApi.rpc.chain.getBlockHash(providerBlockHeight)
} // `proofVersion` is not used, for now, but it's added to avoid introducing unnecessary breaking changes
// proofVersion,
: ProviderStateRootProofOpts): Promise<ProviderStateRootProofRes> {
const providerBlockHash =
await providerApi.rpc.chain.getBlockHash(providerBlockHeight)
const providerApiAtBlock = await providerApi.at(providerBlockHash)
const providerParaId =
await providerApiAtBlock.query.parachainInfo.parachainId()
Expand All @@ -70,6 +71,6 @@ export async function generateProviderStateRootProof({

return {
proof,
relayBlockHeight: relayParentBlockNumber.toBn()
relayBlockHeight: relayParentBlockNumber.toBn(),
}
}
6 changes: 2 additions & 4 deletions src/stateProof/subjectDipCommitment.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,7 @@

import { toChain } from "@kiltprotocol/did"

import type {
DidUri,
} from "@kiltprotocol/types"
import type { DidUri } from "@kiltprotocol/types"
import type { ApiPromise } from "@polkadot/api"
import type { Hash, ReadProof } from "@polkadot/types/interfaces"

Expand Down Expand Up @@ -37,7 +35,7 @@ export type DipCommitmentProofRes = {
* Generate a state proof for the value of a DIP identity commitment of a specific version on the specified provider chain.
*
* For more details about what each `version` provides, please refer to our docs.
*
*
* @param params The state proof params.
*
* @returns The generated state proof.
Expand Down
4 changes: 2 additions & 2 deletions src/stateProof/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,5 +5,5 @@
* found in the LICENSE file in the root directory of this source tree.
*/

export type * from './providerStateRoot.js'
export type * from './subjectDipCommitment.js'
export type * from "./providerStateRoot.js"
export type * from "./subjectDipCommitment.js"
6 changes: 3 additions & 3 deletions src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,6 @@
* found in the LICENSE file in the root directory of this source tree.
*/

export type * from './stateProof/index.js'
export type * from './dipProof/index.js'
export type * from './sibling.js'
export type * from "./stateProof/index.js"
export type * from "./dipProof/index.js"
export type * from "./sibling.js"
Loading

0 comments on commit fe0faa0

Please sign in to comment.