Skip to content

Commit

Permalink
feat: Move utils from CredentialService [DEV-3051] (#235)
Browse files Browse the repository at this point in the history
* Move utils from CredentialService

* Add tests

* Rename kid to publicKeyHex

* Review comments
  • Loading branch information
Andrew Nikitin authored Aug 9, 2023
1 parent 7f9745a commit 2836c9d
Show file tree
Hide file tree
Showing 5 changed files with 86 additions and 3 deletions.
43 changes: 41 additions & 2 deletions package-lock.json

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

2 changes: 2 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -61,11 +61,13 @@
"@cosmjs/tendermint-rpc": "^0.31.0",
"@cosmjs/utils": "^0.31.0",
"@stablelib/ed25519": "^1.0.3",
"@types/secp256k1": "^4.0.3",
"cosmjs-types": "^0.8.0",
"did-jwt": "^7.2.4",
"did-resolver": "^4.1.0",
"file-type": "^18.5.0",
"multiformats": "^12.0.1",
"secp256k1": "^5.0.0",
"uuid": "^9.0.0"
},
"devDependencies": {
Expand Down
15 changes: 15 additions & 0 deletions src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,8 @@ import {
} from '@stablelib/ed25519'
import { DirectSecp256k1HdWallet, DirectSecp256k1Wallet } from '@cosmjs/proto-signing'
import { EnglishMnemonic as _, sha256 } from '@cosmjs/crypto'
import { rawSecp256k1PubkeyToRawAddress } from '@cosmjs/amino'
import pkg from 'secp256k1'
import { v4 } from 'uuid'
import {
VerificationMethod as ProtoVerificationMethod,
Expand All @@ -36,6 +38,9 @@ import {
} from "@cheqd/ts-proto/cheqd/did/v2/index.js"
import { DIDModule } from "./modules/did.js"
import { MsgCreateResourcePayload } from "@cheqd/ts-proto/cheqd/resource/v2/index.js"
import { toBech32 } from "@cosmjs/encoding"
import { StargateClient } from "@cosmjs/stargate"
import { Coin } from "cosmjs-types/cosmos/base/v1beta1/coin"

export type TImportableEd25519Key = {
publicKeyHex: string
Expand Down Expand Up @@ -295,4 +300,14 @@ export function createMsgResourcePayloadToSign(payload: Partial<MsgCreateResourc
return MsgCreateResourcePayload.encode(
MsgCreateResourcePayload.fromPartial(payload)
).finish()
}

export function getCosmosAccount(publicKeyHex: string): string {
const { publicKeyConvert } = pkg
return toBech32('cheqd', rawSecp256k1PubkeyToRawAddress(publicKeyConvert(fromString(publicKeyHex, 'hex'), true)))
}

export async function checkBalance(address: string, rpcAddress: string): Promise<readonly Coin[]> {
const client = await StargateClient.connect(rpcAddress)
return await client.getAllBalances(address)
}
9 changes: 9 additions & 0 deletions tests/testutils.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,15 @@ export const image_content = 'iVBORw0KGgoAAAANSUhEUgAAAQAAAAEAAQMAAABmvDolAAAAA1

export const default_content = '<p>Test file content</p>'

// They are connected
export const pubkey_hex = "04adf6cad45e58a7e1908bebefcc358de229c108fb1170566f83be5ce028eb6b1997711067ffcb445532667ed4a4efc2b334c16421edb52ec5e0324a1c0e570663"

export const bech32_account = "cheqd1ehcg0jarxkyxtkzrwcxayedxrskwyftxj4exm9"
// Testnet RPC and faucet address
export const faucet_address = "cheqd1rnr5jrt4exl0samwj0yegv99jeskl0hsxmcz96"

export const testnet_rpc = "https://rpc.cheqd.network:443"

export function containsAll<T>(array: T[], values: T[]): boolean {
return values.every(value => array.includes(value))
}
Expand Down
20 changes: 19 additions & 1 deletion tests/utils.test.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
import {
TImportableEd25519Key,
createSignInputsFromImportableEd25519Key
checkBalance,
createSignInputsFromImportableEd25519Key,
getCosmosAccount
} from '../src/utils'
import {
createDidVerificationMethod,
Expand All @@ -13,6 +15,7 @@ import {
MethodSpecificIdAlgo,
VerificationMethods
} from '../src/types'
import { faucet_address, pubkey_hex, testnet_rpc } from './testutils.test'

describe('createSignInputsFromImportableEd25519Key', () => {
it('should create a sign input from an importable ed25519 key 2020', async () => {
Expand Down Expand Up @@ -74,4 +77,19 @@ describe('createSignInputsFromImportableEd25519Key', () => {

expect(signInput).toEqual({ verificationMethodId: verificationKeys.keyId, privateKeyHex: importableEd25519Key.privateKeyHex })
})

it('should get the cosmos account from publicKeyHex', () => {
// We know, that such point could be transformed to a cheqd account cheqd1ehcg0jarxkyxtkzrwcxayedxrskwyftxj4exm9
const expectedAddress = "cheqd1ehcg0jarxkyxtkzrwcxayedxrskwyftxj4exm9"

expect(expectedAddress).toEqual(getCosmosAccount(pubkey_hex))

})

it('should return not empty account balance', async () => {
const balances = await checkBalance(faucet_address, testnet_rpc)
expect(balances.length).toBeGreaterThan(0)
expect(balances[0].denom).toEqual("ncheq")
expect(+balances[0].amount).toBeGreaterThan(0)
})
})

0 comments on commit 2836c9d

Please sign in to comment.