Socket
Socket
Sign inDemoInstall

babbage-scrypt-helpers

Package Overview
Dependencies
212
Maintainers
2
Versions
25
Alerts
File Explorer

Advanced tools

Install Socket

Detect and block malicious and high-risk dependencies

Install

    babbage-scrypt-helpers

Tools for deploying sCrypt contracts with Babbage SDK


Version published
Maintainers
2
Created

Readme

Source

babbage-scrypt-helpers

Tools for deploying sCrypt contracts with Babbage SDK

The code is hosted on GitHub and the package is available through NPM.

Installation

npm i babbage-scrypt-helpers

Example Usage

import { deployContract, listContracts, redeemContract } from 'babbage-scrypt-helpers'
import Demo from './src/contracts/Demo.ts'
import { sha256, byteString } from 'scrypt-ts'

// Compile and deploy the Demo contract
await Demo.compile()
const instance = new Demo(sha256(toByteString('hello world', true)))
const deployTX = await deployContract(
    instance, // Contract instance
    1000, // Number of satoshis
    'Deploy a smart contract', // Description
    'tests' // Basket where you want to keep the deployed UTXO
)
console.log('deployed', deployTX.txid)

// List the basket and provide a hydrator to transform the locking script into a contract
const contracts = await listContracts('tests', (lockingScript: string) => {
    return Demo.fromLockingScript(lockingScript) as Demo
})
console.log('listed', contracts)

// Redeem a contract
const redeemTX = await redeemContract(
    contracts[0], // Contract instance to redeem
    // A function that takes the contract and you call a public method to redeem it
    (self: SmartContract): void => {
        (self as Demo).unlock(toByteString('hello world', true))
    },
    'redeem a smart contract' // A description
)
console.log('REDEEMED!!', redeemTX.txid)

API

Links: API, Variables

Variables

deployContract
listContracts
redeemContract

Links: API, Variables


Variable: deployContract
deployContract = async (instance: SmartContract, satoshis: number, description: string, basket?: string, metadata?: string, acceptDelayedBroadcast = false): Promise<CreateActionResult> => {
    return await createAction({
        description,
        outputs: [
            {
                script: instance.lockingScript.toHex(),
                satoshis,
                basket,
                customInstructions: metadata,
            },
        ],
        acceptDelayedBroadcast
    });
}

Links: API, Variables


Variable: listContracts
listContracts = async <T extends SmartContract>(basket: string, contractHydrator: (lockingScript: string) => T): Promise<ListResult<T>[]> => {
    const outputs = await getTransactionOutputs({
        basket,
        spendable: true,
        includeEnvelope: true,
        includeCustomInstructions: true,
    });
    const contracts: ListResult<T>[] = [];
    for (let i = 0; i < outputs.length; i++) {
        contracts.push({
            ...outputs[i],
            contract: contractHydrator(outputs[i].outputScript),
        });
    }
    return contracts;
}

Links: API, Variables


Variable: redeemContract
redeemContract = async (listResult: ListResult<SmartContract>, redeemTransformer: (self: SmartContract) => void, description: string, customLockTime?: number, customSequenceNumber = 4294967295, outputs?: CreateActionOutput[]): Promise<CreateActionResult> => {
    return await createAction({
        inputs: {
            [listResult.txid]: {
                ...verifyTruthy(listResult.envelope),
                outputsToRedeem: [
                    {
                        index: listResult.vout,
                        unlockingScript: (await listResult.contract
                            .getUnlockingScript(redeemTransformer))
                            .toHex(),
                        sequenceNumber: customSequenceNumber
                    }
                ]
            }
        },
        description,
        lockTime: customLockTime,
        outputs,
    });
}

Links: API, Variables


License

The license for the code in this repository is the Open BSV License.

Keywords

FAQs

Last updated on 11 May 2024

Did you know?

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc