Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Rename bsv21 #66

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
Open
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
16 changes: 8 additions & 8 deletions README.md
Original file line number Diff line number Diff line change
@@ -232,8 +232,8 @@ const hash = sha256(message);
const recipient = new HashLockFT(hash);

// create p2pkh from a utxo
// NOTE: You can not use BSV20V1P2PKH.getLatestInstance for bsv20, it only works for NFT
const p2pkh = BSV20V1P2PKH.fromUTXO(`your utxo`);
// NOTE: You can not use BSV20P2PKH.getLatestInstance for bsv20, it only works for NFT
const p2pkh = BSV20P2PKH.fromUTXO(`your utxo`);

await p2pkh.connect(getDefaultSigner());

@@ -243,7 +243,7 @@ const { tx: transferTx } = await p2pkh.methods.unlock(
{
transfer: recipient,
pubKeyOrAddrToSign: `yourPubKey`,
} as OrdiMethodCallOptions<BSV20V1P2PKH>
} as OrdiMethodCallOptions<BSV20P2PKH>
);

console.log("Transferred FT: ", transferTx.id);
@@ -261,22 +261,22 @@ const hash = sha256(message);
const recipient = new HashLockFT(hash);

// create p2pkh from a utxo
// NOTE: you can not use BSV20V1P2PKH.getLatestInstance for bsv20, it only works for NFT
const bsv20V1P2PKHs = await BSV20V1P2PKH.getBSV20(
// NOTE: you can not use BSV20P2PKH.getLatestInstance for bsv20, it only works for NFT
const bsv20P2PKHs = await BSV20P2PKH.getBSV20(
"DOGE",
`your ordinal address`
);

await Promise.all(bsv20V1P2PKHs.map((p) => p.connect(signer)));
await Promise.all(bsv20P2PKHs.map((p) => p.connect(signer)));
const recipients: Array<FTReceiver> = [
{
instance: new HashLockFT(tick, max, lim, dec, sha256(message)),
amt: 6n,
},
];

const { tx, nexts } = await BSV20V1P2PKH.transfer(
bsv20V1P2PKHs,
const { tx, nexts } = await BSV20P2PKH.transfer(
bsv20P2PKHs,
signer,
recipients
);
4 changes: 2 additions & 2 deletions src/1satApis.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { UTXO, bsv } from 'scrypt-ts'

import superagent from 'superagent'
import { handlerApiError, isBSV20v2 } from './utils'
import { handlerApiError, isBSV21 } from './utils'

export class OneSatApis {
private static apiBase(network: bsv.Networks.Network) {
@@ -82,7 +82,7 @@ export class OneSatApis {
tick: string
): Promise<Array<UTXO>> {
const network = bsv.Address.fromString(address).network
const url = isBSV20v2(tick)
const url = isBSV21(tick)
? `${this.apiBase(
network || bsv.Networks.mainnet
)}/bsv20/${address}/id/${tick}`
26 changes: 13 additions & 13 deletions src/contracts/bsv20V1.ts → src/contracts/bsv20.ts
Original file line number Diff line number Diff line change
@@ -22,11 +22,11 @@ import { fromByteString } from '../utils'
import { OrdiMethodCallOptions, FTReceiver, Inscription } from '../types'

/**
* A base class implementing the bsv20 v1 protocol
* A base class implementing the bsv20 protocol
*/
export abstract class BSV20V1 extends SmartContract {
export abstract class BSV20 extends SmartContract {
@prop(true)
isBSV20V1: boolean
isBSV20: boolean
/** Ticker: 4 letter identifier of the bsv-20 */
@prop()
readonly tick: ByteString
@@ -49,15 +49,15 @@ export abstract class BSV20V1 extends SmartContract {
this.max = max
this.lim = lim
this.dec = dec
this.isBSV20V1 = true
this.isBSV20 = true
assert(this.max <= 18446744073709551615n)
assert(this.dec <= 18)
}

@method()
buildStateOutputFT(amt: bigint): ByteString {
const stateScript =
BSV20V1.createTransferInsciption(this.tick, amt) +
BSV20.createTransferInsciption(this.tick, amt) +
Ordinal.removeInsciption(this.getStateScript())
return Utils.buildOutput(stateScript, 1n)
}
@@ -68,7 +68,7 @@ export abstract class BSV20V1 extends SmartContract {
tick: ByteString,
amt: bigint
): ByteString {
const transferScript = BSV20V1.buildTransferScript(address, tick, amt)
const transferScript = BSV20.buildTransferScript(address, tick, amt)
return Utils.buildOutput(transferScript, 1n)
}

@@ -79,7 +79,7 @@ export abstract class BSV20V1 extends SmartContract {
amt: bigint
): ByteString {
return (
BSV20V1.createTransferInsciption(tick, amt) +
BSV20.createTransferInsciption(tick, amt) +
Utils.buildPublicKeyHashScript(address)
)
}
@@ -191,8 +191,8 @@ export abstract class BSV20V1 extends SmartContract {
methodName: string
): MethodCallTxBuilder<T> {
return async function (
current: BSV20V1,
options: OrdiMethodCallOptions<BSV20V1>,
current: BSV20,
options: OrdiMethodCallOptions<BSV20>,
...args: any[]
): Promise<ContractTransaction> {
const recipients = (options.transfer || []) as
@@ -217,7 +217,7 @@ export abstract class BSV20V1 extends SmartContract {
tx.addInput(current.buildContractInput())

function addReceiver(receiver: FTReceiver) {
if (receiver.instance instanceof BSV20V1) {
if (receiver.instance instanceof BSV20) {
receiver.instance.setAmt(receiver.amt)
} else {
throw new Error('unsupport receiver!')
@@ -251,8 +251,8 @@ export abstract class BSV20V1 extends SmartContract {
: await current.signer.getDefaultAddress()

// eslint-disable-next-line @typescript-eslint/no-var-requires
const { BSV20V1P2PKH } = require('./bsv20V1P2PKH')
const p2pkh = new BSV20V1P2PKH(
const { BSV20P2PKH } = require('./bsv20P2PKH')
const p2pkh = new BSV20P2PKH(
current.tick,
current.max,
current.lim,
@@ -324,7 +324,7 @@ export abstract class BSV20V1 extends SmartContract {
}

/**
* recover a `BSV20V1` instance from the transaction
* recover a `BSV20` instance from the transaction
* if the contract contains onchain properties of type `HashedMap` or `HashedSet`
* it's required to pass all their offchain raw data at this transaction moment
* @param tx transaction
38 changes: 19 additions & 19 deletions src/contracts/bsv20V1P2PKH.ts → src/contracts/bsv20P2PKH.ts
Original file line number Diff line number Diff line change
@@ -24,12 +24,12 @@ import {

import { Ordinal } from './ordinal'
import { OneSatApis } from '../1satApis'
import { BSV20V1 } from './bsv20V1'
import { BSV20V1_JSON, FTReceiver, OrdiMethodCallOptions } from '../types'
import { BSV20 } from './bsv20'
import { BSV20_JSON, FTReceiver, OrdiMethodCallOptions } from '../types'

const P2PKHScriptLen = 50

export class BSV20V1P2PKH extends BSV20V1 {
export class BSV20P2PKH extends BSV20 {
// Address of the recipient.
@prop()
readonly addr: Addr
@@ -127,7 +127,7 @@ export class BSV20V1P2PKH extends BSV20V1 {
const bsv20 = Ordinal.getBsv20(
bsv.Script.fromHex(script),
true
) as BSV20V1_JSON
) as BSV20_JSON

// recreate instance
const args = delegateInstance.ctorArgs().map((arg) => {
@@ -160,20 +160,20 @@ export class BSV20V1P2PKH extends BSV20V1 {
throw new Error('invalid ordinal p2pkh utxo')
}

const instance = BSV20V1P2PKH.fromLockingScript(utxo.script) as T
const instance = BSV20P2PKH.fromLockingScript(utxo.script) as T
instance.from = utxo
return instance
}

static async fromOutPoint(
outPoint: string,
network?: bsv.Networks.Network
): Promise<BSV20V1P2PKH> {
): Promise<BSV20P2PKH> {
const utxo = await OneSatApis.fetchUTXOByOutpoint(outPoint, network)
if (utxo === null) {
throw new Error(`no utxo found for outPoint: ${outPoint}`)
}
return BSV20V1P2PKH.fromUTXO(utxo)
return BSV20P2PKH.fromUTXO(utxo)
}

/**
@@ -185,22 +185,22 @@ export class BSV20V1P2PKH extends BSV20V1 {
static async getBSV20(
tick: string,
address: string
): Promise<Array<BSV20V1P2PKH>> {
): Promise<Array<BSV20P2PKH>> {
const bsv20Utxos = await OneSatApis.fetchBSV20Utxos(address, tick)
return bsv20Utxos.map((utxo) => BSV20V1P2PKH.fromUTXO(utxo))
return bsv20Utxos.map((utxo) => BSV20P2PKH.fromUTXO(utxo))
}

/**
* Transfer BSV20 tokens which held by multiple BSV20V1P2PKH instances
* @param senders BSV20V1P2PKH instances
* Transfer BSV20 tokens which held by multiple BSV20P2PKH instances
* @param senders BSV20P2PKH instances
* @param feeSigner used to sign UTXOs that pay transaction fees
* @param receivers token receiving contract
* @param tokenChangeAddress Token change address
* @param sendersPubkey The sender’s public key. By default, the default public key of the Signer connected to BSV20V1P2PKH is used.
* @param sendersPubkey The sender’s public key. By default, the default public key of the Signer connected to BSV20P2PKH is used.
* @returns
*/
static async transfer(
senders: Array<BSV20V1P2PKH>,
senders: Array<BSV20P2PKH>,
feeSigner: Signer,
receivers: Array<FTReceiver>,
tokenChangeAddress: bsv.Address,
@@ -235,7 +235,7 @@ export class BSV20V1P2PKH extends BSV20V1 {
for (let i = 0; i < receivers.length; i++) {
const receiver = receivers[i]

if (receiver.instance instanceof BSV20V1) {
if (receiver.instance instanceof BSV20) {
receiver.instance.setAmt(receiver.amt)
} else {
throw new Error('unsupport receiver, only BSV20!')
@@ -291,8 +291,8 @@ export class BSV20V1P2PKH extends BSV20V1 {
p2pkh.bindTxBuilder(
'unlock',
async (
current: BSV20V1P2PKH,
options: MethodCallOptions<BSV20V1P2PKH>
current: BSV20P2PKH,
options: MethodCallOptions<BSV20P2PKH>
): Promise<ContractTransaction> => {
if (options.partialContractTx?.tx) {
const tx = options.partialContractTx.tx
@@ -323,7 +323,7 @@ export class BSV20V1P2PKH extends BSV20V1 {
},
pubKeyOrAddrToSign: pubkey,
multiContractCall: true,
} as OrdiMethodCallOptions<BSV20V1P2PKH>
} as OrdiMethodCallOptions<BSV20P2PKH>
)
}

@@ -341,7 +341,7 @@ export class BSV20V1P2PKH extends BSV20V1 {
const desc = {
version: 9,
compilerVersion: '1.19.0+commit.72eaeba',
contract: 'BSV20V1P2PKH',
contract: 'BSV20P2PKH',
md5: '0c046dfb1f1a91cf72b9a852537bdfe1',
structs: [],
library: [],
@@ -379,4 +379,4 @@ const desc = {
sourceMapFile: '',
}

BSV20V1P2PKH.loadArtifact(desc)
BSV20P2PKH.loadArtifact(desc)
26 changes: 13 additions & 13 deletions src/contracts/bsv20V2.ts → src/contracts/bsv21.ts
Original file line number Diff line number Diff line change
@@ -19,12 +19,12 @@ import {

import { Ordinal } from './ordinal'
import { fromByteString } from '../utils'
import { OrdiMethodCallOptions, FTReceiver, BSV20V2_JSON } from '../types'
import { OrdiMethodCallOptions, FTReceiver, BSV21_JSON } from '../types'

/**
* A base class implementing the bsv20 v2 protocol
* A base class implementing the bsv21 protocol
*/
export abstract class BSV20V2 extends SmartContract {
export abstract class BSV21 extends SmartContract {
/** Ticker: identifier of the bsv-20 */
@prop(true)
id: ByteString
@@ -58,7 +58,7 @@ export abstract class BSV20V2 extends SmartContract {
}

const stateScript =
BSV20V2.createTransferInsciption(this.id, amt) +
BSV21.createTransferInsciption(this.id, amt) +
Ordinal.removeInsciption(this.getStateScript())
return Utils.buildOutput(stateScript, 1n)
}
@@ -74,7 +74,7 @@ export abstract class BSV20V2 extends SmartContract {
id: ByteString,
amt: bigint
): ByteString {
const transferScript = BSV20V2.buildTransferScript(address, id, amt)
const transferScript = BSV21.buildTransferScript(address, id, amt)
return Utils.buildOutput(transferScript, 1n)
}

@@ -85,7 +85,7 @@ export abstract class BSV20V2 extends SmartContract {
amt: bigint
): ByteString {
return (
BSV20V2.createTransferInsciption(id, amt) +
BSV21.createTransferInsciption(id, amt) +
Utils.buildPublicKeyHashScript(address)
)
}
@@ -121,7 +121,7 @@ export abstract class BSV20V2 extends SmartContract {
const nop = this.getPrependNOPScript()

if (nop) {
const bsv20 = Ordinal.getBsv20(nop, false) as BSV20V2_JSON
const bsv20 = Ordinal.getBsv20(nop, false) as BSV21_JSON

if (bsv20.op === 'deploy+mint') {
return `${this.utxo.txId}_${this.utxo.outputIndex}`
@@ -179,8 +179,8 @@ export abstract class BSV20V2 extends SmartContract {
methodName: string
): MethodCallTxBuilder<T> {
return async function (
current: BSV20V2,
options: OrdiMethodCallOptions<BSV20V2>,
current: BSV21,
options: OrdiMethodCallOptions<BSV21>,
...args: any[]
): Promise<ContractTransaction> {
const recipients = (options.transfer || []) as
@@ -205,7 +205,7 @@ export abstract class BSV20V2 extends SmartContract {
tx.addInput(current.buildContractInput())

function addReceiver(receiver: FTReceiver) {
if (receiver.instance instanceof BSV20V2) {
if (receiver.instance instanceof BSV21) {
receiver.instance.setAmt(receiver.amt)
} else {
throw new Error('unsupport receiver!')
@@ -239,8 +239,8 @@ export abstract class BSV20V2 extends SmartContract {
: await current.signer.getDefaultAddress()

// eslint-disable-next-line @typescript-eslint/no-var-requires
const { BSV20V2P2PKH } = require('./bsv20V2P2PKH')
const p2pkh = new BSV20V2P2PKH(
const { BSV21P2PKH } = require('./bsv21P2PKH')
const p2pkh = new BSV21P2PKH(
toByteString(current.getTokenId(), true),
current.sym,
current.max,
@@ -318,7 +318,7 @@ export abstract class BSV20V2 extends SmartContract {
}

/**
* recover a `BSV20V2` instance from the transaction
* recover a `BSV21` instance from the transaction
* if the contract contains onchain properties of type `HashedMap` or `HashedSet`
* it's required to pass all their offchain raw data at this transaction moment
* @param tx transaction
Loading