Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
More generic code
  • Loading branch information
ntn-x2 committed Jul 19, 2023
commit a6c5138f8c846ed2ea9cd251f25618bbc95f57a1
21 changes: 14 additions & 7 deletions src/dip-sign.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@ import 'dotenv/config'

import * as Kilt from '@kiltprotocol/sdk-js'
import { ApiPromise, WsProvider } from '@polkadot/api'
import { blake2AsHex, cryptoWaitReady } from '@polkadot/util-crypto'
import { dipProviderCalls, types } from '@kiltprotocol/type-definitions'
import { blake2AsHex } from '@polkadot/util-crypto'

import * as utils from './utils'

Expand Down Expand Up @@ -36,6 +36,7 @@ async function main() {
)
}

await cryptoWaitReady()
// eslint-disable-next-line max-len
const authKey =
utils.generateAuthenticationKey() ??
Expand Down Expand Up @@ -72,15 +73,21 @@ async function main() {
)
}

const didKeyId = `#${blake2AsHex(requiredKey.publicKey, 256)}` as '#{string}'
const providerApi = await ApiPromise.create({
provider: new WsProvider(providerWsAddress),
runtime: dipProviderCalls,
types,
})
const didEncodedKey = providerApi.createType('DidDidDetailsDidPublicKey', {
publicVerificationKey: {
[requiredKey.type]: requiredKey.publicKey,
},
})
const didKeyId = `#${blake2AsHex(didEncodedKey.toU8a(), 256)}` as '#{string}'

const tx = await utils.generateDipTx(
await ApiPromise.create({ provider: new WsProvider(relayWsAddress) }),
await ApiPromise.create({
provider: new WsProvider(providerWsAddress),
runtime: dipProviderCalls,
types,
}),
providerApi,
consumerApi,
didUri,
decodedCall,
Expand Down
41 changes: 26 additions & 15 deletions src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,7 @@ export function generateAuthenticationKey(): Kilt.KiltKeyringPair | undefined {
authKeyMnemonic === undefined
? undefined
: (process.env[envNames.authKeyType] as Kilt.KeyringPair['type']) ||
defaults.authKeyType
defaults.authKeyType
if (authKeyMnemonic !== undefined) {
return new Keyring().addFromMnemonic(
authKeyMnemonic,
Expand Down Expand Up @@ -145,7 +145,7 @@ export function generateAttestationKey(): Kilt.KiltKeyringPair | undefined {
attKeyMnemonic === undefined
? undefined
: (process.env[envNames.attKeyType] as Kilt.KeyringPair['type']) ||
defaults.attKeyType
defaults.attKeyType
if (attKeyMnemonic !== undefined) {
return new Keyring().addFromMnemonic(
attKeyMnemonic,
Expand Down Expand Up @@ -180,7 +180,7 @@ export function generateDelegationKey(): Kilt.KiltKeyringPair | undefined {
delKeyMnemonic === undefined
? undefined
: (process.env[envNames.delKeyType] as Kilt.KeyringPair['type']) ||
defaults.delKeyType
defaults.delKeyType
if (delKeyMnemonic !== undefined) {
return new Keyring().addFromMnemonic(
delKeyMnemonic,
Expand Down Expand Up @@ -217,7 +217,7 @@ export function generateNewAuthenticationKey():
authKeyMnemonic === undefined
? undefined
: (process.env[envNames.newAuthKeyType] as Kilt.KeyringPair['type']) ||
defaults.authKeyType
defaults.authKeyType
if (authKeyMnemonic !== undefined) {
return new Keyring().addFromMnemonic(
authKeyMnemonic,
Expand Down Expand Up @@ -309,21 +309,32 @@ export async function generateDipTx(
`Using previous provider block hash for the state proof generation: ${previousBlockHash.toHex()}.`
)
const { proof: paraStateProof } = await providerApi.rpc.state.getReadProof(
[providerApi.query.dipConsumer.identityEntries.key(Kilt.Did.toChain(did))],
[
providerApi.query.dipProvider.identityCommitments.key(
Kilt.Did.toChain(did)
),
],
previousBlockHash
)
const dipProof = (
(await providerApi.call.dipProvider.generateProof({
identifier: Kilt.Did.toChain(did),
keys: [keyId],
accounts: [],
shouldIncludeWeb3Name: false,
// TODO: Improve this line below
})) as Result<Codec, Codec>
).asOk as unknown
console.log(
`DIP proof generated for the DID key ${keyId.substring(
1
)} (${didKeyRelationship}).`
)
const dipProof =
// eslint-disable-next-line @typescript-eslint/no-explicit-any
(
(await providerApi.call.dipProvider.generateProof({
identifier: Kilt.Did.toChain(did),
keys: [keyId.substring(1)],
accounts: [],
shouldIncludeWeb3Name: false,
// TODO: Improve this line below
})) as Result<Codec, Codec>
).asOk as any
providerApi.disconnect()

const extrinsic = providerApi.tx.dipConsumer.dispatchAs(
const extrinsic = consumerApi.tx.dipConsumer.dispatchAs(
Kilt.Did.toChain(did),
{
paraRootProof: {
Expand Down