Skip to content

Instantly share code, notes, and snippets.

@zhfnjust
Created July 26, 2023 13:19
Show Gist options
  • Save zhfnjust/5a8b435b93b61475106b237e79cbcf87 to your computer and use it in GitHub Desktop.
Save zhfnjust/5a8b435b93b61475106b237e79cbcf87 to your computer and use it in GitHub Desktop.
import { Test } from './src/contracts/aaaa'
import {
bsv,
TestWallet,
DefaultProvider,
sha256,
toByteString,
toHex,
PubKey,
findSig,
SignatureResponse,
ByteString,
} from 'scrypt-ts'
import { RabinSig, RabinPubKey, RabinVerifierWOC } from 'scrypt-ts-lib'
import { generatePrivKey, privKeyToPubKey, sign, verify } from 'rabinsig'
import * as dotenv from 'dotenv'
// Load the .env file
dotenv.config()
// Read the private key from the .env file.
// The default private key inside the .env file is meant to be used for the Bitcoin testnet.
// See https://scrypt.io/docs/bitcoin-basics/bsv/#private-keys
const privateKey = bsv.PrivateKey.fromWIF(process.env.PRIVATE_KEY || '')
// Prepare signer.
// See https://scrypt.io/docs/how-to-deploy-and-call-a-contract/#prepare-a-signer-and-provider
const signer = new TestWallet(
privateKey,
new DefaultProvider({
network: bsv.Networks.testnet,
})
)
async function main() {
await Test.compile()
// TODO: Adjust the amount of satoshis locked in the smart contract:
const amount = 1
const key = {
p: 2814015297002173515911792099514050754903060894643230059745767270555014452711294029953724549765934365804606426418393172072563368169125091317706017909563n,
q: 650047001204168007801848889418948532353073326909497585177081016045346562912146630794965372241635285465610094863279373295872825824127728241709483771067n
}
console.log('key', key)
const nRabin = privKeyToPubKey(key.p, key.q)
console.log('nRabin', nRabin)
const instance = new Test(
PubKey(toHex(privateKey.publicKey)), 1n, nRabin
)
// Connect to a signer.
await instance.connect(signer)
// Contract deployment.
const deployTx = await instance.deploy(amount)
console.log(`Aaaa contract deployed: ${deployTx.id}`)
const msg: ByteString = toByteString('00112233445566778899aabbccddeeff')
const signRes = sign(msg, key.p, key.q, nRabin)
let paddingBytes: ByteString = ''
for (let i = 0; i < signRes.paddingByteCount; i++) {
paddingBytes += '00'
}
const sig: RabinSig = {
s: signRes.signature,
padding: paddingBytes,
}
const ok = verify(msg, signRes.paddingByteCount, signRes.signature, nRabin)
console.log('ok', ok)
const {tx: callTx} = await instance.methods.lockStep1((sigResponses: SignatureResponse[]) => {
return findSig(sigResponses, privateKey.publicKey)
}, msg, sig)
console.log(`Aaaa contract called: ${callTx.id}`)
}
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment