核心概念

请注意,SDK 没有导出签名函数,这些函数不是供开发者直接使用的。它们由 SDK 内部用于签署消息和交易。此处描述它们仅供参考。

FROST (灵活轮优化 Schnorr 阈值签名)

FROST 是一种阈值签名方案,允许一组签名者共同生成签名,而无需揭示他们的个人私钥。在 Spark 中,我们修改了 FROST 以拥有一个必需的参与者。

KeyPackage

KeyPackage 结构持有密钥、公钥和验证密钥。

SigningCommitment

SigningCommitment 结构是 FROST 协议的关键组件。它包含两个公钥:

  • hiding: 用于隐藏签名者临时随机数的公钥。
  • binding: 用于将承诺绑定到正在签名的消息的公钥。

SigningNonce

SigningNonce 结构用于签署消息,由两个私钥组成:

  • binding: 用于确保临时随机数对绑定密钥唯一的随机私钥。
  • hiding: 用于隐藏签名者临时随机数的随机私钥。

密钥操作

1. frost_nonce(key_package: KeyPackage): NonceResult

生成签名临时随机数和相关的签名承诺。

参数

  • key_package: 包含密钥、公钥和验证密钥的 KeyPackage

返回值

  • NonceResult: 包含生成的 SigningNonceSigningCommitment 的对象。

用法

import { KeyPackage } from "./wasm/spark_bindings.js";
import { frost_nonce } from "./utils/wasm.js";
const keyPackage = new KeyPackage(secretKey, publicKey, verifyingKey);
const { nonce, commitment } = frost_nonce(keyPackage);

2. signFrost(params: SignFrostParams): Uint8Array

使用 FROST 签名协议生成签名份额。

参数

  • SignFrostParams: 包含以下内容的对象:
    • msg: 要签名的消息(作为 Uint8Array)。
    • keyPackage: 用于派生签名密钥和验证密钥的密钥和公钥。
    • nonce: 签名临时随机数。
    • selfCommitment: 签名承诺。
    • statechainCommitments: 可选的将状态链 ID 映射到其各自签名承诺的对象。
    • adaptorPubKey: 可选的适配器公钥(作为 Uint8Array)。

返回值

  • Uint8Array: 生成的签名份额。

用法

import { KeyPackage } from "./wasm/spark_bindings.js";
import { SigningCommitment, SigningNonce } from "./signer/signer.js";
import { signFrost, createWasmSigningNonce, createWasmSigningCommitment } from "./utils/wasm.js";

// Example usage:
const msg: Uint8Array = new Uint8Array();
const keyPackage: KeyPackage = new KeyPackage();
const nonce: SigningNonce = { hiding: new Uint8Array(), binding: new Uint8Array() };
const selfCommitment: SigningCommitment = { hiding: new Uint8Array(), binding: new Uint8Array() };

const signatureShare = signFrost({
    msg,
    keyPackage,
    nonce: createWasmSigningNonce(nonce),
    selfCommitment: createWasmSigningCommitment(selfCommitment),
});

3. aggregateFrost(params: AggregateFrostParams): Uint8Array

将多个签名份额聚合成单个完整的签名。

参数

  • AggregateFrostParams: 包含以下内容的对象:
    • msg: 已签名的消息(作为 Uint8Array)。
    • statechainCommitments: 将状态链 ID 映射到其各自签名承诺的对象。
    • selfCommitment: 签名承诺。
    • statechainSignatures: 将状态链 ID 映射到其各自签名份额的对象。
    • selfSignature: 用户的签名份额。
    • statechainPublicKeys: 将状态链 ID 映射到其各自公钥的对象。
    • selfPublicKey: 用户的公钥。
    • verifyingKey: 验证密钥。
    • adaptorPubKey: 可选的适配器公钥(作为 Uint8Array)。

返回值

  • Uint8Array: 聚合的签名。

用法

import { SigningCommitment } from "./signer/signer.js";
import { aggregateFrost, createWasmSigningCommitment } from "./utils/wasm.js";

// Example usage:
const msg = new Uint8Array();
const statechainCommitments: { [key: string]: SigningCommitment } = {};
const selfCommitment: SigningCommitment = {
  hiding: new Uint8Array(),
  binding: new Uint8Array(),
};

const statechainSignatures: { [key: string]: Uint8Array } = {};
const selfSignature: Uint8Array = new Uint8Array();
const statechainPublicKeys: { [key: string]: Uint8Array } = {};
const selfPublicKey: Uint8Array = new Uint8Array();
const verifyingKey: Uint8Array = new Uint8Array();

const aggregatedSignature = aggregateFrost({
  msg,
  statechainSignatures,
  statechainPublicKeys,
  verifyingKey,
  statechainCommitments,
  selfCommitment: createWasmSigningCommitment(selfCommitment),
  selfSignature,
  selfPublicKey,
});

在 Spark SDK 中的使用

签名函数集成到 SparkWallet 类中,并由 TransferServiceDepositServiceLightningService 等各种服务内部使用。这些函数提供了 Spark 内部安全和私密交易所需的低级加密操作。