signet.js 0.0.1-beta.7 → 0.0.1-beta.8
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/chains/Bitcoin/BTCRpcAdapter/BTCRpcAdapter.d.ts +10 -0
- package/chains/Bitcoin/BTCRpcAdapter/Mempool/Mempool.d.ts +15 -0
- package/chains/Bitcoin/BTCRpcAdapter/Mempool/index.d.ts +1 -0
- package/chains/Bitcoin/BTCRpcAdapter/Mempool/types.d.ts +69 -0
- package/chains/Bitcoin/BTCRpcAdapter/index.d.ts +5 -0
- package/chains/Bitcoin/Bitcoin.d.ts +69 -0
- package/chains/Bitcoin/types.d.ts +42 -0
- package/chains/Bitcoin/utils.d.ts +2 -0
- package/chains/Chain.d.ts +74 -0
- package/chains/ChainSignatureContract.d.ts +41 -0
- package/chains/Cosmos/Cosmos.d.ts +51 -0
- package/chains/Cosmos/types.d.ts +30 -0
- package/chains/Cosmos/utils.d.ts +2 -0
- package/chains/EVM/EVM.d.ts +42 -0
- package/chains/EVM/types.d.ts +5 -0
- package/chains/EVM/utils.d.ts +7 -0
- package/chains/index.d.ts +12 -0
- package/chains/types.d.ts +31 -0
- package/chains/utils.d.ts +12 -0
- package/index.cjs +2 -0
- package/index.cjs.map +1 -0
- package/index.d.ts +2 -0
- package/index.js +2674 -0
- package/index.js.map +1 -0
- package/package.json +2 -2
- package/utils/chains/index.d.ts +1 -0
- package/utils/chains/near/ChainSignatureContract.d.ts +38 -0
- package/utils/chains/near/account.d.ts +13 -0
- package/utils/chains/near/constants.d.ts +3 -0
- package/utils/chains/near/index.d.ts +3 -0
- package/utils/chains/near/relayer/index.d.ts +1 -0
- package/utils/chains/near/relayer/relayer.d.ts +8 -0
- package/utils/chains/near/relayer/types.d.ts +22 -0
- package/utils/chains/near/signAndSend/index.d.ts +1 -0
- package/utils/chains/near/signAndSend/keypair.d.ts +6 -0
- package/utils/chains/near/transactionBuilder.d.ts +26 -0
- package/utils/chains/near/types.d.ts +50 -0
- package/utils/index.d.ts +1 -0
- package/.eslintrc.json +0 -55
- package/.prettierrc +0 -1
- package/babel.config.js +0 -6
- package/docs/pages/chain-signatures-contract.mdx +0 -56
- package/docs/pages/chain.mdx +0 -45
- package/docs/pages/chains/bitcoin/bitcoin.mdx +0 -191
- package/docs/pages/chains/bitcoin/btc-rpc-adapter.mdx +0 -307
- package/docs/pages/chains/cosmos.mdx +0 -181
- package/docs/pages/chains/evm.mdx +0 -189
- package/docs/pages/index.mdx +0 -99
- package/docs/snippets/contract.ts +0 -21
- package/docs/snippets/env.ts +0 -13
- package/jest.config.ts +0 -199
- package/src/chains/Bitcoin/BTCRpcAdapter/BTCRpcAdapter.ts +0 -11
- package/src/chains/Bitcoin/BTCRpcAdapter/Mempool/Mempool.ts +0 -96
- package/src/chains/Bitcoin/BTCRpcAdapter/Mempool/index.ts +0 -1
- package/src/chains/Bitcoin/BTCRpcAdapter/Mempool/types.ts +0 -72
- package/src/chains/Bitcoin/BTCRpcAdapter/index.ts +0 -6
- package/src/chains/Bitcoin/Bitcoin.ts +0 -301
- package/src/chains/Bitcoin/types.ts +0 -45
- package/src/chains/Bitcoin/utils.ts +0 -14
- package/src/chains/Chain.ts +0 -96
- package/src/chains/ChainSignatureContract.ts +0 -48
- package/src/chains/Cosmos/Cosmos.ts +0 -279
- package/src/chains/Cosmos/types.ts +0 -35
- package/src/chains/Cosmos/utils.ts +0 -45
- package/src/chains/EVM/EVM.ts +0 -180
- package/src/chains/EVM/types.ts +0 -7
- package/src/chains/EVM/utils.ts +0 -26
- package/src/chains/index.ts +0 -34
- package/src/chains/types.ts +0 -35
- package/src/chains/utils.ts +0 -40
- package/src/index.ts +0 -2
- package/src/utils/chains/index.ts +0 -1
- package/src/utils/chains/near/ChainSignatureContract.ts +0 -195
- package/src/utils/chains/near/account.ts +0 -42
- package/src/utils/chains/near/constants.ts +0 -4
- package/src/utils/chains/near/index.ts +0 -3
- package/src/utils/chains/near/relayer/index.ts +0 -1
- package/src/utils/chains/near/relayer/relayer.ts +0 -39
- package/src/utils/chains/near/relayer/types.ts +0 -24
- package/src/utils/chains/near/signAndSend/index.ts +0 -1
- package/src/utils/chains/near/signAndSend/keypair.ts +0 -180
- package/src/utils/chains/near/transactionBuilder.ts +0 -138
- package/src/utils/chains/near/types.ts +0 -67
- package/src/utils/index.ts +0 -1
- package/tsconfig.eslint.json +0 -8
- package/tsconfig.json +0 -116
- package/vite.config.ts +0 -47
- package/vocs.config.ts +0 -60
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "signet.js",
|
|
3
|
-
"version": "0.0.1-beta.
|
|
3
|
+
"version": "0.0.1-beta.8",
|
|
4
4
|
"description": "",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"exports": {
|
|
@@ -22,7 +22,7 @@
|
|
|
22
22
|
"watch": "vite build --watch",
|
|
23
23
|
"pre:deploy": "npm run build && npm run docs:build",
|
|
24
24
|
"publish-npm": "npm run pre:deploy && npm publish",
|
|
25
|
-
"publish-npm:beta": "npm run pre:deploy && npm publish --tag beta",
|
|
25
|
+
"publish-npm:beta": "npm run pre:deploy && cd dist && npm publish --tag beta",
|
|
26
26
|
"test": "jest",
|
|
27
27
|
"docs:dev": "vocs dev",
|
|
28
28
|
"docs:build": "vocs build",
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export * as near from './near';
|
|
@@ -0,0 +1,38 @@
|
|
|
1
|
+
import { Account } from '@near-js/accounts';
|
|
2
|
+
import { KeyPair } from '@near-js/crypto';
|
|
3
|
+
import { default as BN } from 'bn.js';
|
|
4
|
+
import { ChainSignatureContract as AbstractChainSignatureContract, SignArgs } from '../../../chains/ChainSignatureContract';
|
|
5
|
+
import { RSVSignature, UncompressedPubKeySEC1 } from '../../../chains/types';
|
|
6
|
+
import { NearNetworkIds, ChainSignatureContractIds } from './types';
|
|
7
|
+
interface ChainSignatureContractArgs {
|
|
8
|
+
networkId: NearNetworkIds;
|
|
9
|
+
contractId: ChainSignatureContractIds;
|
|
10
|
+
accountId?: string;
|
|
11
|
+
keypair?: KeyPair;
|
|
12
|
+
}
|
|
13
|
+
/**
|
|
14
|
+
* This contract will default to view methods only.
|
|
15
|
+
* If you want to use the change methods, you need to provide an account and keypair.
|
|
16
|
+
*/
|
|
17
|
+
export declare class ChainSignatureContract extends AbstractChainSignatureContract {
|
|
18
|
+
private readonly networkId;
|
|
19
|
+
private readonly contractId;
|
|
20
|
+
private readonly accountId;
|
|
21
|
+
private readonly keypair;
|
|
22
|
+
constructor({ networkId, contractId, accountId, keypair, }: ChainSignatureContractArgs);
|
|
23
|
+
private getContract;
|
|
24
|
+
getCurrentSignatureDeposit(): Promise<BN>;
|
|
25
|
+
getDerivedPublicKey(args: {
|
|
26
|
+
path: string;
|
|
27
|
+
predecessor: string;
|
|
28
|
+
}): Promise<UncompressedPubKeySEC1>;
|
|
29
|
+
sign(args: SignArgs): Promise<RSVSignature>;
|
|
30
|
+
static signWithRelayer({ account, contract, signArgs, deposit, relayerUrl, }: {
|
|
31
|
+
account: Account;
|
|
32
|
+
contract: ChainSignatureContractIds;
|
|
33
|
+
signArgs: SignArgs;
|
|
34
|
+
deposit: BN;
|
|
35
|
+
relayerUrl: string;
|
|
36
|
+
}): Promise<RSVSignature>;
|
|
37
|
+
}
|
|
38
|
+
export {};
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
import { Account } from '@near-js/accounts';
|
|
2
|
+
import { KeyPair } from '@near-js/crypto';
|
|
3
|
+
type SetConnectionArgs = {
|
|
4
|
+
networkId: string;
|
|
5
|
+
accountId: string;
|
|
6
|
+
keypair: KeyPair;
|
|
7
|
+
} | {
|
|
8
|
+
networkId: string;
|
|
9
|
+
accountId?: never;
|
|
10
|
+
keypair?: never;
|
|
11
|
+
};
|
|
12
|
+
export declare const getNearAccount: ({ networkId, accountId, keypair, }: SetConnectionArgs) => Promise<Account>;
|
|
13
|
+
export {};
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export * from './relayer';
|
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
import { SignedDelegate } from '@near-js/transactions';
|
|
2
|
+
import { SignedDelegateRelayerFormat } from './types';
|
|
3
|
+
/**
|
|
4
|
+
* Parses the signedDelegate object from the Multi-Party Computation (MPC) format to the Relayer format.
|
|
5
|
+
* @param signedDelegate - The signedDelegate object in MPC format.
|
|
6
|
+
* @returns The signedDelegate object in Relayer format.
|
|
7
|
+
*/
|
|
8
|
+
export declare function parseSignedDelegateForRelayer(signedDelegate: SignedDelegate): SignedDelegateRelayerFormat;
|
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
interface FunctionCallRelayer {
|
|
2
|
+
FunctionCall: {
|
|
3
|
+
method_name: string;
|
|
4
|
+
args: string;
|
|
5
|
+
gas: number;
|
|
6
|
+
deposit: string;
|
|
7
|
+
};
|
|
8
|
+
}
|
|
9
|
+
type ActionsRelayer = FunctionCallRelayer;
|
|
10
|
+
interface DelegateActionRelayerFormat {
|
|
11
|
+
actions: ActionsRelayer[];
|
|
12
|
+
nonce: number;
|
|
13
|
+
max_block_height: number;
|
|
14
|
+
public_key: string;
|
|
15
|
+
receiver_id: string;
|
|
16
|
+
sender_id: string;
|
|
17
|
+
}
|
|
18
|
+
export interface SignedDelegateRelayerFormat {
|
|
19
|
+
delegate_action: DelegateActionRelayerFormat;
|
|
20
|
+
signature: string;
|
|
21
|
+
}
|
|
22
|
+
export {};
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export * as keypair from './keypair';
|
|
@@ -0,0 +1,6 @@
|
|
|
1
|
+
import { KeyPair } from '@near-js/crypto';
|
|
2
|
+
import { Response } from '../../../../chains/types';
|
|
3
|
+
import { BitcoinRequest, CosmosRequest, EVMRequest } from '../types';
|
|
4
|
+
export declare const EVMTransaction: (req: EVMRequest, keyPair: KeyPair) => Promise<Response>;
|
|
5
|
+
export declare const BTCTransaction: (req: BitcoinRequest, keyPair: KeyPair) => Promise<Response>;
|
|
6
|
+
export declare const CosmosTransaction: (req: CosmosRequest, keyPair: KeyPair) => Promise<Response>;
|
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
import { Action, FinalExecutionOutcome, NetworkId } from '@near-wallet-selector/core';
|
|
2
|
+
import { RSVSignature, KeyDerivationPath, MPCPayloads } from '../../../chains/types';
|
|
3
|
+
import { NFTKeysContracts, ChainSignatureContractIds } from './types';
|
|
4
|
+
export declare const mpcPayloadsToChainSigTransaction: ({ networkId, contractId, mpcPayloads, path, }: {
|
|
5
|
+
networkId: NetworkId;
|
|
6
|
+
contractId: ChainSignatureContractIds;
|
|
7
|
+
mpcPayloads: MPCPayloads;
|
|
8
|
+
path: KeyDerivationPath;
|
|
9
|
+
}) => Promise<{
|
|
10
|
+
receiverId: string;
|
|
11
|
+
actions: Action[];
|
|
12
|
+
}>;
|
|
13
|
+
export declare const mpcPayloadsToNFTKeysTransaction: ({ networkId, chainSigContract, nftKeysContract, mpcPayloads, path, tokenId, }: {
|
|
14
|
+
networkId: NetworkId;
|
|
15
|
+
chainSigContract: ChainSignatureContractIds;
|
|
16
|
+
nftKeysContract: NFTKeysContracts;
|
|
17
|
+
mpcPayloads: MPCPayloads;
|
|
18
|
+
path: KeyDerivationPath;
|
|
19
|
+
tokenId: string;
|
|
20
|
+
}) => Promise<{
|
|
21
|
+
receiverId: string;
|
|
22
|
+
actions: Action[];
|
|
23
|
+
}>;
|
|
24
|
+
export declare const responseToMpcSignature: ({ response, }: {
|
|
25
|
+
response: FinalExecutionOutcome;
|
|
26
|
+
}) => RSVSignature | undefined;
|
|
@@ -0,0 +1,50 @@
|
|
|
1
|
+
import { BTCTransactionRequest, BTCNetworkIds } from '../../../chains/Bitcoin/types';
|
|
2
|
+
import { CosmosNetworkIds, CosmosTransactionRequest } from '../../../chains/Cosmos/types';
|
|
3
|
+
import { EVMTransactionRequest } from '../../../chains/EVM/types';
|
|
4
|
+
import { KeyDerivationPath } from '../../../chains/types';
|
|
5
|
+
/**
|
|
6
|
+
Available ChainSignature contracts:
|
|
7
|
+
- Mainnet: v1.signer
|
|
8
|
+
- Testnet: v1.signer-prod.testnet
|
|
9
|
+
- Development (unstable): v1.signer-dev.testnet
|
|
10
|
+
*/
|
|
11
|
+
export type ChainSignatureContractIds = string;
|
|
12
|
+
export type NFTKeysContracts = string;
|
|
13
|
+
export type NearNetworkIds = 'mainnet' | 'testnet';
|
|
14
|
+
export interface ChainProvider {
|
|
15
|
+
providerUrl: string;
|
|
16
|
+
contract: ChainSignatureContractIds;
|
|
17
|
+
}
|
|
18
|
+
export interface NearAuthentication {
|
|
19
|
+
networkId: NearNetworkIds;
|
|
20
|
+
accountId: string;
|
|
21
|
+
}
|
|
22
|
+
export type EVMChainConfigWithProviders = ChainProvider;
|
|
23
|
+
export interface EVMRequest {
|
|
24
|
+
transaction: EVMTransactionRequest;
|
|
25
|
+
chainConfig: EVMChainConfigWithProviders;
|
|
26
|
+
nearAuthentication: NearAuthentication;
|
|
27
|
+
fastAuthRelayerUrl?: string;
|
|
28
|
+
derivationPath: KeyDerivationPath;
|
|
29
|
+
}
|
|
30
|
+
export type BTCChainConfigWithProviders = ChainProvider & {
|
|
31
|
+
network: BTCNetworkIds;
|
|
32
|
+
};
|
|
33
|
+
export interface BitcoinRequest {
|
|
34
|
+
transaction: BTCTransactionRequest;
|
|
35
|
+
chainConfig: BTCChainConfigWithProviders;
|
|
36
|
+
nearAuthentication: NearAuthentication;
|
|
37
|
+
fastAuthRelayerUrl?: string;
|
|
38
|
+
derivationPath: KeyDerivationPath;
|
|
39
|
+
}
|
|
40
|
+
export interface CosmosChainConfig {
|
|
41
|
+
contract: ChainSignatureContractIds;
|
|
42
|
+
chainId: CosmosNetworkIds;
|
|
43
|
+
}
|
|
44
|
+
export interface CosmosRequest {
|
|
45
|
+
chainConfig: CosmosChainConfig;
|
|
46
|
+
transaction: CosmosTransactionRequest;
|
|
47
|
+
nearAuthentication: NearAuthentication;
|
|
48
|
+
derivationPath: KeyDerivationPath;
|
|
49
|
+
fastAuthRelayerUrl?: string;
|
|
50
|
+
}
|
package/utils/index.d.ts
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export * as chains from './chains';
|
package/.eslintrc.json
DELETED
|
@@ -1,55 +0,0 @@
|
|
|
1
|
-
{
|
|
2
|
-
"env": {
|
|
3
|
-
"browser": true,
|
|
4
|
-
"es2021": true
|
|
5
|
-
},
|
|
6
|
-
"extends": [
|
|
7
|
-
"standard-with-typescript",
|
|
8
|
-
"plugin:import/typescript",
|
|
9
|
-
"plugin:prettier/recommended"
|
|
10
|
-
],
|
|
11
|
-
"parserOptions": {
|
|
12
|
-
"project": ["./tsconfig.json", "./tsconfig.eslint.json"]
|
|
13
|
-
},
|
|
14
|
-
"rules": {
|
|
15
|
-
"@typescript-eslint/strict-boolean-expressions": "off",
|
|
16
|
-
"@typescript-eslint/prefer-nullish-coalescing": "off",
|
|
17
|
-
"import/order": [
|
|
18
|
-
"error",
|
|
19
|
-
{
|
|
20
|
-
"groups": [
|
|
21
|
-
"builtin",
|
|
22
|
-
"external",
|
|
23
|
-
"internal",
|
|
24
|
-
"parent",
|
|
25
|
-
"sibling",
|
|
26
|
-
"index"
|
|
27
|
-
],
|
|
28
|
-
"pathGroups": [
|
|
29
|
-
{
|
|
30
|
-
"pattern": "@chains",
|
|
31
|
-
"group": "internal"
|
|
32
|
-
},
|
|
33
|
-
{
|
|
34
|
-
"pattern": "@chains/**",
|
|
35
|
-
"group": "internal"
|
|
36
|
-
},
|
|
37
|
-
{
|
|
38
|
-
"pattern": "@utils",
|
|
39
|
-
"group": "internal"
|
|
40
|
-
},
|
|
41
|
-
{
|
|
42
|
-
"pattern": "@utils/**",
|
|
43
|
-
"group": "internal"
|
|
44
|
-
}
|
|
45
|
-
],
|
|
46
|
-
"pathGroupsExcludedImportTypes": ["builtin"],
|
|
47
|
-
"newlines-between": "always",
|
|
48
|
-
"alphabetize": {
|
|
49
|
-
"order": "asc",
|
|
50
|
-
"caseInsensitive": true
|
|
51
|
-
}
|
|
52
|
-
}
|
|
53
|
-
]
|
|
54
|
-
}
|
|
55
|
-
}
|
package/.prettierrc
DELETED
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{ "singleQuote": true, "trailingComma": "es5", "semi": false }
|
package/babel.config.js
DELETED
|
@@ -1,56 +0,0 @@
|
|
|
1
|
-
# Implementing a Custom Chain Signature Contract
|
|
2
|
-
|
|
3
|
-
This guide explains how to implement a custom Chain Signature Contract to provide as argument when instantiating a Chain instance.
|
|
4
|
-
|
|
5
|
-
## Overview
|
|
6
|
-
|
|
7
|
-
The `ChainSignatureContract` is an abstract class that defines the interface for interacting with the Sig Network infrastructure.
|
|
8
|
-
|
|
9
|
-
- Key Derivation: Derive the child key from the root key using Sig Network key derivation function
|
|
10
|
-
- Sign: Sigs the given payload using Sig Network MPC
|
|
11
|
-
- Current Fee: Gets the current signature deposit that handles network congestion
|
|
12
|
-
|
|
13
|
-
While the library includes a default implementation for the NEAR protocol, you have the flexibility to implement your own contract for other blockchain networks.
|
|
14
|
-
|
|
15
|
-
## Using the NEAR Implementation
|
|
16
|
-
|
|
17
|
-
```ts twoslash
|
|
18
|
-
// [!include ~/snippets/env.ts]
|
|
19
|
-
// ---cut---
|
|
20
|
-
import { utils, EVM } from 'signet.js'
|
|
21
|
-
|
|
22
|
-
const contract = new utils.chains.near.ChainSignatureContract({
|
|
23
|
-
networkId: 'testnet',
|
|
24
|
-
contractId: 'v1.signer-prod.testnet',
|
|
25
|
-
accountId,
|
|
26
|
-
keypair,
|
|
27
|
-
})
|
|
28
|
-
|
|
29
|
-
const evmChain = new EVM({
|
|
30
|
-
rpcUrl: 'https://mainnet.infura.io/v3/YOUR-PROJECT-ID',
|
|
31
|
-
contract,
|
|
32
|
-
})
|
|
33
|
-
```
|
|
34
|
-
|
|
35
|
-
Currently the contracts are available on the following Near addresses:
|
|
36
|
-
|
|
37
|
-
- Mainnet:
|
|
38
|
-
- `v1.signer`
|
|
39
|
-
- Testnet:
|
|
40
|
-
- `v1.signer-prod.testnet`
|
|
41
|
-
|
|
42
|
-
## Implementing a Custom Chain Signature Contract
|
|
43
|
-
|
|
44
|
-
To create your own implementation, your contract must implement the `ChainSignatureContract` interface.
|
|
45
|
-
|
|
46
|
-
```ts twoslash
|
|
47
|
-
// [!include ~/../src/chains/ChainSignatureContract.ts]
|
|
48
|
-
```
|
|
49
|
-
|
|
50
|
-
### Example: NEAR Implementation
|
|
51
|
-
|
|
52
|
-
Below is the reference implementation for the NEAR, which you can use as a guide for implementing your own chain signature contract:
|
|
53
|
-
|
|
54
|
-
```ts twoslash
|
|
55
|
-
// [!include ~/../src/utils/chains/near/ChainSignatureContract.ts]
|
|
56
|
-
```
|
package/docs/pages/chain.mdx
DELETED
|
@@ -1,45 +0,0 @@
|
|
|
1
|
-
# Chain
|
|
2
|
-
|
|
3
|
-
Chain is the most basic interface of the signet.js library. It provides a standard set of methods that all chains must implement.
|
|
4
|
-
|
|
5
|
-
With this interface you will be able to interact with Sig Network smart contracts in a standard way to:
|
|
6
|
-
|
|
7
|
-
- getBalance
|
|
8
|
-
- deriveAddressAndPublicKey
|
|
9
|
-
- setTransaction
|
|
10
|
-
- getTransaction
|
|
11
|
-
- getMPCPayloadAndTransaction
|
|
12
|
-
- addSignature
|
|
13
|
-
- broadcastTx
|
|
14
|
-
|
|
15
|
-
If you wanna have a look this is the chain interface with a brief description of each method:
|
|
16
|
-
|
|
17
|
-
```ts twoslash
|
|
18
|
-
// [!include ~/../src/chains/Chain.ts]
|
|
19
|
-
```
|
|
20
|
-
|
|
21
|
-
# Implementing a New Chain
|
|
22
|
-
|
|
23
|
-
This guide explains how to implement support for a new blockchain network in the signet.js library.
|
|
24
|
-
|
|
25
|
-
## Overview
|
|
26
|
-
|
|
27
|
-
To add support for a new blockchain, you need to implement the `Chain` interface:
|
|
28
|
-
|
|
29
|
-
## Step-by-Step Guide
|
|
30
|
-
|
|
31
|
-
### 1. Create the Chain Types
|
|
32
|
-
|
|
33
|
-
First, define the transaction types for your chain:
|
|
34
|
-
|
|
35
|
-
```ts twoslash
|
|
36
|
-
// [!include ~/../src/chains/EVM/types.ts]
|
|
37
|
-
```
|
|
38
|
-
|
|
39
|
-
### 2. Implement the Chain Interface
|
|
40
|
-
|
|
41
|
-
Create a new class that implements the `Chain` interface:
|
|
42
|
-
|
|
43
|
-
```ts twoslash
|
|
44
|
-
// [!include ~/../src/chains/EVM/EVM.ts]
|
|
45
|
-
```
|
|
@@ -1,191 +0,0 @@
|
|
|
1
|
-
# Bitcoin Chain Implementation
|
|
2
|
-
|
|
3
|
-
The Bitcoin chain implementation supports both Bitcoin mainnet and testnet networks, with a focus on P2WPKH (Native SegWit) transactions.
|
|
4
|
-
|
|
5
|
-
## Configuration
|
|
6
|
-
|
|
7
|
-
```ts twoslash filename="base.ts"
|
|
8
|
-
// [!include ~/snippets/contract.ts]
|
|
9
|
-
// ---cut---
|
|
10
|
-
import { Bitcoin, BTCRpcAdapters } from 'signet.js'
|
|
11
|
-
|
|
12
|
-
// Initialize the RPC adapter using Mempool
|
|
13
|
-
const btcRpcAdapter = new BTCRpcAdapters.Mempool('https://mempool.space/api')
|
|
14
|
-
|
|
15
|
-
// Initialize the chain
|
|
16
|
-
const bitcoinChain = new Bitcoin({
|
|
17
|
-
network: 'testnet', // 'mainnet' | 'testnet' | 'regtest'
|
|
18
|
-
contract,
|
|
19
|
-
btcRpcAdapter,
|
|
20
|
-
})
|
|
21
|
-
```
|
|
22
|
-
|
|
23
|
-
## RPC Adapter
|
|
24
|
-
|
|
25
|
-
The RPC adapter is a class that provides an interface for interacting with the Bitcoin network. It handles essential operations such as:
|
|
26
|
-
|
|
27
|
-
- Fetching UTXOs (Unspent Transaction Outputs)
|
|
28
|
-
- Retrieving transaction details
|
|
29
|
-
- Getting current network fees
|
|
30
|
-
- Broadcasting transactions
|
|
31
|
-
- Querying address balances
|
|
32
|
-
|
|
33
|
-
The adapter abstracts away the complexity of different Bitcoin API providers, allowing you to easily switch between services like Mempool.space, BlockCypher, or your own Bitcoin node.
|
|
34
|
-
|
|
35
|
-
For detailed implementation and configuration options, see the [RPC Adapter](./btc-rpc-adapter) documentation.
|
|
36
|
-
|
|
37
|
-
## Methods
|
|
38
|
-
|
|
39
|
-
### getBalance
|
|
40
|
-
|
|
41
|
-
Gets the BTC balance of an address.
|
|
42
|
-
|
|
43
|
-
```ts twoslash
|
|
44
|
-
// [!include base.ts]
|
|
45
|
-
// ---cut---
|
|
46
|
-
const balance = await bitcoinChain.getBalance(
|
|
47
|
-
'bc1qxy2kgdygjrsqtzq2n0yrf2493p83kkfjhx0wlh'
|
|
48
|
-
)
|
|
49
|
-
```
|
|
50
|
-
|
|
51
|
-
**Parameters:**
|
|
52
|
-
|
|
53
|
-
- `address`: The Bitcoin address to check
|
|
54
|
-
|
|
55
|
-
**Returns:**
|
|
56
|
-
|
|
57
|
-
- The balance in BTC as a string
|
|
58
|
-
|
|
59
|
-
### deriveAddressAndPublicKey
|
|
60
|
-
|
|
61
|
-
Derives a Bitcoin address and public key for a given path.
|
|
62
|
-
|
|
63
|
-
```ts twoslash
|
|
64
|
-
// [!include base.ts]
|
|
65
|
-
// ---cut---
|
|
66
|
-
const { address, publicKey } = await bitcoinChain.deriveAddressAndPublicKey(
|
|
67
|
-
'predecessor_id',
|
|
68
|
-
'any_string'
|
|
69
|
-
)
|
|
70
|
-
```
|
|
71
|
-
|
|
72
|
-
**Parameters:**
|
|
73
|
-
|
|
74
|
-
- `predecessor`: The ID of the signer to derive from
|
|
75
|
-
- `path`: The derivation path to use
|
|
76
|
-
|
|
77
|
-
**Returns:**
|
|
78
|
-
|
|
79
|
-
- Object containing:
|
|
80
|
-
- `address`: The derived Bitcoin P2WPKH address
|
|
81
|
-
- `publicKey`: The corresponding compressed public key
|
|
82
|
-
|
|
83
|
-
### getMPCPayloadAndTransaction
|
|
84
|
-
|
|
85
|
-
Prepares a transaction for MPC signing.
|
|
86
|
-
|
|
87
|
-
```ts twoslash filename="unsigned-transaction.ts"
|
|
88
|
-
// [!include base.ts]
|
|
89
|
-
// ---cut---
|
|
90
|
-
import { BTCTransactionRequest } from 'signet.js'
|
|
91
|
-
|
|
92
|
-
// Simple transfer format
|
|
93
|
-
const transactionRequest: BTCTransactionRequest = {
|
|
94
|
-
from: 'bc1qxy2kgdygjrsqtzq2n0yrf2493p83kkfjhx0wlh',
|
|
95
|
-
to: 'bc1qw508d6qejxtdg4y5r3zarvary0c5xw7kv8f3t4',
|
|
96
|
-
value: '0.001', // 0.001 BTC
|
|
97
|
-
publicKey: '0235a3...',
|
|
98
|
-
}
|
|
99
|
-
|
|
100
|
-
const { transaction: unsignedTransaction, mpcPayloads } =
|
|
101
|
-
await bitcoinChain.getMPCPayloadAndTransaction(transactionRequest)
|
|
102
|
-
```
|
|
103
|
-
|
|
104
|
-
**Parameters:**
|
|
105
|
-
|
|
106
|
-
- `transactionRequest`: Either:
|
|
107
|
-
- Simple format:
|
|
108
|
-
```ts
|
|
109
|
-
{
|
|
110
|
-
from: string // Source address
|
|
111
|
-
to: string // Destination address
|
|
112
|
-
value: string // Amount in BTC
|
|
113
|
-
publicKey: string // Signer's public key
|
|
114
|
-
}
|
|
115
|
-
```
|
|
116
|
-
- Advanced format (manual UTXO selection):
|
|
117
|
-
```ts
|
|
118
|
-
{
|
|
119
|
-
inputs: BTCInput[] // Selected UTXOs
|
|
120
|
-
outputs: BTCOutput[] // Transaction outputs
|
|
121
|
-
publicKey: string // Signer's public key
|
|
122
|
-
}
|
|
123
|
-
```
|
|
124
|
-
|
|
125
|
-
**Returns:**
|
|
126
|
-
|
|
127
|
-
- Object containing:
|
|
128
|
-
- `transaction`: The unsigned transaction with PSBT
|
|
129
|
-
- `mpcPayloads`: Array of payloads to be signed
|
|
130
|
-
|
|
131
|
-
### addSignature
|
|
132
|
-
|
|
133
|
-
Adds signatures to a PSBT.
|
|
134
|
-
|
|
135
|
-
```ts twoslash filename="add-signature.ts"
|
|
136
|
-
// [!include unsigned-transaction.ts]
|
|
137
|
-
// ---cut---
|
|
138
|
-
import { RSVSignature } from 'signet.js'
|
|
139
|
-
|
|
140
|
-
// Ideally it would be a request to the Sig Network Smart Contract
|
|
141
|
-
const mpcSignatures: RSVSignature[] = [
|
|
142
|
-
{
|
|
143
|
-
r: '0x...',
|
|
144
|
-
s: '0x...',
|
|
145
|
-
v: 27,
|
|
146
|
-
},
|
|
147
|
-
]
|
|
148
|
-
|
|
149
|
-
const signedTx = bitcoinChain.addSignature({
|
|
150
|
-
transaction: unsignedTransaction,
|
|
151
|
-
mpcSignatures,
|
|
152
|
-
})
|
|
153
|
-
```
|
|
154
|
-
|
|
155
|
-
**Parameters:**
|
|
156
|
-
|
|
157
|
-
- `transaction`: The unsigned transaction with PSBT
|
|
158
|
-
- `mpcSignatures`: Array of RSV signatures from MPC
|
|
159
|
-
|
|
160
|
-
**Returns:**
|
|
161
|
-
|
|
162
|
-
- The serialized signed transaction in hex format
|
|
163
|
-
|
|
164
|
-
### broadcastTx
|
|
165
|
-
|
|
166
|
-
Broadcasts a signed transaction to the network.
|
|
167
|
-
|
|
168
|
-
```ts twoslash filename="broadcast-tx.ts"
|
|
169
|
-
// [!include add-signature.ts]
|
|
170
|
-
// ---cut---
|
|
171
|
-
const txid = await bitcoinChain.broadcastTx(signedTx)
|
|
172
|
-
```
|
|
173
|
-
|
|
174
|
-
**Parameters:**
|
|
175
|
-
|
|
176
|
-
- `txSerialized`: The serialized transaction in hex format
|
|
177
|
-
|
|
178
|
-
**Returns:**
|
|
179
|
-
|
|
180
|
-
- The transaction ID (txid)
|
|
181
|
-
|
|
182
|
-
## Technical Details
|
|
183
|
-
|
|
184
|
-
The implementation:
|
|
185
|
-
|
|
186
|
-
- Uses `bitcoinjs-lib` for transaction handling
|
|
187
|
-
- Supports P2WPKH (Native SegWit) addresses
|
|
188
|
-
- Handles automatic UTXO selection and change outputs
|
|
189
|
-
- Supports custom fee rates through the RPC adapter
|
|
190
|
-
- Uses PSBT (Partially Signed Bitcoin Transactions)
|
|
191
|
-
- Supports both mainnet and testnet networks
|