# Sigma Protocol

A digital signature scheme for signing Bitcoin transaction data.

<figure><img src="https://1096683769-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FNNpEU6Q6PipT0CXjqFvV%2Fuploads%2Fgit-blob-2fcd235abb705d9ddebfb7fe984983d622149e36%2FSIGMA.svg?alt=media" alt=""><figcaption></figcaption></figure>

{% hint style="info" %}
This repo contains the SIGMA protocol specification, and a node library for signing and verifying bsv-wasm transactions. See the [Library usage documentation](/sigma-library) for more information. These docs are best viewed via [GitBook](https://docs.sigmaidentity.com).
{% endhint %}

### Abstract

In an increasingly adversarial digital world, the importance of identity verification and secure transactions has become paramount. The Sigma Protocol is designed to enhance transaction security by signing custom output scripts in blockchain transactions. This paper proposes a protocol that addresses replay attack concerns by incorporating an input transaction ID (txid) and output data hashes into the signature process to strengthen identity verification in blockchain transactions.

### Introduction

As the digital landscape continues to evolve, establishing and maintaining trust in online transactions has become increasingly challenging. In response, the blockchain community has been developing solutions to tackle issues such as identity verification and transaction security. One such solution is the Sigma Protocol, which enables users to sign custom output scripts in blockchain transactions, asserting the authorship and integrity of the data. This paper presents the Sigma Protocol designed to cover custom output scripts and mitigate potential replay attacks.

### The Sigma Protocol

The Sigma Protocol is designed to sign custom output scripts by appending a few fields of data after the data being signed in the transaction output script. The protocol has the following structure:

```
<locking script>
OP_RETURN
  [Additional Data]
  |
  SIGMA
  [Signing Algorithm]
  [Signing Address]
  [Signature]
  [VIN]
```

Here's a brief explanation of the fields:

* `Additional Data`: The OP\_RETURN data you want to sign. Optional. If present, this library will add a protocol separator character "|" which is not signed. If absent, the library will add "OP\_RETURN" to the script, followed by the SIGMA protocol fields.
* `Signing Algorithm`: The algorithm used for signing. Supported algorithms:
  * `BSM` - Bitcoin Signed Message (default). Standard ECDSA message signing using SHA256 as the digest with magic prefix. Signatures can be verified by anyone who knows the signer's address.
  * `BRC77` - BRC-77 Message Signing. Uses derived child keys and embeds the signer's public key in the signature. Supports both public verification and private verification (where only a specific recipient can verify).
* `Signing Address`: The P2PKH address derived from the public key of the signer. If using Bitcoin Attestation Protocol to sign with an existing on-chain identity, this should be derived from your current signing key.
* `Signature`: The Sigma signature generated using the private key corresponding to the signing address. You will see the signature in hex format in Bitcoin scripts, but the library will return this field in Base64 format for the sake of consistency with other signing schemes.
* `VIN` : The input to reference by index. The txid of this input will be incorporated into the signature. If a -1 is specified, it indicates the corresponding input will be signed.


# Sigma Library

A digital signature scheme for signing Bitcoin transaction data.

{% hint style="success" %}
A Go version of this library is available at [github.com/bitcoinschema/go-sigma](https://github.com/bitcoinschema/go-sigma)
{% endhint %}

### Library Usage

To use the Sigma Protocol library, follow the instructions below:

1. Install the library and peer dependency using npm / yarn / bun:

```bash
bun add sigma-protocol @bsv/sdk
```

2. You can use the `verify` method to check a signature:

```javascript
import { Sigma } from "sigma-protocol";
```

3. Use the `sign` function to sign your data:

```javascript
const outputScriptAsm = `OP_0 OP_RETURN ${Buffer.from(
  "pushdata1",
  "utf-8"
).toString("hex")} ${Buffer.from("pushdata2", "utf-8").toString("hex")}`;

const script = Script.from_asm_string(outputScriptAsm);

const tx = new Transaction(1, 0);
const txOut = new TxOut(BigInt(0), script);
tx.add_output(txOut);

const sigma = new Sigma(tx);

const { signedTx } = sigma.sign(privateKey);
```

4. Use the `verify` method to verify the signature:

```javascript
const sigma = new Sigma(tx);

const isValid = sigma.verify()

console.log("Signature is valid:", isValid);
```

You can select a transaction output, and sigma instance to target. If you do not specify a target, the first output and first sigma instance will be assumed.

Here we target output index 1:

```javascript
const sigma = new Sigma(tx, 1);
```

Here we target output index 1, sigma instance 2:

```javascript
// this means there are 2 signatures on a single output
// this is typical when a user signs, and then a 
// platform signs covering the user signature
const sigma = new Sigma(tx, 1);
```

Once an instance is targeted, you can use verify like normal:

```javascript
  const isValid = sigma.verify()
  console.log("Signature is valid:", isValid);
```

If you sign a tx and the sign it a again the signature will be replaced. However, you can add additional signatures by incrementing the sigma instance number before signing.

```javascript
  const sigma = new Sigma(tx, 1, 2);
  sigma.sign(privateKey);
```

### Remote Signatures

This library supports using a remove key server such as starfish or tokenpass. use the `sigmaInstance.remoteSign(keyHost)` method to sign the message buffer using a remove key service instead of passing in a private key. This will take a second parameter, `authTokenHeader` object which will be forwarded as a header to the key server. This is useful if you need a signing server in the cloud.

```javascript
  const sigma = new Sigma(tx, 1, 2);
  sigma.remoteSign("http://localhost:21000", "authToken");
```

### Building the Library:

To build the Sigma Protocol library yourself, follow these steps:

1. Clone the repository:

```bash
git clone https://github.com/BitcoinSchema/sigma.git
```

2. Navigate to the project directory and install dependencies:

```bash
cd sigma
yarn
```

3. Build the library:

```bash
yarn build
```

The compiled JavaScript files will be output to the `./dist` directory.


