-
Notifications
You must be signed in to change notification settings - Fork 0
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Feat/index account #111
base: main
Are you sure you want to change the base?
Feat/index account #111
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change | ||||||||||||||||||||||||||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
@@ -1,45 +1,68 @@ | ||||||||||||||||||||||||||||||||||||||||
import type { CosmosMessage } from "@subql/types-cosmos"; | ||||||||||||||||||||||||||||||||||||||||
import type { MsgStoreCode } from "cosmjs-types/cosmwasm/wasm/v1/tx"; | ||||||||||||||||||||||||||||||||||||||||
import { Contract } from "../types"; | ||||||||||||||||||||||||||||||||||||||||
import { findEventAttribute } from "./helper"; | ||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||
enum AccessType { | ||||||||||||||||||||||||||||||||||||||||
ACCESS_TYPE_UNSPECIFIED = 0, | ||||||||||||||||||||||||||||||||||||||||
ACCESS_TYPE_NOBODY = 1, | ||||||||||||||||||||||||||||||||||||||||
ACCESS_TYPE_ONLY_ADDRESS = 2, | ||||||||||||||||||||||||||||||||||||||||
ACCESS_TYPE_EVERYBODY = 3, | ||||||||||||||||||||||||||||||||||||||||
ACCESS_TYPE_ANY_OF_ADDRESSES = 4, | ||||||||||||||||||||||||||||||||||||||||
UNRECOGNIZED = -1, | ||||||||||||||||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||||||||||||||||
import { AccessType } from "cosmjs-types/cosmwasm/wasm/v1/types"; | ||||||||||||||||||||||||||||||||||||||||
import { Account, Contract, ContractPermissionAccount } from "../types"; | ||||||||||||||||||||||||||||||||||||||||
import { contractPermissionAccountId, findEventAttribute } from "./helper"; | ||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||
export const handleStoreContract = async ( | ||||||||||||||||||||||||||||||||||||||||
msg: CosmosMessage<MsgStoreCode>, | ||||||||||||||||||||||||||||||||||||||||
): Promise<void> => { | ||||||||||||||||||||||||||||||||||||||||
const id = findEventAttribute(msg.tx.tx.events, "store_code", "code_id") | ||||||||||||||||||||||||||||||||||||||||
?.value; | ||||||||||||||||||||||||||||||||||||||||
const contractId = findEventAttribute( | ||||||||||||||||||||||||||||||||||||||||
msg.tx.tx.events, | ||||||||||||||||||||||||||||||||||||||||
"store_code", | ||||||||||||||||||||||||||||||||||||||||
"code_id", | ||||||||||||||||||||||||||||||||||||||||
)?.value; | ||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||
const dataHash = findEventAttribute( | ||||||||||||||||||||||||||||||||||||||||
msg.tx.tx.events, | ||||||||||||||||||||||||||||||||||||||||
"store_code", | ||||||||||||||||||||||||||||||||||||||||
"code_checksum", | ||||||||||||||||||||||||||||||||||||||||
)?.value; | ||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||
if (!id || !dataHash) { | ||||||||||||||||||||||||||||||||||||||||
if (!contractId || !dataHash) { | ||||||||||||||||||||||||||||||||||||||||
return; | ||||||||||||||||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||
const { instantiatePermission: instantiatePermissionWithEnums, sender } = | ||||||||||||||||||||||||||||||||||||||||
msg.msg.decodedMsg; | ||||||||||||||||||||||||||||||||||||||||
const { instantiatePermission, sender } = msg.msg.decodedMsg; | ||||||||||||||||||||||||||||||||||||||||
const senderAccount = await Account.get(sender); | ||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||
const instantiatePermission = instantiatePermissionWithEnums && { | ||||||||||||||||||||||||||||||||||||||||
...instantiatePermissionWithEnums, | ||||||||||||||||||||||||||||||||||||||||
permission: AccessType[instantiatePermissionWithEnums.permission], | ||||||||||||||||||||||||||||||||||||||||
}; | ||||||||||||||||||||||||||||||||||||||||
if (!senderAccount) { | ||||||||||||||||||||||||||||||||||||||||
const publicKey = msg.tx.decodedTx.authInfo.signerInfos.find( | ||||||||||||||||||||||||||||||||||||||||
(signerInfo) => signerInfo.publicKey, | ||||||||||||||||||||||||||||||||||||||||
)?.publicKey; | ||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||
await Account.create({ | ||||||||||||||||||||||||||||||||||||||||
id: sender, | ||||||||||||||||||||||||||||||||||||||||
pubKey: publicKey && { | ||||||||||||||||||||||||||||||||||||||||
typeUrl: publicKey.typeUrl, | ||||||||||||||||||||||||||||||||||||||||
key: Buffer.from(publicKey.value).toString("base64"), | ||||||||||||||||||||||||||||||||||||||||
}, | ||||||||||||||||||||||||||||||||||||||||
balances: [], | ||||||||||||||||||||||||||||||||||||||||
}).save(); | ||||||||||||||||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||
const permission = | ||||||||||||||||||||||||||||||||||||||||
instantiatePermission && AccessType[instantiatePermission.permission]; | ||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||
await Contract.create({ | ||||||||||||||||||||||||||||||||||||||||
id, | ||||||||||||||||||||||||||||||||||||||||
id: contractId, | ||||||||||||||||||||||||||||||||||||||||
dataHash, | ||||||||||||||||||||||||||||||||||||||||
creator: sender, | ||||||||||||||||||||||||||||||||||||||||
instantiatePermission, | ||||||||||||||||||||||||||||||||||||||||
creatorId: sender, | ||||||||||||||||||||||||||||||||||||||||
permission, | ||||||||||||||||||||||||||||||||||||||||
}).save(); | ||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||
for (const permissionAddress in instantiatePermission?.addresses) { | ||||||||||||||||||||||||||||||||||||||||
if (!(await Account.get(permissionAddress))) { | ||||||||||||||||||||||||||||||||||||||||
await Account.create({ | ||||||||||||||||||||||||||||||||||||||||
id: permissionAddress, | ||||||||||||||||||||||||||||||||||||||||
balances: [], | ||||||||||||||||||||||||||||||||||||||||
}).save(); | ||||||||||||||||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||
await ContractPermissionAccount.create({ | ||||||||||||||||||||||||||||||||||||||||
id: contractPermissionAccountId(contractId, sender), | ||||||||||||||||||||||||||||||||||||||||
contractId: permissionAddress, | ||||||||||||||||||||||||||||||||||||||||
accountId: sender, | ||||||||||||||||||||||||||||||||||||||||
}).save(); | ||||||||||||||||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||||||||||||||||
Comment on lines
+54
to
+67
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The code iterates over - await ContractPermissionAccount.create({
- id: contractPermissionAccountId(contractId, sender),
- contractId: permissionAddress,
- accountId: sender,
- }).save();
+ await ContractPermissionAccount.create({
+ id: contractPermissionAccountId(contractId, permissionAddress),
+ contractId: contractId,
+ accountId: permissionAddress,
+ }).save(); Committable suggestion (Beta)
Suggested change
|
||||||||||||||||||||||||||||||||||||||||
}; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Not correct. What you have is a protobuf
Any
. This Any type has atypeUrl
that tells you what kind of data it contains, and a value which is the data itself in a specific format. You can't just convert the data into base64 directly, because you'll only get the binary form of that data.Instead, you should use the
typeUrl
to understand what type is, knowing that, decode the binary data into its proper form. After that, you can get a string representation of it.For instance, for the secp256k1 public key type, you can get the compressed base64 representation like this:
which gives:
Verification:
➜ okp4d debug pubkey-raw "A8Ny+hVbRIB8h5iiF7d4rSS+9ts06W2t1p5riyOIl1Pm" Parsed key as secp256k1 Address: 1BC29537C771DCA2C365EE81B39B44042F916DF6 ... ➜ okp4d debug addr 1BC29537C771DCA2C365EE81B39B44042F916DF6 Address: [27 194 149 55 199 113 220 162 195 101 238 129 179 155 68 4 47 145 109 246] Address (hex): 1BC29537C771DCA2C365EE81B39B44042F916DF6 Bech32 Acc: okp41r0pf2d78w8w29sm9a6qm8x6yqshezm0k6vwcrg Bech32 Val: okp4valoper1r0pf2d78w8w29sm9a6qm8x6yqshezm0k0t73af