Skip to content

Commit

Permalink
feat: deprecate old transaction and add new
Browse files Browse the repository at this point in the history
  • Loading branch information
GaeaKat committed Jun 27, 2023
1 parent 76e3416 commit 883639b
Show file tree
Hide file tree
Showing 6 changed files with 119 additions and 4 deletions.
1 change: 1 addition & 0 deletions lib/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,3 +27,4 @@ export { Job as BoostPowJob }
export { Redeem as BoostPowJobProof }
export { Metadata as BoostPowMetadata }
export { Utils as BoostUtilsHelper }
export * as transaction from './transaction'
8 changes: 7 additions & 1 deletion lib/job.ts
Original file line number Diff line number Diff line change
Expand Up @@ -280,7 +280,7 @@ export class Job {
return true
}

private static readScript(script: bsv.Script, txid?: string, vout?: number, value?: number): Job {
public static readScript(script: bsv.Script, txid?: string, vout?: number, value?: number): Job {
let category
let content
let diff
Expand Down Expand Up @@ -491,6 +491,12 @@ export class Job {
return bsv.crypto.Hash.sha256(buffer).reverse().toString('hex')
}

/**
* @deprecated
* @param t
* @param vout
* @returns
*/
static fromTransaction(t: bsv.Transaction | Buffer | string, vout: number = 0): Job | undefined {
if (!t) {
return undefined
Expand Down
7 changes: 7 additions & 0 deletions lib/output.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,13 @@ export class Output {
else if (job.vout === undefined) throw "invalid output: missing parameter vout"
}

/**
*
* @deprecated
* @param tx
* @param vout
* @returns
*/
static fromTransaction(tx: bsv.Transaction | Buffer, vout: number): Output | undefined {
let j = Job.fromTransaction(tx, vout)
if (j) return new Output(j)
Expand Down
8 changes: 6 additions & 2 deletions lib/redeem.ts
Original file line number Diff line number Diff line change
Expand Up @@ -203,7 +203,11 @@ export class Redeem {

return buildOut
}

/**
* @deprecated
* @param tx
* @returns
*/
static fromTransaction(tx: bsv.Transaction): Redeem | undefined {
if (!tx) {
return undefined
Expand Down Expand Up @@ -231,7 +235,7 @@ export class Redeem {
return Redeem.fromTransaction(tx)
}

private static fromScript(script: bsv.Script, txid?: string, vin?: number, spentTxid?: string, spentVout?: number): Redeem {
public static fromScript(script: bsv.Script, txid?: string, vin?: number, spentTxid?: string, spentVout?: number): Redeem {
let signature
let minerPubKey
let time
Expand Down
57 changes: 56 additions & 1 deletion lib/transaction.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,18 @@
import { Job } from './job'
import * as bsv from './bsv'
import { Digest32 } from './fields/digest32'
import { Int32Little } from './fields/int32Little'
import { UInt32Little } from './fields/uint32Little'
import { UInt16Little } from './fields/uint16Little'
import { Digest32 } from './fields/digest32'
import { Digest20 } from './fields/digest20'
import { Bytes } from './fields/bytes'
import { Difficulty } from './fields/difficulty'
import * as work from './work/proof'
import { Redeem } from './redeem'
import { Metadata } from './metadata'
import { Utils } from './utils'
import { Output } from './output'
import { Puzzle } from './puzzle'

// How to create a transaction:
//
Expand Down Expand Up @@ -235,6 +246,50 @@ export function sign(
Buffer.from([sigtype & 0xff])
])
}
export interface ScriptsFound {
jobs: Record<number, Job>;
redemptions: Record<string, Redeem>;

}
/**
*
*
* @export
* @param {(bsv.Transaction | Buffer | string)} t
* @return {*} {(ScriptsFound | undefined)}
*/
export function fromTransaction(t:bsv.Transaction | Buffer | string): ScriptsFound | undefined {
if (!t) {
return undefined
}
const tx: bsv.Transaction = new bsv.Transaction(t)
let curJobs: Record<number, Job> = {};
t.outputs.forEach((output, index) => {
if(output.script && output.script.chunks[0].buf &&
output.script.chunks[0].buf.toString('hex') === Buffer.from('boostpow', 'utf8').toString('hex')) {
let job = Job.readScript(output.script, t.hash, index, output.satoshis);
if(job) {
curJobs[index] = job;
}
}
});
let curRedemptions:Record<string, Redeem> = {};
t.inputs.forEach((input, index) => {
try {
let redeem = Redeem.fromScript(input.script, t.hash, index, input.prevTxId.toString('hex'), input.outputIndex);
if(redeem) {
curRedemptions[index] = redeem;
}
} catch (ex) {
// Skip and try another output
}
});

return {
jobs: curJobs,
redemptions: curRedemptions
};
}

export function verify(
pubkey: Buffer | bsv.PublicKey,
Expand Down
42 changes: 42 additions & 0 deletions test/boost-pow-transaction.test.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 883639b

Please sign in to comment.