wrap up cosmos js

This commit is contained in:
Ilja 2022-03-01 15:34:37 +02:00
parent 24f5aa1a15
commit 643b38a3a8
4 changed files with 84 additions and 68 deletions

View File

@ -22,7 +22,10 @@
"ethers": "5.5.4", "ethers": "5.5.4",
"valtio": "1.3.1", "valtio": "1.3.1",
"react-code-blocks": "0.0.9-0", "react-code-blocks": "0.0.9-0",
"@cosmjs/proto-signing": "0.27.1" "@cosmjs/proto-signing": "0.27.1",
"@cosmjs/crypto": "0.27.1",
"@cosmjs/encoding": "0.27.1",
"@cosmjs/amino": "0.27.1"
}, },
"devDependencies": { "devDependencies": {
"@walletconnect/types": "2.0.0-beta.22", "@walletconnect/types": "2.0.0-beta.22",

View File

@ -1,6 +1,8 @@
import { Secp256k1Wallet } from '@cosmjs/amino'
import { fromHex } from '@cosmjs/encoding'
import { DirectSecp256k1Wallet, makeSignBytes } from '@cosmjs/proto-signing'
// @ts-expect-error // @ts-expect-error
import { SignDoc } from '@cosmjs/proto-signing/build/codec/cosmos/tx/v1beta1/tx' import { SignDoc } from '@cosmjs/proto-signing/build/codec/cosmos/tx/v1beta1/tx'
import CosmosWallet from 'cosmos-wallet'
import MnemonicKeyring from 'mnemonic-keyring' import MnemonicKeyring from 'mnemonic-keyring'
/** /**
@ -14,6 +16,7 @@ const DEFAULT_PATH = "m/44'/118'/0'/0/0"
interface IInitArguments { interface IInitArguments {
mnemonic?: string mnemonic?: string
path?: string path?: string
prefix?: string
} }
/** /**
@ -21,22 +24,31 @@ interface IInitArguments {
*/ */
export class Cosmos { export class Cosmos {
private keyring: MnemonicKeyring private keyring: MnemonicKeyring
private wallet: CosmosWallet private directSigner: DirectSecp256k1Wallet
private aminoSigner: Secp256k1Wallet
constructor(keyring: MnemonicKeyring, wallet: CosmosWallet) { constructor(
this.wallet = wallet keyring: MnemonicKeyring,
directSigner: DirectSecp256k1Wallet,
aminoSigner: Secp256k1Wallet
) {
this.directSigner = directSigner
this.keyring = keyring this.keyring = keyring
this.aminoSigner = aminoSigner
} }
static async init({ mnemonic, path }: IInitArguments) { static async init({ mnemonic, path, prefix }: IInitArguments) {
const keyring = await MnemonicKeyring.init({ mnemonic }) const keyring = await MnemonicKeyring.init({ mnemonic })
const wallet = await CosmosWallet.init(keyring.getPrivateKey(path ?? DEFAULT_PATH)) const privateKey = fromHex(keyring.getPrivateKey(path ?? DEFAULT_PATH))
const chainPrefix = prefix ?? 'cosmos'
const directSigner = await DirectSecp256k1Wallet.fromKey(privateKey, chainPrefix)
const aminoSigner = await Secp256k1Wallet.fromKey(privateKey, chainPrefix)
return new Cosmos(keyring, wallet) return new Cosmos(keyring, directSigner, aminoSigner)
} }
public async getAccount(number = 0) { public async getAccount(number = 0) {
const account = await this.wallet.getAccounts() const account = await this.directSigner.getAccounts()
return account[number] return account[number]
} }
@ -45,11 +57,14 @@ export class Cosmos {
return this.keyring.mnemonic return this.keyring.mnemonic
} }
public signDirect(address: string, signDoc: SignDoc) { public async signDirect(address: string, signDoc: SignDoc) {
return this.wallet.signDirect(address, signDoc) console.log(signDoc)
const signDocBytes = makeSignBytes(signDoc)
// @ts-expect-error
return await this.directSigner.signDirect(address, signDocBytes)
} }
public signAmino(address: string, signDoc: SignDoc) { public async signAmino(address: string, signDoc: SignDoc) {
return this.wallet.signAmino(address, signDoc) return await this.aminoSigner.signAmino(address, signDoc)
} }
} }

View File

@ -4,6 +4,7 @@ import { approveCosmosRequest, rejectCosmosRequest } from '@/utils/CosmosRequest
import { walletConnectClient } from '@/utils/WalletConnectUtil' import { walletConnectClient } from '@/utils/WalletConnectUtil'
import { Avatar, Button, Col, Container, Divider, Link, Modal, Row, Text } from '@nextui-org/react' import { Avatar, Button, Col, Container, Divider, Link, Modal, Row, Text } from '@nextui-org/react'
import { Fragment } from 'react' import { Fragment } from 'react'
import { CodeBlock, codepen } from 'react-code-blocks'
export default function SessionSignCosmosModal() { export default function SessionSignCosmosModal() {
// Get request and wallet data from store // Get request and wallet data from store
@ -78,17 +79,13 @@ export default function SessionSignCosmosModal() {
<Row> <Row>
<Col> <Col>
<Text h5>Message</Text> <Text h5>Data</Text>
<Text color="$gray400">{JSON.stringify(params.signDoc.msgs) ?? 'No Message'}</Text> <CodeBlock
</Col> showLineNumbers={false}
</Row> text={JSON.stringify(params, null, 2)}
theme={codepen}
<Divider y={2} /> language="json"
/>
<Row>
<Col>
<Text h5>Memo</Text>
<Text color="$gray400">{JSON.stringify(params.signDoc.memo)}</Text>
</Col> </Col>
</Row> </Row>

View File

@ -641,9 +641,9 @@
integrity sha512-WEezM1FWztfbzqIUbsDzFRVMxSoLy3HugVcux6KDDtTqzPsLE8NDRHfXvev66aH1i2oOKKar3/XDjbvh/OUBdg== integrity sha512-WEezM1FWztfbzqIUbsDzFRVMxSoLy3HugVcux6KDDtTqzPsLE8NDRHfXvev66aH1i2oOKKar3/XDjbvh/OUBdg==
"@humanwhocodes/config-array@^0.9.2": "@humanwhocodes/config-array@^0.9.2":
version "0.9.3" version "0.9.5"
resolved "https://registry.yarnpkg.com/@humanwhocodes/config-array/-/config-array-0.9.3.tgz#f2564c744b387775b436418491f15fce6601f63e" resolved "https://registry.yarnpkg.com/@humanwhocodes/config-array/-/config-array-0.9.5.tgz#2cbaf9a89460da24b5ca6531b8bbfc23e1df50c7"
integrity sha512-3xSMlXHh03hCcCmFc0rbKp3Ivt2PFEJnQUJDDMTJQ2wkECZWdq4GePs2ctc5H8zV+cHPaq8k2vU8mrQjA6iHdQ== integrity sha512-ObyMyWxZiCu/yTisA7uzx81s40xR2fD5Cg/2Kq7G02ajkNubJf6BopgDTmDyc3U7sXpNKM8cYOw7s7Tyr+DnCw==
dependencies: dependencies:
"@humanwhocodes/object-schema" "^1.2.1" "@humanwhocodes/object-schema" "^1.2.1"
debug "^4.1.1" debug "^4.1.1"
@ -1027,47 +1027,47 @@
integrity sha512-hppQEBDmlwhFAXKJX2KnWLYu5yMfi91yazPb2l+lbJiwW+wdo1gNeRA+3RgNSO39WYX2euey41KEwnqesU2Jew== integrity sha512-hppQEBDmlwhFAXKJX2KnWLYu5yMfi91yazPb2l+lbJiwW+wdo1gNeRA+3RgNSO39WYX2euey41KEwnqesU2Jew==
"@typescript-eslint/parser@^5.0.0": "@typescript-eslint/parser@^5.0.0":
version "5.12.0" version "5.13.0"
resolved "https://registry.yarnpkg.com/@typescript-eslint/parser/-/parser-5.12.0.tgz#0ca669861813df99ce54916f66f524c625ed2434" resolved "https://registry.yarnpkg.com/@typescript-eslint/parser/-/parser-5.13.0.tgz#0394ed8f2f849273c0bf4b811994d177112ced5c"
integrity sha512-MfSwg9JMBojMUoGjUmX+D2stoQj1CBYTCP0qnnVtu9A+YQXVKNtLjasYh+jozOcrb/wau8TCfWOkQTiOAruBog== integrity sha512-GdrU4GvBE29tm2RqWOM0P5QfCtgCyN4hXICj/X9ibKED16136l9ZpoJvCL5pSKtmJzA+NRDzQ312wWMejCVVfg==
dependencies: dependencies:
"@typescript-eslint/scope-manager" "5.12.0" "@typescript-eslint/scope-manager" "5.13.0"
"@typescript-eslint/types" "5.12.0" "@typescript-eslint/types" "5.13.0"
"@typescript-eslint/typescript-estree" "5.12.0" "@typescript-eslint/typescript-estree" "5.13.0"
debug "^4.3.2" debug "^4.3.2"
"@typescript-eslint/scope-manager@5.12.0": "@typescript-eslint/scope-manager@5.13.0":
version "5.12.0" version "5.13.0"
resolved "https://registry.yarnpkg.com/@typescript-eslint/scope-manager/-/scope-manager-5.12.0.tgz#59619e6e5e2b1ce6cb3948b56014d3a24da83f5e" resolved "https://registry.yarnpkg.com/@typescript-eslint/scope-manager/-/scope-manager-5.13.0.tgz#cf6aff61ca497cb19f0397eea8444a58f46156b6"
integrity sha512-GAMobtIJI8FGf1sLlUWNUm2IOkIjvn7laFWyRx7CLrv6nLBI7su+B7lbStqVlK5NdLvHRFiJo2HhiDF7Ki01WQ== integrity sha512-T4N8UvKYDSfVYdmJq7g2IPJYCRzwtp74KyDZytkR4OL3NRupvswvmJQJ4CX5tDSurW2cvCc1Ia1qM7d0jpa7IA==
dependencies: dependencies:
"@typescript-eslint/types" "5.12.0" "@typescript-eslint/types" "5.13.0"
"@typescript-eslint/visitor-keys" "5.12.0" "@typescript-eslint/visitor-keys" "5.13.0"
"@typescript-eslint/types@5.12.0": "@typescript-eslint/types@5.13.0":
version "5.12.0" version "5.13.0"
resolved "https://registry.yarnpkg.com/@typescript-eslint/types/-/types-5.12.0.tgz#5b4030a28222ee01e851836562c07769eecda0b8" resolved "https://registry.yarnpkg.com/@typescript-eslint/types/-/types-5.13.0.tgz#da1de4ae905b1b9ff682cab0bed6b2e3be9c04e5"
integrity sha512-JowqbwPf93nvf8fZn5XrPGFBdIK8+yx5UEGs2QFAYFI8IWYfrzz+6zqlurGr2ctShMaJxqwsqmra3WXWjH1nRQ== integrity sha512-LmE/KO6DUy0nFY/OoQU0XelnmDt+V8lPQhh8MOVa7Y5k2gGRd6U9Kp3wAjhB4OHg57tUO0nOnwYQhRRyEAyOyg==
"@typescript-eslint/typescript-estree@5.12.0": "@typescript-eslint/typescript-estree@5.13.0":
version "5.12.0" version "5.13.0"
resolved "https://registry.yarnpkg.com/@typescript-eslint/typescript-estree/-/typescript-estree-5.12.0.tgz#cabf545fd592722f0e2b4104711e63bf89525cd2" resolved "https://registry.yarnpkg.com/@typescript-eslint/typescript-estree/-/typescript-estree-5.13.0.tgz#b37c07b748ff030a3e93d87c842714e020b78141"
integrity sha512-Dd9gVeOqt38QHR0BEA8oRaT65WYqPYbIc5tRFQPkfLquVEFPD1HAtbZT98TLBkEcCkvwDYOAvuSvAD9DnQhMfQ== integrity sha512-Q9cQow0DeLjnp5DuEDjLZ6JIkwGx3oYZe+BfcNuw/POhtpcxMTy18Icl6BJqTSd+3ftsrfuVb7mNHRZf7xiaNA==
dependencies: dependencies:
"@typescript-eslint/types" "5.12.0" "@typescript-eslint/types" "5.13.0"
"@typescript-eslint/visitor-keys" "5.12.0" "@typescript-eslint/visitor-keys" "5.13.0"
debug "^4.3.2" debug "^4.3.2"
globby "^11.0.4" globby "^11.0.4"
is-glob "^4.0.3" is-glob "^4.0.3"
semver "^7.3.5" semver "^7.3.5"
tsutils "^3.21.0" tsutils "^3.21.0"
"@typescript-eslint/visitor-keys@5.12.0": "@typescript-eslint/visitor-keys@5.13.0":
version "5.12.0" version "5.13.0"
resolved "https://registry.yarnpkg.com/@typescript-eslint/visitor-keys/-/visitor-keys-5.12.0.tgz#1ac9352ed140b07ba144ebf371b743fdf537ec16" resolved "https://registry.yarnpkg.com/@typescript-eslint/visitor-keys/-/visitor-keys-5.13.0.tgz#f45ff55bcce16403b221ac9240fbeeae4764f0fd"
integrity sha512-cFwTlgnMV6TgezQynx2c/4/tx9Tufbuo9LPzmWqyRC3QC4qTGkAG1C6pBr0/4I10PAI/FlYunI3vJjIcu+ZHMg== integrity sha512-HLKEAS/qA1V7d9EzcpLFykTePmOQqOFim8oCvhY3pZgQ8Hi38hYpHd9e5GN6nQBFQNecNhws5wkS9Y5XIO0s/g==
dependencies: dependencies:
"@typescript-eslint/types" "5.12.0" "@typescript-eslint/types" "5.13.0"
eslint-visitor-keys "^3.0.0" eslint-visitor-keys "^3.0.0"
"@walletconnect/client@2.0.0-beta.22": "@walletconnect/client@2.0.0-beta.22":
@ -1390,14 +1390,15 @@ axobject-query@^2.2.0:
integrity sha512-Td525n+iPOOyUQIeBfcASuG6uJsDOITl7Mds5gFyerkWiX7qhUTdYUBlSgNMyVqtSJqwpt1kXGLdUt6SykLMRA== integrity sha512-Td525n+iPOOyUQIeBfcASuG6uJsDOITl7Mds5gFyerkWiX7qhUTdYUBlSgNMyVqtSJqwpt1kXGLdUt6SykLMRA==
"babel-plugin-styled-components@>= 1.12.0": "babel-plugin-styled-components@>= 1.12.0":
version "2.0.2" version "2.0.6"
resolved "https://registry.yarnpkg.com/babel-plugin-styled-components/-/babel-plugin-styled-components-2.0.2.tgz#0fac11402dc9db73698b55847ab1dc73f5197c54" resolved "https://registry.yarnpkg.com/babel-plugin-styled-components/-/babel-plugin-styled-components-2.0.6.tgz#6f76c7f7224b7af7edc24a4910351948c691fc90"
integrity sha512-7eG5NE8rChnNTDxa6LQfynwgHTVOYYaHJbUYSlOhk8QBXIQiMBKq4gyfHBBKPrxUcVBXVJL61ihduCpCQbuNbw== integrity sha512-Sk+7o/oa2HfHv3Eh8sxoz75/fFvEdHsXV4grdeHufX0nauCmymlnN0rGhIvfpMQSJMvGutJ85gvCGea4iqmDpg==
dependencies: dependencies:
"@babel/helper-annotate-as-pure" "^7.16.0" "@babel/helper-annotate-as-pure" "^7.16.0"
"@babel/helper-module-imports" "^7.16.0" "@babel/helper-module-imports" "^7.16.0"
babel-plugin-syntax-jsx "^6.18.0" babel-plugin-syntax-jsx "^6.18.0"
lodash "^4.17.11" lodash "^4.17.11"
picomatch "^2.3.0"
babel-plugin-syntax-jsx@^6.18.0: babel-plugin-syntax-jsx@^6.18.0:
version "6.18.0" version "6.18.0"
@ -2017,21 +2018,21 @@ eslint-plugin-react-hooks@^4.3.0:
integrity sha512-XslZy0LnMn+84NEG9jSGR6eGqaZB3133L8xewQo3fQagbQuGt7a63gf+P1NGKZavEYEC3UXaWEAA/AqDkuN6xA== integrity sha512-XslZy0LnMn+84NEG9jSGR6eGqaZB3133L8xewQo3fQagbQuGt7a63gf+P1NGKZavEYEC3UXaWEAA/AqDkuN6xA==
eslint-plugin-react@^7.27.0: eslint-plugin-react@^7.27.0:
version "7.28.0" version "7.29.2"
resolved "https://registry.yarnpkg.com/eslint-plugin-react/-/eslint-plugin-react-7.28.0.tgz#8f3ff450677571a659ce76efc6d80b6a525adbdf" resolved "https://registry.yarnpkg.com/eslint-plugin-react/-/eslint-plugin-react-7.29.2.tgz#2d4da69d30d0a736efd30890dc6826f3e91f3f7c"
integrity sha512-IOlFIRHzWfEQQKcAD4iyYDndHwTQiCMcJVJjxempf203jnNLUnW34AXLrV33+nEXoifJE2ZEGmcjKPL8957eSw== integrity sha512-ypEBTKOy5liFQXZWMchJ3LN0JX1uPI6n7MN7OPHKacqXAxq5gYC30TdO7wqGYQyxD1OrzpobdHC3hDmlRWDg9w==
dependencies: dependencies:
array-includes "^3.1.4" array-includes "^3.1.4"
array.prototype.flatmap "^1.2.5" array.prototype.flatmap "^1.2.5"
doctrine "^2.1.0" doctrine "^2.1.0"
estraverse "^5.3.0" estraverse "^5.3.0"
jsx-ast-utils "^2.4.1 || ^3.0.0" jsx-ast-utils "^2.4.1 || ^3.0.0"
minimatch "^3.0.4" minimatch "^3.1.2"
object.entries "^1.1.5" object.entries "^1.1.5"
object.fromentries "^2.0.5" object.fromentries "^2.0.5"
object.hasown "^1.1.0" object.hasown "^1.1.0"
object.values "^1.1.5" object.values "^1.1.5"
prop-types "^15.7.2" prop-types "^15.8.1"
resolve "^2.0.0-next.3" resolve "^2.0.0-next.3"
semver "^6.3.0" semver "^6.3.0"
string.prototype.matchall "^4.0.6" string.prototype.matchall "^4.0.6"
@ -2203,9 +2204,9 @@ fast-levenshtein@^2.0.6:
integrity sha1-PYpcZog6FqMMqGQ+hR8Zuqd5eRc= integrity sha1-PYpcZog6FqMMqGQ+hR8Zuqd5eRc=
fast-redact@^3.0.0: fast-redact@^3.0.0:
version "3.1.0" version "3.1.1"
resolved "https://registry.yarnpkg.com/fast-redact/-/fast-redact-3.1.0.tgz#37c26cda9cab70bc04393f7ba1feb2d176da6c6b" resolved "https://registry.yarnpkg.com/fast-redact/-/fast-redact-3.1.1.tgz#790fcff8f808c2e12fabbfb2be5cb2deda448fa0"
integrity sha512-dir8LOnvialLxiXDPESMDHGp82CHi6ZEYTVkcvdn5d7psdv9ZkkButXrOeXST4aqreIRR+N7CYlsrwFuorurVg== integrity sha512-odVmjC8x8jNeMZ3C+rPMESzXVSEU8tSWSHv9HFxP2mm89G/1WwqhrerJDQm9Zus8X6aoRgQDThKqptdNA6bt+A==
fast-safe-stringify@^2.0.7, fast-safe-stringify@^2.0.8: fast-safe-stringify@^2.0.7, fast-safe-stringify@^2.0.8:
version "2.1.1" version "2.1.1"
@ -2935,7 +2936,7 @@ minimalistic-crypto-utils@^1.0.1:
resolved "https://registry.yarnpkg.com/minimalistic-crypto-utils/-/minimalistic-crypto-utils-1.0.1.tgz#f6c00c1c0b082246e5c4d99dfb8c7c083b2b582a" resolved "https://registry.yarnpkg.com/minimalistic-crypto-utils/-/minimalistic-crypto-utils-1.0.1.tgz#f6c00c1c0b082246e5c4d99dfb8c7c083b2b582a"
integrity sha1-9sAMHAsIIkblxNmd+4x8CDsrWCo= integrity sha1-9sAMHAsIIkblxNmd+4x8CDsrWCo=
minimatch@^3.0.4: minimatch@^3.0.4, minimatch@^3.1.2:
version "3.1.2" version "3.1.2"
resolved "https://registry.yarnpkg.com/minimatch/-/minimatch-3.1.2.tgz#19cd194bfd3e428f049a70817c038d89ab4be35b" resolved "https://registry.yarnpkg.com/minimatch/-/minimatch-3.1.2.tgz#19cd194bfd3e428f049a70817c038d89ab4be35b"
integrity sha512-J7p63hRiAjw1NDEww1W7i37+ByIrOWO5XQQAzZ3VOcL0PNybwpfmV/N05zFAzwQ9USyEcX6t3UO+K5aqBQOIHw== integrity sha512-J7p63hRiAjw1NDEww1W7i37+ByIrOWO5XQQAzZ3VOcL0PNybwpfmV/N05zFAzwQ9USyEcX6t3UO+K5aqBQOIHw==
@ -3206,7 +3207,7 @@ picocolors@^1.0.0:
resolved "https://registry.yarnpkg.com/picocolors/-/picocolors-1.0.0.tgz#cb5bdc74ff3f51892236eaf79d68bc44564ab81c" resolved "https://registry.yarnpkg.com/picocolors/-/picocolors-1.0.0.tgz#cb5bdc74ff3f51892236eaf79d68bc44564ab81c"
integrity sha512-1fygroTLlHu66zi26VoTDv8yRgm0Fccecssto+MhsZ0D/DGW2sm8E8AjW7NU5VVTRt5GxbeZ5qBuJr+HyLYkjQ== integrity sha512-1fygroTLlHu66zi26VoTDv8yRgm0Fccecssto+MhsZ0D/DGW2sm8E8AjW7NU5VVTRt5GxbeZ5qBuJr+HyLYkjQ==
picomatch@^2.2.3: picomatch@^2.2.3, picomatch@^2.3.0:
version "2.3.1" version "2.3.1"
resolved "https://registry.yarnpkg.com/picomatch/-/picomatch-2.3.1.tgz#3ba3833733646d9d3e4995946c1365a67fb07a42" resolved "https://registry.yarnpkg.com/picomatch/-/picomatch-2.3.1.tgz#3ba3833733646d9d3e4995946c1365a67fb07a42"
integrity sha512-JU3teHTNjmE2VCGFzuY8EXzCDVwEqB2a8fsIvwaStHhAWJEeVd1o1QD80CU6+ZdEXXSLbSsuLwJjkCBWqRQUVA== integrity sha512-JU3teHTNjmE2VCGFzuY8EXzCDVwEqB2a8fsIvwaStHhAWJEeVd1o1QD80CU6+ZdEXXSLbSsuLwJjkCBWqRQUVA==
@ -3322,7 +3323,7 @@ process-warning@^1.0.0:
resolved "https://registry.yarnpkg.com/process-warning/-/process-warning-1.0.0.tgz#980a0b25dc38cd6034181be4b7726d89066b4616" resolved "https://registry.yarnpkg.com/process-warning/-/process-warning-1.0.0.tgz#980a0b25dc38cd6034181be4b7726d89066b4616"
integrity sha512-du4wfLyj4yCZq1VupnVSZmRsPJsNuxoDQFdCFHLaYiEbFBD7QE0a+I4D7hOxrVnh78QE/YipFAj9lXHiXocV+Q== integrity sha512-du4wfLyj4yCZq1VupnVSZmRsPJsNuxoDQFdCFHLaYiEbFBD7QE0a+I4D7hOxrVnh78QE/YipFAj9lXHiXocV+Q==
prop-types@^15.7.2: prop-types@^15.7.2, prop-types@^15.8.1:
version "15.8.1" version "15.8.1"
resolved "https://registry.yarnpkg.com/prop-types/-/prop-types-15.8.1.tgz#67d87bf1a694f48435cf332c24af10214a3140b5" resolved "https://registry.yarnpkg.com/prop-types/-/prop-types-15.8.1.tgz#67d87bf1a694f48435cf332c24af10214a3140b5"
integrity sha512-oj87CgZICdulUohogVAR7AjlC0327U4el4L6eAvOqCeudMDVU0NThNaV+b9Df4dXgSP1gXMTnPdhfe/2qDH5cg== integrity sha512-oj87CgZICdulUohogVAR7AjlC0327U4el4L6eAvOqCeudMDVU0NThNaV+b9Df4dXgSP1gXMTnPdhfe/2qDH5cg==