Skip to content
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
3 changes: 0 additions & 3 deletions .github/workflows/typedoc-generator.yml
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,6 @@ on:
push:
branches:
- main
pull_request:
branches:
- main
workflow_dispatch:

env:
Expand Down
2 changes: 1 addition & 1 deletion __tests__/bitcoin/Integration.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -151,7 +151,7 @@ describe('Bitcoin MPC Integration', () => {
keyType: 'Ecdsa',
signerAccount: {
accountId: 'test-account',
signAndSendTransactions: async () => ({}),
signAndSendTransactions: async () => [],
},
})
)
Expand Down
3 changes: 2 additions & 1 deletion __tests__/contracts/signAndSend/keypair.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,11 @@ import * as chainAdapters from '@chain-adapters'
import type { BTCTransactionRequest } from '@chain-adapters/Bitcoin/types'
import type { CosmosTransactionRequest } from '@chain-adapters/Cosmos/types'
import type { EVMTransactionRequest } from '@chain-adapters/EVM/types'
import { getNearAccount } from '@contracts/account'
import { ChainSignatureContract } from '@contracts/ChainSignatureContract'
import * as keypair from '@contracts/signAndSend/keypair'

import { getNearAccount } from '../../../src/contracts/signAndSend/utils'

// Mock dependencies
vi.mock('@contracts/account')
vi.mock('@contracts/ChainSignatureContract')
Expand Down
2 changes: 1 addition & 1 deletion __tests__/solana/Integration.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -164,7 +164,7 @@ describe('Solana MPC Integration', () => {
keyType: 'Eddsa',
signerAccount: {
accountId: 'test-account',
signAndSendTransactions: async () => ({}),
signAndSendTransactions: async () => [],
},
})
)
Expand Down
2 changes: 1 addition & 1 deletion __tests__/xrp/Integration.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -172,7 +172,7 @@ describe('XRP MPC Integration', () => {
keyType: 'Ecdsa', // Changed from 'Secp256k1' to 'Ecdsa'
signerAccount: {
accountId: 'test-account',
signAndSendTransactions: async () => ({}),
signAndSendTransactions: async () => [],
},
})
)
Expand Down
174 changes: 76 additions & 98 deletions examples/send-apt.ts
Original file line number Diff line number Diff line change
@@ -1,117 +1,95 @@
import { KeyPairString, KeyPair } from '@near-js/crypto'
import { contracts, chainAdapters } from '../src/index'
import { KeyPairSigner } from '@near-js/signers'
import { JsonRpcProvider } from '@near-js/providers'
import { Account } from '@near-js/accounts'
import { Aptos, AptosConfig, Network } from '@aptos-labs/ts-sdk'
import { createAction } from '@near-wallet-selector/wallet-utils'
import { getTransactionLastResult } from '@near-js/utils'
import { Account } from '@near-js/accounts'
import { type KeyPairString, KeyPair } from '@near-js/crypto'
import { JsonRpcProvider } from '@near-js/providers'
import { KeyPairSigner } from '@near-js/signers'
import { contracts, chainAdapters } from 'chainsig.js'
import { config } from 'dotenv'

// Load environment variables
config()
config() // Load environment variables

