Skip to content
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(ts-client): add signer messages to stacks payloads #657

Merged
merged 4 commits into from
Oct 22, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 @@ -749,9 +749,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 @@ -152,6 +152,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';
7 changes: 6 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 Down Expand Up @@ -101,9 +102,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>;
Loading