-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #149 from AbstractSDK/adair/abstraxion-utils
Abstraxion Utils for building on XION
- Loading branch information
Showing
39 changed files
with
1,408 additions
and
119 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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", | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
# Generated file. Do not edit directly. | ||
actions/** | ||
decorators/** |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
# @abstract-money/actions-xion |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
export * from './public' | ||
export * from './wallet' |
59 changes: 59 additions & 0 deletions
59
packages/actions-xion/src/actions/public/get-xion-account-by-external-owner.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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' |
93 changes: 93 additions & 0 deletions
93
packages/actions-xion/src/actions/public/predict-xion-account-id-by-external-owner.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} | ||
} |
29 changes: 29 additions & 0 deletions
29
packages/actions-xion/src/actions/wallet/create-xion-account-externally-owned.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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, | ||
}, | ||
}, | ||
}) | ||
} |
Oops, something went wrong.