Skip to content

Commit

Permalink
fix: more types and docs for message
Browse files Browse the repository at this point in the history
  • Loading branch information
hugomrdias committed Jan 28, 2025
1 parent 105ba42 commit 0e70763
Show file tree
Hide file tree
Showing 4 changed files with 412 additions and 444 deletions.
2 changes: 1 addition & 1 deletion packages/iso-filecoin/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -148,7 +148,7 @@
"playwright-test": "^14.1.7",
"rimraf": "^6.0.1",
"rollup": "^4.30.1",
"type-fest": "^4.32.0",
"type-fest": "^4.33.0",
"typescript": "5.7.3"
},
"publishConfig": {
Expand Down
29 changes: 18 additions & 11 deletions packages/iso-filecoin/src/message.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,12 @@ import { z } from 'zod'
import * as Address from './address.js'
import { Token } from './token.js'

/**
* @typedef {z.infer<typeof MessageSchema>} MessageObj
*
* @typedef {import('type-fest').SetOptional<MessageObj, 'version' | 'nonce' | 'gasLimit' | 'gasFeeCap' | 'gasPremium' | 'method' | 'params'>} PartialMessageObj
*/

/**
* Validation schema for a message
*/
Expand Down Expand Up @@ -32,24 +38,17 @@ const MessageSchema = z.object({
params: z.string().default(''),
})

const MessageSchemaPartial = MessageSchema.partial({
version: true,
nonce: true,
gasLimit: true,
gasFeeCap: true,
gasPremium: true,
method: true,
params: true,
})
export const Schemas = {
message: MessageSchema,
messagePartial: MessageSchemaPartial,
}

/**
* Message class
*/
export class Message {
/**
*
* @param {import('./types').PartialMessageObj} msg
* @param {PartialMessageObj} msg
*/
constructor(msg) {
const _msg = MessageSchema.parse(msg)
Expand All @@ -65,6 +64,9 @@ export class Message {
this.params = _msg.params
}

/**
* Convert message to Lotus message
*/
toLotus() {
return {
Version: this.version,
Expand All @@ -81,6 +83,7 @@ export class Message {
}

/**
* Create message from Lotus message
*
* @param {import('./types').LotusMessage} json
*/
Expand All @@ -102,6 +105,7 @@ export class Message {
}

/**
* Prepare message for signing with nonce and gas estimation
*
* @param {import('./rpc.js').RPC} rpc
*/
Expand Down Expand Up @@ -133,6 +137,9 @@ export class Message {
return this
}

/**
* Serialize message using dag-cbor
*/
serialize() {
const msg = [
this.version,
Expand Down
20 changes: 19 additions & 1 deletion packages/iso-filecoin/src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,12 @@ import type { Driver } from 'iso-kv'
import type { JsonValue } from 'type-fest'
import type { z } from 'zod'
import type { AddressId, PROTOCOL_INDICATOR } from './address'
import type { Schemas as MessageSchemas } from './message'
import type { MessageObj, PartialMessageObj } from './message.js'
import type { RPC } from './rpc'
import type { SIGNATURE_TYPE, Schemas as SignatureSchemas } from './signature'

export type { MaybeResult } from 'iso-web/types'
export * from './message.js'

export type ProtocolIndicator = typeof PROTOCOL_INDICATOR
export type ProtocolIndicatorCode = ProtocolIndicator[keyof ProtocolIndicator]
Expand All @@ -29,6 +30,23 @@ export interface AddressRpcSafetyOptions extends AddressRpcOptions {
safety?: Safety
}

/**
* Lotus message
*/
export interface LotusMessage {
Version: 0
To: string
From: string
Nonce: number
Value: string
GasLimit: number
GasFeeCap: string
GasPremium: string
Method: number
Params: string
CID?: CID
}

export interface Address {
protocol: ProtocolIndicatorCode
payload: Uint8Array
Expand Down
Loading

0 comments on commit 0e70763

Please sign in to comment.