Skip to content

Commit

Permalink
feat: Update proto && Accept did core spec input [DEV-2113] (#111)
Browse files Browse the repository at this point in the history
* feat: Update proto && Accept did core spec input

* test: Update integration tests

* test: Fix test cases

* build: Use latest cheqd-node

* test: Add tests for Ed25519VerificationKey2018

* feat: Update deactivate did payload & test

* fix: Minor issues

* feat: Support external signatures

* fix: SignInput check

* feat: Update utils (#114)
  • Loading branch information
DaevMithran authored Jan 24, 2023
1 parent 95d481f commit f2f710a
Show file tree
Hide file tree
Showing 14 changed files with 420 additions and 170 deletions.
10 changes: 7 additions & 3 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,9 @@ jobs:
- name: Download binary artifact
run: |
mkdir -p ${{ env.RUNNER_BIN_DIR }}
wget -c https://github.com/cheqd/cheqd-node/releases/download/v1.0.4-develop.1/cheqd-noded-1.0.4-develop.1-Linux-x86_64.tar.gz
tar -xvf cheqd-noded-1.0.4-develop.1-Linux-x86_64.tar.gz -C ${{ env.RUNNER_BIN_DIR }}
CHEQD_NODE_VERSION="$(cut -c2- <<< "$(jq -r 'map(select(.prerelease)) | first | .tag_name' <<< "$(curl --silent https://api.github.com/repos/cheqd/cheqd-node/releases)")")"
wget -c "https://github.com/cheqd/cheqd-node/releases/download/v$CHEQD_NODE_VERSION/cheqd-noded-$CHEQD_NODE_VERSION-Linux-x86_64.tar.gz"
tar -xvf "cheqd-noded-$CHEQD_NODE_VERSION-Linux-x86_64.tar.gz" -C ${{ env.RUNNER_BIN_DIR }}
sudo chmod +x ${{ env.RUNNER_BIN_DIR }}/cheqd-noded
- name: Restore binary permissions
Expand All @@ -45,7 +46,10 @@ jobs:
- name: Set up Docker localnet
working-directory: ./docker/localnet
run: |
docker compose --env-file build-latest.env up --detach --no-build
CHEQD_NODE_VERSION="$(cut -c2- <<< "$(jq -r 'map(select(.prerelease)) | first | .tag_name' <<< "$(curl --silent https://api.github.com/repos/cheqd/cheqd-node/releases)")")"
BUILD_IMAGE="ghcr.io/cheqd/cheqd-node:$CHEQD_NODE_VERSION"
export BUILD_IMAGE
docker compose up --detach
- name: Import keys
working-directory: ./docker/localnet
Expand Down
24 changes: 14 additions & 10 deletions package-lock.json

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

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@
},
"homepage": "https://github.com/cheqd/sdk#readme",
"dependencies": {
"@cheqd/ts-proto": "^1.0.16-develop.2",
"@cheqd/ts-proto": "^1.0.16-develop.3",
"@cosmjs/amino": "^0.29.4",
"@cosmjs/encoding": "^0.29.4",
"@cosmjs/math": "^0.29.4",
Expand Down
4 changes: 1 addition & 3 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,5 @@ export {
createKeyPairHex,
createVerificationKeys,
createDidVerificationMethod,
createDidPayload,
createDidPayloadWithSignInputs,
createSignInputsFromKeyPair
createDidPayload
} from './utils'
40 changes: 28 additions & 12 deletions src/modules/did.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,18 +2,17 @@ import { createProtobufRpcClient, DeliverTxResponse, QueryClient } from "@cosmjs
/* import { QueryClientImpl } from '@cheqd/ts-proto/cheqd/did/v1/query' */
import { CheqdExtension, AbstractCheqdSDKModule, MinimalImportableCheqdSDKModule } from "./_"
import { CheqdSigningStargateClient } from "../signer"
import { DidStdFee, IContext, ISignInputs, MsgDeactivateDidPayload } from "../types"
import { DidStdFee, IContext, ISignInputs, MsgCreateDidPayload, MsgDeactivateDidPayload, MsgUpdateDidPayload, VerificationMethodPayload } from "../types"
import {
MsgCreateDidDoc,
MsgCreateDidDocPayload,
MsgCreateDidDocResponse,
MsgDeactivateDidDoc,
MsgDeactivateDidDocPayload,
MsgDeactivateDidDocResponse,
MsgUpdateDidDoc,
MsgUpdateDidDocPayload,
MsgUpdateDidDocResponse,
protobufPackage
protobufPackage,
SignInfo
} from "@cheqd/ts-proto/cheqd/did/v2"
import { EncodeObject, GeneratedType } from "@cosmjs/proto-signing"

Expand Down Expand Up @@ -84,6 +83,8 @@ export class DIDModule extends AbstractCheqdSDKModule {
[typeUrlMsgCreateDidDocResponse, MsgCreateDidDocResponse],
[typeUrlMsgUpdateDidDoc, MsgUpdateDidDoc],
[typeUrlMsgUpdateDidDocResponse, MsgUpdateDidDocResponse],
[typeUrlMsgDeactivateDidDoc, MsgDeactivateDidDoc],
[typeUrlMsgDeactivateDidDocResponse, MsgDeactivateDidDocResponse],
]

constructor(signer: CheqdSigningStargateClient) {
Expand All @@ -99,13 +100,18 @@ export class DIDModule extends AbstractCheqdSDKModule {
return DIDModule.registryTypes
}

async createDidTx(signInputs: ISignInputs[], didPayload: Partial<MsgCreateDidDocPayload>, address: string, fee: DidStdFee | 'auto' | number, memo?: string, context?: IContext): Promise<DeliverTxResponse> {
async createDidTx(signInputs: ISignInputs[] | SignInfo[], didPayload: Partial<MsgCreateDidPayload>, address: string, fee: DidStdFee | 'auto' | number, memo?: string, context?: IContext): Promise<DeliverTxResponse> {
if (!this._signer) {
this._signer = context!.sdk!.signer
}

const payload = MsgCreateDidDocPayload.fromPartial(didPayload)
const signatures = await this._signer.signCreateDidTx(signInputs, payload)
const payload = MsgCreateDidPayload.transformPayload(didPayload)
let signatures: SignInfo[]
if(ISignInputs.isSignInput(signInputs)) {
signatures = await this._signer.signCreateDidTx(signInputs, payload)
} else {
signatures = signInputs
}

const value: MsgCreateDidDoc = {
payload,
Expand All @@ -125,13 +131,18 @@ export class DIDModule extends AbstractCheqdSDKModule {
)
}

async updateDidTx(signInputs: ISignInputs[], didPayload: Partial<MsgUpdateDidDocPayload>, address: string, fee: DidStdFee | 'auto' | number, memo?: string, context?: IContext): Promise<DeliverTxResponse> {
async updateDidTx(signInputs: ISignInputs[] | SignInfo[], didPayload: Partial<MsgUpdateDidPayload>, address: string, fee: DidStdFee | 'auto' | number, memo?: string, context?: IContext): Promise<DeliverTxResponse> {
if (!this._signer) {
this._signer = context!.sdk!.signer
}

const payload = MsgUpdateDidDocPayload.fromPartial(didPayload)
const signatures = await this._signer.signUpdateDidTx(signInputs, payload)
const payload = MsgCreateDidPayload.transformPayload(didPayload)
let signatures: SignInfo[]
if(ISignInputs.isSignInput(signInputs)) {
signatures = await this._signer.signUpdateDidTx(signInputs, payload)
} else {
signatures = signInputs
}

const value: MsgUpdateDidDoc = {
payload,
Expand All @@ -151,13 +162,18 @@ export class DIDModule extends AbstractCheqdSDKModule {
)
}

async deactivateDidTx(signInputs: ISignInputs[], didPayload: MsgDeactivateDidPayload, address: string, fee: DidStdFee | 'auto' | number, memo?: string, context?: IContext): Promise<DeliverTxResponse> {
async deactivateDidTx(signInputs: ISignInputs[] | SignInfo[], didPayload: MsgDeactivateDidPayload, address: string, fee: DidStdFee | 'auto' | number, memo?: string, context?: IContext): Promise<DeliverTxResponse> {
if (!this._signer) {
this._signer = context!.sdk!.signer
}

const payload = MsgDeactivateDidDocPayload.fromPartial({id: didPayload.id, versionId: didPayload.versionId})
const signatures = await this._signer.signDeactivateDidTx(signInputs, payload, didPayload.verificationMethod)
let signatures: SignInfo[]
if(ISignInputs.isSignInput(signInputs)) {
signatures = await this._signer.signDeactivateDidTx(signInputs, payload, didPayload.verificationMethod.map((e)=>VerificationMethodPayload.transformPayload(e)))
} else {
signatures = signInputs
}

const value: MsgDeactivateDidDoc = {
payload,
Expand Down
12 changes: 9 additions & 3 deletions src/modules/resource.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import { EncodeObject, GeneratedType } from "@cosmjs/proto-signing"
import { DidStdFee, IContext, ISignInputs } from '../types';
import { MsgCreateResource, MsgCreateResourcePayload, MsgCreateResourceResponse, protobufPackage } from "@cheqd/ts-proto/cheqd/resource/v2"
import { DeliverTxResponse } from "@cosmjs/stargate"
import { SignInfo } from "@cheqd/ts-proto/cheqd/did/v2";

export const typeUrlMsgCreateResource = `/${protobufPackage}.MsgCreateResource`
export const typeUrlMsgCreateResourceResponse = `/${protobufPackage}.MsgCreateResourceResponse`
Expand Down Expand Up @@ -34,17 +35,22 @@ export class ResourceModule extends AbstractCheqdSDKModule {
return []
}

static async signPayload(payload: MsgCreateResourcePayload, signInputs: ISignInputs[]): Promise<MsgCreateResource> {
static async signPayload(payload: MsgCreateResourcePayload, signInputs: ISignInputs[] | SignInfo[]): Promise<MsgCreateResource> {
const signBytes = MsgCreateResourcePayload.encode(payload).finish()
const signatures = await CheqdSigningStargateClient.signIdentityTx(signBytes, signInputs)
let signatures: SignInfo[]
if(ISignInputs.isSignInput(signInputs)) {
signatures = await CheqdSigningStargateClient.signIdentityTx(signBytes, signInputs)
} else {
signatures = signInputs
}

return {
payload,
signatures
}
}

async createResourceTx(signInputs: ISignInputs[], resourcePayload: Partial<MsgCreateResourcePayload>, address: string, fee: DidStdFee | 'auto' | number, memo?: string, context?: IContext): Promise<DeliverTxResponse> {
async createResourceTx(signInputs: ISignInputs[] | SignInfo[], resourcePayload: Partial<MsgCreateResourcePayload>, address: string, fee: DidStdFee | 'auto' | number, memo?: string, context?: IContext): Promise<DeliverTxResponse> {
if (!this._signer) {
this._signer = context!.sdk!.signer
}
Expand Down
12 changes: 6 additions & 6 deletions src/signer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import { DeliverTxResponse, GasPrice, HttpEndpoint, QueryClient, SigningStargate
import { Tendermint34Client } from "@cosmjs/tendermint-rpc"
import { createDefaultCheqdRegistry } from "./registry"
import { MsgCreateDidDocPayload, SignInfo, MsgUpdateDidDocPayload, MsgDeactivateDidDocPayload, VerificationMethod } from '@cheqd/ts-proto/cheqd/did/v2';
import { DidStdFee, ISignInputs, TSignerAlgo, VerificationMethods } from './types';
import { DidStdFee, ISignInputs, TSignerAlgo, VerificationMethodPayload, VerificationMethods } from './types';
import { base64ToBytes, EdDSASigner, hexToBytes, Signer, ES256Signer, ES256KSigner } from 'did-jwt';
import { assert, assertDefined } from '@cosmjs/utils'
import { encodeSecp256k1Pubkey } from '@cosmjs/amino'
Expand Down Expand Up @@ -176,11 +176,11 @@ export class CheqdSigningStargateClient extends SigningStargateClient {
}

verificationMethods.forEach((verificationMethod) => {
if (!(Object.values(VerificationMethods) as string[]).includes(verificationMethod.type ?? '')) {
throw new Error(`Unsupported verification method type: ${verificationMethod.type}`)
if (!(Object.values(VerificationMethods) as string[]).includes(verificationMethod.verificationMethodType ?? '')) {
throw new Error(`Unsupported verification method type: ${verificationMethod.verificationMethodType}`)
}
if (!this.didSigners[verificationMethod.type ?? '']) {
this.didSigners[verificationMethod.type ?? ''] = EdDSASigner
if (!this.didSigners[verificationMethod.verificationMethodType ?? '']) {
this.didSigners[verificationMethod.verificationMethodType ?? ''] = EdDSASigner
}
})

Expand All @@ -189,7 +189,7 @@ export class CheqdSigningStargateClient extends SigningStargateClient {

async getDidSigner(verificationMethodId: string, verificationMethods: Partial<VerificationMethod>[]): Promise<(secretKey: Uint8Array) => Signer> {
await this.checkDidSigners(verificationMethods)
const verificationMethod = verificationMethods.find(method => method.id === verificationMethodId)?.type
const verificationMethod = verificationMethods.find(method => method.id === verificationMethodId)?.verificationMethodType
if (!verificationMethod) {
throw new Error(`Verification method for ${verificationMethodId} not found`)
}
Expand Down
Loading

0 comments on commit f2f710a

Please sign in to comment.