Skip to content

Commit

Permalink
feat: update ts client to include signer messages
Browse files Browse the repository at this point in the history
  • Loading branch information
rafaelcr committed Oct 18, 2024
1 parent e44d84a commit bbc4c9d
Show file tree
Hide file tree
Showing 5 changed files with 129 additions and 14 deletions.
8 changes: 5 additions & 3 deletions components/chainhook-sdk/src/indexer/stacks/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -696,9 +696,11 @@ pub fn standardize_stacks_stackerdb_chunks(
block: standardize_stacks_nakamoto_block(&nakamoto_block),
})
}
SignerMessage::MockSignature(_) => StacksSignerMessage::MockSignature,
SignerMessage::MockProposal(_) => StacksSignerMessage::MockProposal,
SignerMessage::MockBlock(_) => StacksSignerMessage::MockBlock,
SignerMessage::MockSignature(_)
| SignerMessage::MockProposal(_)
| SignerMessage::MockBlock(_) => {
continue;
}
};
parsed_chunks.push(StacksStackerDbChunk {
contract: contract_id.clone(),
Expand Down
21 changes: 11 additions & 10 deletions components/chainhook-types-rs/src/signers.rs
Original file line number Diff line number Diff line change
Expand Up @@ -37,16 +37,17 @@ pub struct BlockAcceptedResponse {

#[derive(Debug, Clone, PartialEq, Deserialize, Serialize)]
pub enum BlockValidationFailedCode {
BadBlockHash = 0,
BadTransaction = 1,
InvalidBlock = 2,
ChainstateError = 3,
UnknownParent = 4,
NonCanonicalTenure = 5,
NoSuchTenure = 6,
BadBlockHash,
BadTransaction,
InvalidBlock,
ChainstateError,
UnknownParent,
NonCanonicalTenure,
NoSuchTenure,
}

#[derive(Debug, Clone, PartialEq, Deserialize, Serialize)]
#[serde(rename_all = "SCREAMING_SNAKE_CASE")]
pub enum BlockRejectReasonCode {
ValidationFailed(BlockValidationFailedCode),
ConnectivityIssues,
Expand All @@ -66,6 +67,7 @@ pub struct BlockRejectedResponse {
}

#[derive(Debug, Clone, PartialEq, Deserialize, Serialize)]
#[serde(tag = "type", content = "data")]
pub enum BlockResponseData {
Accepted(BlockAcceptedResponse),
Rejected(BlockRejectedResponse),
Expand All @@ -77,13 +79,12 @@ pub struct BlockPushedData {
}

#[derive(Debug, Clone, PartialEq, Deserialize, Serialize)]
#[serde(tag = "type", content = "data")]
pub enum StacksSignerMessage {
BlockProposal(BlockProposalData),
BlockResponse(BlockResponseData),
BlockPushed(BlockPushedData),
MockProposal,
MockSignature,
MockBlock,
// TODO(rafaelcr): Add mock messages
}

#[derive(Debug, Clone, PartialEq, Deserialize, Serialize)]
Expand Down
1 change: 1 addition & 0 deletions components/client/typescript/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@ export * from './schemas/payload';
export * from './schemas/predicate';
export * from './schemas/stacks/if_this';
export * from './schemas/stacks/payload';
export * from './schemas/stacks/signers';
export * from './schemas/stacks/tx_events';
export * from './schemas/stacks/tx_kind';
export * from './server';
9 changes: 8 additions & 1 deletion components/client/typescript/src/schemas/stacks/payload.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import {
import { StacksTransactionEventSchema } from './tx_events';
import { StacksTransactionKindSchema } from './tx_kind';
import { StacksIfThisSchema } from './if_this';
import { StacksSignerMessageEventSchema } from './signers';

export const StacksExecutionCostSchema = Type.Optional(
Type.Object({
Expand Down Expand Up @@ -52,7 +53,7 @@ export const StacksTransactionMetadataSchema = Type.Object({
});
export type StacksTransactionMetadata = Static<typeof StacksTransactionMetadataSchema>;

const StacksTransactionSchema = Type.Object({
export const StacksTransactionSchema = Type.Object({
transaction_identifier: TransactionIdentifierSchema,
operations: Type.Array(RosettaOperationSchema),
metadata: StacksTransactionMetadataSchema,
Expand All @@ -66,6 +67,8 @@ export const StacksEventMetadataSchema = Type.Object({
pox_cycle_length: Type.Integer(),
pox_cycle_position: Type.Integer(),
stacks_block_hash: Type.String(),
signer_bitvec: Type.String(),
signer_signatures: Type.Array(Type.String()),
});
export type StacksEventMetadata = Static<typeof StacksEventMetadataSchema>;

Expand All @@ -78,9 +81,13 @@ export const StacksEventSchema = Type.Object({
});
export type StacksEvent = Static<typeof StacksEventSchema>;

export const StacksNonConsensusEventSchema = Type.Union([StacksSignerMessageEventSchema]);
export type StacksNonConsensusEvent = Static<typeof StacksNonConsensusEventSchema>;

export const StacksPayloadSchema = Type.Object({
apply: Type.Array(StacksEventSchema),
rollback: Type.Array(StacksEventSchema),
events: Type.Array(StacksNonConsensusEventSchema),
chainhook: Type.Object({
uuid: Type.String(),
predicate: StacksIfThisSchema,
Expand Down
104 changes: 104 additions & 0 deletions components/client/typescript/src/schemas/stacks/signers.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,104 @@
import { Static, Type } from '@fastify/type-provider-typebox';
import { BlockIdentifierSchema } from '../common';
import { StacksTransactionSchema } from './payload';

export const StacksNakamotoBlockHeaderSchema = Type.Object({
version: Type.Integer(),
chain_length: Type.Integer(),
burn_spent: Type.Integer(),
consensus_hash: Type.String(),
parent_block_id: Type.String(),
tx_merkle_root: Type.String(),
state_index_root: Type.String(),
timestamp: Type.Integer(),
miner_signature: Type.String(),
signer_signature: Type.Array(Type.String()),
pox_treatment: Type.String(),
});
export type StacksNakamotoBlockHeader = Static<typeof StacksNakamotoBlockHeaderSchema>;

export const StacksNakamotoBlockSchema = Type.Object({
header: StacksNakamotoBlockHeaderSchema,
transactions: Type.Array(StacksTransactionSchema),
});
export type StacksNakamotoBlock = Static<typeof StacksNakamotoBlockSchema>;

export const StacksSignerMessageBlockProposalSchema = Type.Object({
type: Type.Literal('BlockProposal'),
data: Type.Object({
block: StacksNakamotoBlockSchema,
burn_height: Type.Integer(),
reward_cycle: Type.Integer(),
}),
});
export type StacksSignerMessageBlockProposal = Static<
typeof StacksSignerMessageBlockProposalSchema
>;

export const StacksSignerMessageBlockResponseAcceptedSchema = Type.Object({
type: Type.Literal('Accepted'),
data: Type.Object({
signer_signature_hash: Type.String(),
sig: Type.String(),
}),
});
export type StacksSignerMessageBlockResponseAccepted = Static<
typeof StacksSignerMessageBlockResponseAcceptedSchema
>;

export const StacksSignerMessageBlockResponseRejectedSchema = Type.Object({
type: Type.Literal('Rejected'),
data: Type.Object({
reason: Type.String(),
reason_code: Type.Union([
Type.Literal('VALIDATION_FAILED'),
Type.Literal('CONNECTIVITY_ISSUES'),
Type.Literal('REJECTED_IN_PRIOR_ROUND'),
Type.Literal('NO_SORTITION_VIEW'),
Type.Literal('SORTITION_VIEW_MISMATCH'),
Type.Literal('TESTING_DIRECTIVE'),
]),
signer_signature_hash: Type.String(),
chain_id: Type.Integer(),
signature: Type.String(),
}),
});
export type StacksSignerMessageBlockResponseRejected = Static<
typeof StacksSignerMessageBlockResponseRejectedSchema
>;

export const StacksSignerMessageBlockResponseSchema = Type.Object({
type: Type.Literal('BlockResponse'),
data: Type.Union([
StacksSignerMessageBlockResponseAcceptedSchema,
StacksSignerMessageBlockResponseRejectedSchema,
]),
});
export type StacksSignerMessageBlockResponse = Static<
typeof StacksSignerMessageBlockResponseSchema
>;

export const StacksSignerMessageBlockPushedSchema = Type.Object({
type: Type.Literal('BlockPushed'),
data: Type.Object({
block: StacksNakamotoBlockSchema,
}),
});
export type StacksSignerMessageBlockPushed = Static<typeof StacksSignerMessageBlockPushedSchema>;

export const StacksSignerMessageSchema = Type.Union([
StacksSignerMessageBlockProposalSchema,
StacksSignerMessageBlockResponseSchema,
StacksSignerMessageBlockPushedSchema,
]);
export type StacksSignerMessage = Static<typeof StacksSignerMessageSchema>;

export const StacksSignerMessageEventSchema = Type.Object({
contract: Type.String(),
sig: Type.String(),
pubkey: Type.String(),
message: StacksSignerMessageSchema,
received_at: Type.Integer(),
received_at_block: BlockIdentifierSchema,
});
export type StacksSignerMessageEvent = Static<typeof StacksSignerMessageEventSchema>;

0 comments on commit bbc4c9d

Please sign in to comment.