# Sign a transaction

In this tutorial, you will learn how to sign a transaction in FastSet.

Signing a transaction involves several steps:

#### 1. Construct the `Transaction` data type and serialize it.

Recall a [`Transaction`](/fast-archived/developers/fast-api/data-types.md#transaction) data type contains several fields. We construct each individually first, and we serialize the structure at the end.

In order to perform a transaction, we need a sender and a receiver and their addresses.

In FastSet, the public keys of the accounts are also their addresses. The private keys are, as their name says, private and they should be kept as such.

Let's assume the public address of the sender is `senderPublicKey` and the public address of the receiver is `recipientPublicKey` .

In FastSet we distinguish between addresses native to FastSet and addresses owned by external entities such as blockchains. In this example, we assume the recipient is native.

The address of the recipient is constructed from their public key:

```typescript
const Address = { FastSet: recipientPublicKey };
```

In FastSet, only the order of transactions originating from the same account is enforced. For this, every transaction carries a nonce denoting the transaction number.

The nonce on FastSet is similar to Ethereum's nonce.

```typescript
const Nonce = bcs.u64();
```

FastSet is more than settling payment transactions. In FastSet, you can settle what we call *claims*, independently verifiable statements. Claims include, but are not limited to voting, verifiable computing, or auctions. As such, the data type for claims `ClaimType` comes in different variants. In this example, we focus on a token transfer:

```typescript
const ClaimType = bcs.enum("ClaimType", {
    TokenTransfer: TokenTransfer,
});
```

```typescript
const Transaction = bcs.struct("Transaction", {
    sender: senderPublicKey,
    recipient: {FastSet: recipientPublicKey},
    nonce: Nonce,
    timestamp_nanos: bcs.u128(),
    claim: ClaimType,
});
```

#### 2. Sign the transaction

Before a transaction is sent, it has to be signed by the sender.

```typescript
const msgBytes = Transaction.toBytes();

// FastSet uses a prefix to identify the type of the message. For transactions, the prefix is always
// "Transaction::"
const prefix = new TextEncoder().encode("Transaction::");
// The data to sign is the prefix followed by the message (the serialized transaction)
const dataToSign = new Uint8Array(prefix.length + msgBytes.length);
dataToSign.set(prefix, 0);
dataToSign.set(msgBytes, prefix.length);
const signature = { Signature: ed.sign(dataToSign, senderPrivateKey) };
```


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://docs.fast.xyz/fast-archived/developers/tutorials/sign-a-transaction.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