async function main(): Promise<void> {
const accountId = process.env.ACCOUNT_ID // 'your-account.testnet'
const privateKey = process.env.PRIVATE_KEY as KeyPairString // ed25519:3D4YudUahN...

if (!accountId) throw new Error('Setup environmental variables')

async function main() {
// Create an account object from environment variables
const accountId = process.env.ACCOUNT_ID || 'your-account.testnet'
// Create a signer from a private key string
const privateKey = (process.env.PRIVATE_KEY || 'ed25519:3D4YudUahN1HMqD5VvhE6RdcjbJGgMvRpMYhtKZhKVGG5FNFMRik2bLBmXvSjSznKvJLhxpxehVLrDLpFAqbsciH') as KeyPairString
const keyPair = KeyPair.fromString(privateKey)
const signer = new KeyPairSigner(keyPair)

const provider = new JsonRpcProvider({
url: 'https://test.rpc.fastnear.com',
})
const provider = new JsonRpcProvider({
url: 'https://test.rpc.fastnear.com',
})

// Use Account constructor (accountId, provider, signer)
const account = new Account(accountId, provider, signer)

const contract = new contracts.ChainSignatureContract({
networkId: 'testnet',
contractId: process.env.NEXT_PUBLIC_NEAR_CHAIN_SIGNATURE_CONTRACT || 'v1.signer-prod.testnet',
})
const contract = new contracts.ChainSignatureContract({
networkId: 'testnet',
contractId:
process.env.NEXT_PUBLIC_NEAR_CHAIN_SIGNATURE_CONTRACT ||
'v1.signer-prod.testnet',
})

const aptosClient = new Aptos(
new AptosConfig({
network: Network.TESTNET,
})
)

const aptosClient = new Aptos(
new AptosConfig({
network: Network.TESTNET,
const derivationPath = 'any_string'

const aptosChain = new chainAdapters.aptos.Aptos({
client: aptosClient,
contract,
})
)

const derivationPath = 'any_string'

const aptosChain = new chainAdapters.aptos.Aptos({
client: aptosClient,
contract,
})

const { address, publicKey } = await aptosChain.deriveAddressAndPublicKey(
accountId,
derivationPath
)

console.log('address', address)

// Check balance
const { balance, decimals } = await aptosChain.getBalance(address)

console.log('balance', balance)

const transaction = await aptosClient.transaction.build.simple({
sender: address,
data: {
function: '0x1::aptos_account::transfer',
functionArguments: [
// USDC address
'0x7257adc3ae461378c2a3359933ecf35f316247dc2e163031313e57a638ecf0f4',
'100',
],
},
})

const { hashesToSign } =
await aptosChain.prepareTransactionForSigning(transaction)

// Sign with MPC
const signature = await contract.sign({
payloads: hashesToSign,
path: derivationPath,
keyType: 'Eddsa',
signerAccount: {
accountId: account.accountId,
signAndSendTransactions: async ({
transactions: walletSelectorTransactions,
}) => {
const results = []

for (const tx of walletSelectorTransactions) {
const actions = tx.actions.map((a: any) => createAction(a))

const result = await account.signAndSendTransaction({
receiverId: tx.receiverId,
actions,
})

// @ts-ignore - Type mismatch between @near-js package versions
results.push(getTransactionLastResult(result))
}

return results

const { address, publicKey } = await aptosChain.deriveAddressAndPublicKey(
accountId,
derivationPath
)

console.log('address', address)

// Check balance
const { balance } = await aptosChain.getBalance(address)

console.log('balance', balance)

const transaction = await aptosClient.transaction.build.simple({
sender: address,
data: {
function: '0x1::aptos_account::transfer',
functionArguments: [
// USDC address
'0x7257adc3ae461378c2a3359933ecf35f316247dc2e163031313e57a638ecf0f4',
'100',
],
},
},
})
})

const { hashesToSign } =
await aptosChain.prepareTransactionForSigning(transaction)

// The signature is already in the correct format for Ed25519
console.log('Raw signature:', signature[0])
const aptosSignature = signature[0] as any
// Sign with MPC
const signature = await contract.sign({
payloads: hashesToSign,
path: derivationPath,
keyType: 'Eddsa',
signerAccount: account,
})

// Add signature
const signedTx = aptosChain.finalizeTransactionSigning({
transaction,
rsvSignatures: aptosSignature,
publicKey: publicKey,
})
// The signature is already in the correct format for Ed25519
console.log('Raw signature:', signature[0])
const aptosSignature = signature[0] as any

// Add signature
const signedTx = aptosChain.finalizeTransactionSigning({
transaction,
rsvSignatures: aptosSignature,
publicKey,
})

const { hash: txHash } = await aptosChain.broadcastTx(signedTx)
const { hash: txHash } = await aptosChain.broadcastTx(signedTx)

console.log(`https://explorer.aptoslabs.com/txn/${txHash}?network=testnet`)
}
Expand Down
69 changes: 19 additions & 50 deletions examples/send-btc.ts
Original file line number Diff line number Diff line change
@@ -1,41 +1,32 @@
import { InMemoryKeyStore } from '@near-js/keystores'
import { KeyPair } from '@near-js/crypto'
import { connect } from 'near-api-js'
import { getTransactionLastResult } from '@near-js/utils'
import { Action } from '@near-js/transactions'
import { Account } from '@near-js/accounts'
import { KeyPair, type KeyPairString } from '@near-js/crypto'
import { JsonRpcProvider } from '@near-js/providers'
import { KeyPairSigner } from '@near-js/signers'
import { contracts, chainAdapters } from 'chainsig.js'
import { createAction } from '@near-wallet-selector/wallet-utils'
import { config } from 'dotenv'

import dotenv from 'dotenv'
import { KeyPairString } from '@near-js/crypto'
config() // Load environment variables

async function main(): Promise<void> {
const accountId = process.env.ACCOUNT_ID // 'your-account.testnet'
const privateKey = process.env.PRIVATE_KEY as KeyPairString // ed25519:3D4YudUahN...

async function main() {
// Load environment variables
dotenv.config({ path: '.env' }) // Path relative to the working directory
if (!accountId) throw new Error('Setup environmental variables')

// Create an account object
const accountId = process.env.ACCOUNT_ID!
// Create a signer from a private key string
const privateKey = process.env.PRIVATE_KEY as KeyPairString
const keyPair = KeyPair.fromString(privateKey) // ed25519:5Fg2...
const keyPair = KeyPair.fromString(privateKey)
const signer = new KeyPairSigner(keyPair)

// Create a keystore and add the key
const keyStore = new InMemoryKeyStore()
await keyStore.setKey('testnet', accountId, keyPair)

// Create a connection to testnet
const near = await connect({
networkId: 'testnet',
keyStore: keyStore as any,
nodeUrl: 'https://test.rpc.fastnear.com',
const provider = new JsonRpcProvider({
url: 'https://test.rpc.fastnear.com',
})

const account = await near.account(accountId)
const account = new Account(accountId, provider, signer)

const contract = new contracts.ChainSignatureContract({
networkId: 'testnet',
contractId: 'v1.signer-prod.testnet',
contractId:
process.env.NEXT_PUBLIC_NEAR_CHAIN_SIGNATURE_CONTRACT ||
'v1.signer-prod.testnet',
})

const derivationPath = 'any_string'
Expand Down Expand Up @@ -77,29 +68,7 @@ async function main() {
payloads: hashesToSign,
path: derivationPath,
keyType: 'Ecdsa',
signerAccount: {
accountId: account.accountId,
signAndSendTransactions: async ({
transactions: walletSelectorTransactions,
}) => {
const transactions = walletSelectorTransactions.map((tx) => {
return {
receiverId: tx.receiverId,
actions: tx.actions.map((a) => createAction(a)),
} satisfies { receiverId: string; actions: Action[] }
})

const txs: any[] = []
for (const transaction of transactions) {
const tx = await account.signAndSendTransaction(transaction)
txs.push(tx)
}

console.dir(txs, { depth: Infinity })

return txs.map((tx) => getTransactionLastResult(tx))
},
},
signerAccount: account,
})

// Add signature
Expand Down
Loading