Skip to content

Commit

Permalink
Merge pull request #149 from AbstractSDK/adair/abstraxion-utils
Browse files Browse the repository at this point in the history
Abstraxion Utils for building on XION
  • Loading branch information
adairrr authored Nov 23, 2024
2 parents 6c15e2b + 9d7e351 commit 3db3a23
Show file tree
Hide file tree
Showing 39 changed files with 1,408 additions and 119 deletions.
8 changes: 8 additions & 0 deletions .changeset/breezy-dodos-hear.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
---
"@abstract-money/actions-xion": patch
"@abstract-money/core": patch
"@abstract-money/cosmwasm-utils": patch
"@abstract-money/provider-xion": patch
---

Add Abtsraxion utils for building on xion, including new wallet and client actions. Publicize more types from core
9 changes: 8 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -42,12 +42,19 @@
"tsup": "^8.0.0",
"turbo": "1.10.9",
"tsx": "^4.7.1",

"typescript": "^5.0.4"
},
"simple-git-hooks": {
"pre-commit": "pnpm format && pnpm lint:fix && git add -u"
},
"pnpm": {
"patchedDependencies": {
"@cosmjs/[email protected]": "patches/@[email protected]",
"@cosmjs/[email protected]": "patches/@[email protected]",
"@usecapsule/[email protected]": "patches/@[email protected]",
"@cosmjs/[email protected]": "patches/@[email protected]"
}
},
"packageManager": "[email protected]",
"dependencies": {
"happy-dom": "^13.3.8",
Expand Down
3 changes: 3 additions & 0 deletions packages/actions-xion/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
# Generated file. Do not edit directly.
actions/**
decorators/**
1 change: 1 addition & 0 deletions packages/actions-xion/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
# @abstract-money/actions-xion
21 changes: 21 additions & 0 deletions packages/actions-xion/LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
MIT License

Copyright (c) 2024, Abstract Money

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
15 changes: 15 additions & 0 deletions packages/actions-xion/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
# XION Actions

> Abstract.js actions for interacting with XION
## Install

```bash
npm install --save @abstract-money/actions-xion
```

## Usage

## License

MIT © [adairrr](https://github.com/adairrr)
64 changes: 64 additions & 0 deletions packages/actions-xion/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
{
"name": "@abstract-money/actions-xion",
"version": "0.0.1",
"description": "Abstraxion utils",
"author": "adairrr <[email protected]>",
"license": "MIT",
"repository": "",
"engines": {
"node": ">=10"
},
"type": "module",
"scripts": {
"build": "tsup",
"clean": "rimraf dist",
"typecheck": "tsc --noEmit"
},
"dependencies": {
"@cosmjs/encoding": "0.32.3",
"@cosmjs/stargate": "0.32.3",
"@cosmjs/proto-signing": "0.32.3",
"@cosmjs/tendermint-rpc": "0.32.3",
"@cosmjs/cosmwasm-stargate": "0.32.3",
"@cosmjs/math": "0.32.3",
"protobufjs": "^7.4.0",
"long": "^5.2.3",
"type-fest": "^4.2.6",
"@abstract-money/core": "workspace:*"
},
"peerDependenciesMeta": {
"typescript": {
"optional": true
}
},
"peerDependencies": {
"typescript": ">=5.0.4"
},
"devDependencies": {
"rimraf": "^3.0.0",
"@types/node": "^20.0.0"
},
"main": "./dist/index.js",
"types": "./dist/index.d.ts",
"exports": {
".": {
"types": "./dist/index.d.ts",
"default": "./dist/index.js"
},
"./actions": {
"types": "./dist/actions/index.d.ts",
"default": "./dist/actions/index.js"
},
"./decorators": {
"types": "./dist/decorators/index.d.ts",
"default": "./dist/decorators/index.js"
},
"./package.json": "./package.json"
},
"files": [
"/actions",
"/decorators",
"/dist"
],
"sideEffects": false
}
21 changes: 21 additions & 0 deletions packages/actions-xion/src/LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
MIT License

Copyright (c) 2024, Abstract Money

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
2 changes: 2 additions & 0 deletions packages/actions-xion/src/actions/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
export * from './public'
export * from './wallet'
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
import {
AccountQueryClient,
RegistryTypes,
} from '@abstract-money/core/codegen/abstract'
import { getRegistryQueryClientFromApi } from '@abstract-money/core/src/actions'
import { CosmWasmClient } from '@cosmjs/cosmwasm-stargate'
import { predictXionAccountIdByExternalOwner } from './predict-xion-account-id-by-external-owner'

interface XionAbstractAccountByOwner {
cosmWasmClient: CosmWasmClient
apiUrl: string
owner: string
}

/**
* @unstable
* @param cosmWasmClient
* @param owner
* @param apiUrl
* @param rpcEndpoint
*/
export async function getXionAccountByExternalOwner({
cosmWasmClient,
owner,
apiUrl,
}: XionAbstractAccountByOwner): Promise<
{ client: AccountQueryClient; accountId: RegistryTypes.AccountId } | undefined
> {
const registryQueryClient = await getRegistryQueryClientFromApi({
cosmWasmClient,
apiUrl,
})

try {
const accountId = await predictXionAccountIdByExternalOwner({
owner,
cosmWasmClient,
})

const { accounts } = await registryQueryClient.accounts({
accountIds: [accountId],
})

if (accounts.length > 0) {
const client = new AccountQueryClient(
registryQueryClient.client,
accounts[0]!,
)
return {
client,
accountId,
}
}
} catch (e) {
console.debug('Error getting account by owner', e)
return undefined
}
return undefined
}
2 changes: 2 additions & 0 deletions packages/actions-xion/src/actions/public/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
export * from './get-xion-account-by-external-owner'
export * from './predict-xion-account-id-by-external-owner'
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
import { RegistryTypes } from '@abstract-money/core/src/codegen/abstract'
import { CosmWasmClient } from '@cosmjs/cosmwasm-stargate'
import { Uint64 } from '@cosmjs/math'
import { decodePubkey } from '@cosmjs/proto-signing'
import {
type Account,
QueryClient,
accountFromAny,
setupAuthExtension,
} from '@cosmjs/stargate'
import { CometClient } from '@cosmjs/tendermint-rpc'
import type Long from 'long'
import { AbstractAccount } from '../../proto/abstract-account'

export const XION_ACCOUNTS_START = 2147483648

export interface PredictXionAccountIdByOwnerParameters {
cosmWasmClient: CosmWasmClient
owner: string
}

const xionAbstractAccountSeqFromNumber = (accNumber: number) => {
return XION_ACCOUNTS_START + accNumber
}

/**
* Predicts the XION abstract account ID for a given owner address. This is used as a hack for testnet, though will be different on mainnet.
* @param owner
* @param rpcEndpoint
*/
export const predictXionAccountIdByExternalOwner = async ({
owner,
cosmWasmClient,
}: PredictXionAccountIdByOwnerParameters): Promise<RegistryTypes.AccountId> => {
// @ts-ignore
const cometClient = cosmWasmClient.cometClient as CometClient
const xionQueryClient = QueryClient.withExtensions(
cometClient,
setupAuthExtension,
)
const accAny = await xionQueryClient.auth.account(owner)

if (!accAny) {
throw new Error(`XION base account not found for owner: ${owner}`)
}

const account = customAccountFromAny(accAny)
return {
seq: xionAbstractAccountSeqFromNumber(account.accountNumber),
trace: 'local',
} satisfies RegistryTypes.AccountId
}

export interface BaseAccount {
address: string
pubKey?: Any
accountNumber: Long
sequence: Long
}

export interface Any {
typeUrl: string
value: Uint8Array
}

function accountFromBaseAccount(input: BaseAccount) {
const { address, pubKey, accountNumber, sequence } = input
let pubkey: Account['pubkey'] | null = null
if (pubKey) {
pubkey = decodePubkey(pubKey)
}
return {
address: address,
pubkey: pubkey,
accountNumber: Uint64.fromString(accountNumber.toString()).toNumber(),
sequence: Uint64.fromString(sequence.toString()).toNumber(),
}
}

export function customAccountFromAny(input: Any): Account {
const { typeUrl, value } = input
switch (typeUrl) {
case '/abstractaccount.v1.AbstractAccount': {
const abstractAccount = AbstractAccount.decode(value)
if (!abstractAccount) {
throw new Error('Failed to decode AbstractAccount')
}
return accountFromBaseAccount(abstractAccount)
}
default:
return accountFromAny(input)
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
import { OverrideProperties } from 'type-fest'
import {
CreateXionAccountParameters,
createXionAccount,
} from './create-xion-account'

export type CreateXionAccountExternallyOwnedParameters = OverrideProperties<
CreateXionAccountParameters,
{ owner: string }
>

/**
* Create a XION account with an externally owned owner, such as a wallet.
* @param signingCosmWasmClient
* @param owner
*/
export async function createXionAccountExternallyOwned({
owner,
...params
}: CreateXionAccountExternallyOwnedParameters) {
return createXionAccount({
...params,
owner: {
monarchy: {
monarch: owner,
},
},
})
}
Loading

0 comments on commit 3db3a23

Please sign in to comment.