Skip to content

Commit

Permalink
wip: feat(namada): decoding transactions from rust/wasm, pt.12
Browse files Browse the repository at this point in the history
  • Loading branch information
egasimus committed Mar 13, 2024
1 parent b3e79d8 commit 7754f24
Show file tree
Hide file tree
Showing 8 changed files with 949 additions and 13 deletions.
4 changes: 4 additions & 0 deletions packages/namada/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
!pkg
!pkg/*.js
!pkg/*.d.ts
pkg/*.wasm
12 changes: 7 additions & 5 deletions packages/namada/namada-connection.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,8 @@ import {
isPGFSteward
} from "./namada-pgf"
import {
NamadaTransaction
NamadaTransaction,
UndecodedNamadaTransaction
} from './namada-tx'

export async function connect (optionsWithDecoder: ConstructorParameters<typeof NamadaConnection>[0] & {
Expand All @@ -53,11 +54,12 @@ export class NamadaConnection extends CW.Connection {
const txsDecoded: NamadaTransaction[] = []
const {txs} = (block as { txs: Uint8Array[] })
for (const i in txs) {
const decoded = Decode.tx(txs[i].slice(3)) as any
if (decoded.txType === 'Wrapper') {
console.log('decoded:', decoded)
const binary = txs[i].slice(3)
try {
txsDecoded[i] = NamadaTransaction.fromDecoded(Decode.tx(binary) as any)
} catch (error) {
txsDecoded[i] = new UndecodedNamadaTransaction({ binary, error })
}
txsDecoded[i] = NamadaTransaction.fromDecoded(decoded)
}
Object.assign(block, { txsDecoded })
return block as typeof block & { txsDecoded: NamadaTransaction[] }
Expand Down
16 changes: 16 additions & 0 deletions packages/namada/namada-tx.ts
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,22 @@ export class NamadaTransaction {
}
}

export class UndecodedNamadaTransaction extends NamadaTransaction {
binary: Uint8Array
error: Error
constructor ({ binary, error }) {
super()
this.binary = binary
this.error = error
}
print (console = new Core.Console()) {
console.warn(`Failed to decode a transaction: ${this.error.message||'(unknown error)'}`)
}
printSections (console = new Core.Console()) {
console.warn(`Failed to decode a transaction: ${this.error.message||'(unknown error)'}`)
}
}

export class NamadaRawTransaction extends NamadaTransaction {
//txType = 'Raw' as 'Raw'
//constructor (header: object, details: object, sections: object[]) {
Expand Down
4 changes: 2 additions & 2 deletions packages/namada/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,8 @@
"scripts": {
"check": "time tsc --noEmit",
"clean": "rm -rf .ubik* *.dist.*",
"build:wasm:dev": "wasm-pack build --dev --target web && rm pkg/package.json",
"build:wasm:prod": "wasm-pack build --release --target web && rm pkg/package.json",
"build:wasm:dev": "wasm-pack build --dev --target web && rm -v pkg/package.json pkg/.gitignore",
"build:wasm:prod": "wasm-pack build --release --target web && rm -v pkg/package.json pkg/.gitignore",

"release": "time sh -c 'pnpm clean && pnpm i && pnpm check && pnpm cov all && ubik release --otp 123123'",
"release:fast": "time sh -c 'pnpm clean && pnpm i && pnpm check && ubik release --otp 123123'",
Expand Down
164 changes: 164 additions & 0 deletions packages/namada/pkg/fadroma_namada.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,164 @@
/* tslint:disable */
/* eslint-disable */
/**
*/
export class Decode {
free(): void;
/**
* @param {Uint8Array} source
* @returns {string}
*/
static address(source: Uint8Array): string;
/**
* @param {Uint8Array} source
* @returns {object}
*/
static tx(source: Uint8Array): object;
/**
* @param {Uint8Array} binary
* @returns {object}
*/
static tx_content_become_validator(binary: Uint8Array): object;
/**
* @param {Uint8Array} binary
* @returns {object}
*/
static tx_content_bond(binary: Uint8Array): object;
/**
* @param {Uint8Array} binary
* @returns {object}
*/
static tx_content_change_consensus_key(binary: Uint8Array): object;
/**
* @param {Uint8Array} binary
* @returns {object}
*/
static tx_content_change_validator_commission(binary: Uint8Array): object;
/**
* @param {Uint8Array} binary
* @returns {object}
*/
static tx_content_change_validator_metadata(binary: Uint8Array): object;
/**
* @param {Uint8Array} binary
* @returns {object}
*/
static tx_content_claim_rewards(binary: Uint8Array): object;
/**
* @param {Uint8Array} binary
* @returns {object}
*/
static tx_content_deactivate_validator(binary: Uint8Array): object;
/**
* @param {Uint8Array} binary
* @returns {object}
*/
static tx_content_init_account(binary: Uint8Array): object;
/**
* @param {Uint8Array} binary
* @returns {object}
*/
static tx_content_init_proposal(binary: Uint8Array): object;
/**
* @param {Uint8Array} binary
* @returns {object}
*/
static tx_content_reactivate_validator(binary: Uint8Array): object;
/**
* @param {Uint8Array} binary
* @returns {object}
*/
static tx_content_resign_steward(binary: Uint8Array): object;
/**
* @param {Uint8Array} binary
* @returns {object}
*/
static tx_content_reveal_pk(binary: Uint8Array): object;
/**
* @param {Uint8Array} binary
* @returns {object}
*/
static tx_content_transfer(binary: Uint8Array): object;
/**
* @param {Uint8Array} binary
* @returns {object}
*/
static tx_content_unbond(binary: Uint8Array): object;
/**
* @param {Uint8Array} binary
* @returns {object}
*/
static tx_content_unjail_validator(binary: Uint8Array): object;
/**
* @param {Uint8Array} binary
* @returns {object}
*/
static tx_content_update_account(binary: Uint8Array): object;
/**
* @param {Uint8Array} binary
* @returns {object}
*/
static tx_content_update_steward_commission(binary: Uint8Array): object;
/**
* @param {Uint8Array} binary
* @returns {object}
*/
static tx_content_vote_proposal(binary: Uint8Array): object;
/**
* @param {Uint8Array} binary
* @returns {object}
*/
static tx_content_withdraw(binary: Uint8Array): object;
}

