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
Next Next commit
Working script
  • Loading branch information
ntn-x2 committed Jul 19, 2023
commit f83e703ee488bd446a1327628cbdf89be4349d0c
25 changes: 11 additions & 14 deletions src/dip-sign.ts
Original file line number Diff line number Diff line change
Expand Up @@ -56,23 +56,20 @@ async function main() {
'The DID key to authorize the operation is not part of the DID Document. Please add such a key before re-trying.'
)
}
const [dipSignature, blockNumber] = await utils.generateDipTxSignature(
api,
didUri,
decodedCall,
submitterAddress,
verificationMethod,
utils.getKeypairTxSigningCallback(requiredKey)
)

const tx =
await utils.generateDipTx(
api,
didUri,
decodedCall,
submitterAddress,
verificationMethod,
utils.getKeypairTxSigningCallback(requiredKey)
)

console.log(
`
DID signature for submission via DIP: ${JSON.stringify(
utils.hexifyDipSignature(dipSignature),
null,
2
)}.
Block number used for signature generation: ${blockNumber.toString()}.
DIP tx: ${tx.toHex()}.
Please add these details to the "dipConsumer.dispatchAs" function in PolkadotJS.
`
)
Expand Down
93 changes: 82 additions & 11 deletions src/utils.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,17 @@
import type { BN } from '@polkadot/util'
import type { Call } from '@polkadot/types/interfaces'
import type { Codec } from '@polkadot/types/types'
import type { Result } from '@polkadot/types'

import { ApiPromise, Keyring } from '@polkadot/api'
import { setTimeout } from 'timers/promises'

import { ApiPromise, Keyring, WsProvider } from '@polkadot/api'
import { KeyringPair } from '@polkadot/keyring/types'
import { u8aToHex } from '@polkadot/util'
import { decodeAddress } from '@polkadot/util-crypto'

import * as Kilt from '@kiltprotocol/sdk-js'
import { dipProviderCalls, types } from '@kiltprotocol/type-definitions'

export const envNames = {
wsAddress: 'WS_ADDRESS',
Expand Down Expand Up @@ -106,7 +112,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 @@ -141,7 +147,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 @@ -176,7 +182,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 @@ -213,7 +219,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 @@ -246,7 +252,72 @@ export function parseVerificationMethod(): Kilt.VerificationKeyRelationship {
}
}

export async function generateDipTxSignature(
export async function generateDipTx(
api: ApiPromise,
did: Kilt.DidUri,
call: Call,
submitterAccount: KeyringPair['address'],
didKeyRelationship: Kilt.VerificationKeyRelationship,
sign: Kilt.SignExtrinsicCallback
): Promise<Kilt.SubmittableExtrinsic> {
const signature = await generateDipTxSignature(api, did, call, submitterAccount, didKeyRelationship, sign)
// TODO: Remove hardcoded address
const relayWs = await ApiPromise.create({ provider: new WsProvider('ws://127.0.0.1:50001') })
// TODO: Adjust this logic
const currentBlockNumber = (await relayWs.derive.chain.bestNumber()).toNumber()
if (currentBlockNumber % 2 == 0) {
console.log(`Block number ${currentBlockNumber} is even. Waiting 6 seconds then retrying.`)
await setTimeout(6_000)
}
// TODO: Remove hardcoded key
const { at: relayHash, proof: relayProof } =
await relayWs.rpc.state.getReadProof(['0xcd710b30bd2eab0352ddcc26417aa1941b3c252fcb29d88eff4f3de5de4476c363f5a4efb16ffa83d0070000'])
console.log(`Relaychain block hash targeted for the state proof: ${relayHash.toHex()}`)
// We don't wait for it, but simply send the signal
relayWs.disconnect()
// TODO: Remove hardcoded address
const providerWs = await ApiPromise.create({ provider: new WsProvider('ws://127.0.0.1:50010'), runtime: dipProviderCalls, types })
const { at: providerHash, proof: paraStateProof } =
// TODO: Remove hardcoded key
// eslint-disable-next-line max-len
await providerWs.rpc.state.getReadProof(['0xb375edf06348b4330d1e88564111cb3d5bf19e4ed2927982e234d989e812f3f3925166f4bddf5baa48435e0c89cf7c9b837846eaf6ac9f4e3a6987e963650e86d84cd8b1f0469614'])
console.log(`Provider chain block hash targeted for the state proof: ${providerHash.toHex()}`)
// We don't wait for it, but simply send the signal
providerWs.disconnect()
// TODO: Adjust the whole logic
const dipProof = (await providerWs.call.dipProvider.generateProof({
identifier: Kilt.Did.toChain(did),
// TODO: Remove hardcoded value
keys: ['0x97db64b51eabf455f2b9aa25c2f9f0ef98d2142271ff416a103b5f9c511e96db'],
accounts: [],
shouldIncludeWeb3Name: false
// eslint-disable-next-line @typescript-eslint/no-explicit-any
}) as Result<Codec, Codec>).asOk as any

// TODO: Better creation for this extrinsic (?)
const extrinsic = api.tx.dipConsumer.dispatchAs(
Kilt.Did.toChain(did),
{
paraRootProof: relayProof,
dipCommitmentProof: paraStateProof,
dipProof: {
merkleLeaves: {
blinded: dipProof.proof.blinded,
revealed: dipProof.proof.revealed,
},
didSignature: {
signature: signature[0],
blockNumber: signature[1],
}
}
},
call
)

return extrinsic
}

async function generateDipTxSignature(
api: ApiPromise,
did: Kilt.DidUri,
call: Call,
Expand All @@ -262,11 +333,12 @@ export async function generateDipTxSignature(
console.log(`DIP signature targeting block number: ${blockNumber.toHuman()}`)
const genesisHash = await api.query.system.blockHash(0)
console.log(`DIP consumer genesis hash: ${genesisHash.toHuman()}`)
const identityDetails = await api.query.dipConsumer.identityEntries(
Kilt.Did.toChain(did)
)
const identityDetailsType =
process.env[envNames.identityDetailsType] ?? defaults.identityDetailsType
const identityDetails = (await api.query.dipConsumer.identityEntries(
Kilt.Did.toChain(did)
// eslint-disable-next-line @typescript-eslint/no-explicit-any
) as any).details || api.createType(identityDetailsType, null)
console.log(
`DIP subject identity details on consumer chain: ${JSON.stringify(
identityDetails,
Expand All @@ -285,8 +357,7 @@ export async function generateDipTxSignature(
`(Call, ${identityDetailsType}, ${accountIdType}, ${blockNumberType}, Hash)`,
[
call,
// eslint-disable-next-line @typescript-eslint/no-explicit-any
(identityDetails.toJSON() as any).details,
identityDetails,
submitterAccount,
blockNumber,
genesisHash,
Expand Down