diff --git a/.github/workflows/codeql.yml b/.github/workflows/codeql.yml index 342356a..2af1ce3 100644 --- a/.github/workflows/codeql.yml +++ b/.github/workflows/codeql.yml @@ -26,7 +26,7 @@ jobs: fetch-depth: 0 - name: Initialise CodeQL - uses: github/codeql-action/init@v2 + uses: github/codeql-action/init@v3 with: languages: 'javascript' queries: security-and-quality @@ -37,4 +37,4 @@ jobs: npm run build - name: Perform CodeQL Analysis - uses: github/codeql-action/analyze@v2 + uses: github/codeql-action/analyze@v3 \ No newline at end of file diff --git a/.github/workflows/dispatch.yml b/.github/workflows/dispatch.yml index 1a3ad1b..2f92ff7 100644 --- a/.github/workflows/dispatch.yml +++ b/.github/workflows/dispatch.yml @@ -14,13 +14,12 @@ jobs: call-build: name: "Build & Test" - needs: call-lint uses: ./.github/workflows/build.yml secrets: inherit call-release: name: "Release" - needs: call-build + needs: [ call-lint, call-build ] if: ${{ github.ref_protected == true }} uses: ./.github/workflows/release.yml - secrets: inherit + secrets: inherit \ No newline at end of file diff --git a/.github/workflows/lint.yml b/.github/workflows/lint.yml index c6e690a..8a0f576 100644 --- a/.github/workflows/lint.yml +++ b/.github/workflows/lint.yml @@ -31,7 +31,7 @@ jobs: fetch-depth: 0 # Required to fetch version - name: Run Super Linter - uses: github/super-linter/slim@v4 + uses: github/super-linter/slim@v6 env: IGNORE_GITIGNORED_FILES: true DEFAULT_BRANCH: main diff --git a/src/utils.ts b/src/utils.ts index 84159f6..3a11ff1 100644 --- a/src/utils.ts +++ b/src/utils.ts @@ -29,6 +29,11 @@ import { } from '@cheqd/ts-proto/cheqd/did/v2'; import { MsgCreateResourcePayload } from '@cheqd/ts-proto/cheqd/resource/v2'; import { DIDModule } from './modules/did'; +import { toBech32 } from '@cosmjs/encoding'; +import { StargateClient } from '@cosmjs/stargate'; +import { Coin } from 'cosmjs-types/cosmos/base/v1beta1/coin'; +import { rawSecp256k1PubkeyToRawAddress } from '@cosmjs/amino'; +import pkg from 'secp256k1'; export type TImportableEd25519Key = { publicKeyHex: string; @@ -265,7 +270,7 @@ export function createCosmosPayerWallet( : DirectSecp256k1Wallet.fromKey(fromString(cosmosPayerSeed.replace(/^0x/, ''), 'hex'), 'cheqd'); } -function toMultibaseRaw(key: Uint8Array) { +export function toMultibaseRaw(key: Uint8Array) { const multibase = new Uint8Array(MULTICODEC_ED25519_HEADER.length + key.length); multibase.set(MULTICODEC_ED25519_HEADER); @@ -305,6 +310,18 @@ export function createMsgDeactivateDidDocPayloadToSign(didPayload: DIDDocument, ).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 { + const client = await StargateClient.connect(rpcAddress); + + return await client.getAllBalances(address); +} + export function createMsgResourcePayloadToSign(payload: Partial | MsgCreateResourcePayload) { return MsgCreateResourcePayload.encode(MsgCreateResourcePayload.fromPartial(payload)).finish(); } diff --git a/tests/testutils.test.ts b/tests/testutils.test.ts index 3611305..8142a2d 100644 --- a/tests/testutils.test.ts +++ b/tests/testutils.test.ts @@ -22,6 +22,16 @@ export const image_content = export const default_content = '

Test file content

'; +// 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(array: T[], values: T[]): boolean { return values.every((value) => array.includes(value)); } diff --git a/tests/utils.test.ts b/tests/utils.test.ts index 1822b9e..c032814 100644 --- a/tests/utils.test.ts +++ b/tests/utils.test.ts @@ -1,7 +1,16 @@ -import { TImportableEd25519Key, createSignInputsFromImportableEd25519Key, isJSON } from '../src/utils'; -import { createDidVerificationMethod, createVerificationKeys, createKeyPairRaw } from '../src/utils'; +import { + TImportableEd25519Key, + checkBalance, + createSignInputsFromImportableEd25519Key, + isJSON, + createDidVerificationMethod, + createVerificationKeys, + createKeyPairRaw, + getCosmosAccount, +} from '../src/utils'; import { toString } from 'uint8arrays'; import { IKeyPair, 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 () => { @@ -73,6 +82,22 @@ describe('createSignInputsFromImportableEd25519Key', () => { }); }); + 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) + }) +}) + it('should return valid json', async () => { // define invalid cases const invalid = [