export type InitInput = RequestInfo | URL | Response | BufferSource | WebAssembly.Module;

export interface InitOutput {
readonly memory: WebAssembly.Memory;
readonly __wbg_decode_free: (a: number) => void;
readonly decode_address: (a: number, b: number) => void;
readonly decode_tx: (a: number, b: number) => void;
readonly decode_tx_content_become_validator: (a: number, b: number, c: number) => void;
readonly decode_tx_content_bond: (a: number, b: number, c: number) => void;
readonly decode_tx_content_change_consensus_key: (a: number, b: number, c: number) => void;
readonly decode_tx_content_change_validator_commission: (a: number, b: number, c: number) => void;
readonly decode_tx_content_change_validator_metadata: (a: number, b: number, c: number) => void;
readonly decode_tx_content_claim_rewards: (a: number, b: number, c: number) => void;
readonly decode_tx_content_deactivate_validator: (a: number, b: number, c: number) => void;
readonly decode_tx_content_init_account: (a: number, b: number, c: number) => void;
readonly decode_tx_content_init_proposal: (a: number, b: number, c: number) => void;
readonly decode_tx_content_reactivate_validator: (a: number, b: number, c: number) => void;
readonly decode_tx_content_reveal_pk: (a: number, b: number, c: number) => void;
readonly decode_tx_content_transfer: (a: number, b: number, c: number) => void;
readonly decode_tx_content_update_account: (a: number, b: number, c: number) => void;
readonly decode_tx_content_update_steward_commission: (a: number, b: number, c: number) => void;
readonly decode_tx_content_vote_proposal: (a: number, b: number, c: number) => void;
readonly decode_tx_content_withdraw: (a: number, b: number, c: number) => void;
readonly decode_tx_content_resign_steward: (a: number, b: number, c: number) => void;
readonly decode_tx_content_unjail_validator: (a: number, b: number, c: number) => void;
readonly decode_tx_content_unbond: (a: number, b: number, c: number) => void;
readonly __wbindgen_add_to_stack_pointer: (a: number) => number;
readonly __wbindgen_malloc: (a: number, b: number) => number;
readonly __wbindgen_exn_store: (a: number) => void;
}

export type SyncInitInput = BufferSource | WebAssembly.Module;
/**
* Instantiates the given `module`, which can either be bytes or
* a precompiled `WebAssembly.Module`.
*
* @param {SyncInitInput} module
*
* @returns {InitOutput}
*/
export function initSync(module: SyncInitInput): InitOutput;

/**
* If `module_or_path` is {RequestInfo} or {URL}, makes a request and
* for everything else, calls `WebAssembly.instantiate` directly.
*
* @param {InitInput | Promise<InitInput>} module_or_path
*
* @returns {Promise<InitOutput>}
*/
export default function __wbg_init (module_or_path: InitInput | Promise<InitInput>): Promise<InitOutput>;
Loading

0 comments on commit 7754f24

Please sign in to comment.