Skip to content

Commit

Permalink
Add splice opcodes (#510)
Browse files Browse the repository at this point in the history
* Add splice related opcodes

* Fix test
  • Loading branch information
jingyi2811 authored Jul 29, 2021
1 parent 053839f commit bd19f65
Show file tree
Hide file tree
Showing 5 changed files with 170 additions and 6 deletions.
90 changes: 90 additions & 0 deletions packages/jellyfish-transaction/__tests__/script/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -882,6 +882,96 @@ describe('[OP_TUCK]', () => {
})
})

describe('[OP_CAT]', () => {
const hex = '017e'

it('should map fromBuffer', () => {
const codes = OP_CODES.fromBuffer(SmartBuffer.fromBuffer(
Buffer.from(hex, 'hex')
))
expect(codes[0].type).toStrictEqual('OP_CAT')
expect(codes.length).toStrictEqual(1)
})

it('should map toBuffer', () => {
const smartBuffer = new SmartBuffer()
OP_CODES.toBuffer([OP_CODES.OP_CAT], smartBuffer)
expect(smartBuffer.toBuffer().toString('hex')).toStrictEqual(hex)
})
})

describe('[OP_SUBSTR]', () => {
const hex = '017f'

it('should map fromBuffer', () => {
const codes = OP_CODES.fromBuffer(SmartBuffer.fromBuffer(
Buffer.from(hex, 'hex')
))
expect(codes[0].type).toStrictEqual('OP_SUBSTR')
expect(codes.length).toStrictEqual(1)
})

it('should map toBuffer', () => {
const smartBuffer = new SmartBuffer()
OP_CODES.toBuffer([OP_CODES.OP_SUBSTR], smartBuffer)
expect(smartBuffer.toBuffer().toString('hex')).toStrictEqual(hex)
})
})

describe('[OP_LEFT]', () => {
const hex = '0180'

it('should map fromBuffer', () => {
const codes = OP_CODES.fromBuffer(SmartBuffer.fromBuffer(
Buffer.from(hex, 'hex')
))
expect(codes[0].type).toStrictEqual('OP_LEFT')
expect(codes.length).toStrictEqual(1)
})

it('should map toBuffer', () => {
const smartBuffer = new SmartBuffer()
OP_CODES.toBuffer([OP_CODES.OP_LEFT], smartBuffer)
expect(smartBuffer.toBuffer().toString('hex')).toStrictEqual(hex)
})
})

describe('[OP_RIGHT]', () => {
const hex = '0181'

it('should map fromBuffer', () => {
const codes = OP_CODES.fromBuffer(SmartBuffer.fromBuffer(
Buffer.from(hex, 'hex')
))
expect(codes[0].type).toStrictEqual('OP_RIGHT')
expect(codes.length).toStrictEqual(1)
})

it('should map toBuffer', () => {
const smartBuffer = new SmartBuffer()
OP_CODES.toBuffer([OP_CODES.OP_RIGHT], smartBuffer)
expect(smartBuffer.toBuffer().toString('hex')).toStrictEqual(hex)
})
})

describe('[OP_SIZE]', () => {
const hex = '0182'

it('should map fromBuffer', () => {
const codes = OP_CODES.fromBuffer(SmartBuffer.fromBuffer(
Buffer.from(hex, 'hex')
))
expect(codes[0].type).toStrictEqual('OP_SIZE')
expect(codes.length).toStrictEqual(1)
})

it('should map toBuffer', () => {
const smartBuffer = new SmartBuffer()
OP_CODES.toBuffer([OP_CODES.OP_SIZE], smartBuffer)
expect(smartBuffer.toBuffer().toString('hex')).toStrictEqual(hex)
})
})

describe('[OP_INVERT]', () => {
const hex = '0183'

Expand Down
20 changes: 20 additions & 0 deletions packages/jellyfish-transaction/__tests__/script/mapping.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -194,6 +194,26 @@ describe('All mapped OP_CODES are setup properly: (static, hex, num, asm)', () =
expectOPCode(script.OP_CODES.OP_TUCK, script.OP_TUCK, 'OP_TUCK', 0x7d, '7d')
})

it('OP_CAT', () => {
expectOPCode(script.OP_CODES.OP_CAT, script.OP_CAT, 'OP_CAT', 0x7e, '7e')
})

it('OP_SUBSTR', () => {
expectOPCode(script.OP_CODES.OP_SUBSTR, script.OP_SUBSTR, 'OP_SUBSTR', 0x7f, '7f')
})

it('OP_LEFT', () => {
expectOPCode(script.OP_CODES.OP_LEFT, script.OP_LEFT, 'OP_LEFT', 0x80, '80')
})

it('OP_RIGHT', () => {
expectOPCode(script.OP_CODES.OP_RIGHT, script.OP_RIGHT, 'OP_RIGHT', 0x81, '81')
})

it('OP_SIZE', () => {
expectOPCode(script.OP_CODES.OP_SIZE, script.OP_SIZE, 'OP_SIZE', 0x82, '82')
})

it('OP_INVERT', () => {
expectOPCode(script.OP_CODES.OP_INVERT, script.OP_INVERT, 'OP_INVERT', 0x83, '83')
})
Expand Down
1 change: 1 addition & 0 deletions packages/jellyfish-transaction/src/script/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ export * from './data'
export * from './constants'
export * from './control'
export * from './stack'
export * from './splice'
export * from './bitwise'
export * from './arithmetic'
export * from './crypto'
Expand Down
19 changes: 13 additions & 6 deletions packages/jellyfish-transaction/src/script/mapping.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import { CDfTx, DfTx } from './dftx/dftx'
import * as constants from './constants'
import * as control from './control'
import * as stack from './stack'
import * as splice from './splice'
import * as bitwise from './bitwise'
import * as arithmetic from './arithmetic'
import * as crypto from './crypto'
Expand Down Expand Up @@ -388,12 +389,12 @@ export const OP_CODES = {
OP_SWAP: new stack.OP_SWAP(),
OP_TUCK: new stack.OP_TUCK(),

// splice ops
// OP_CAT = 0x7e,
// OP_SUBSTR = 0x7f,
// OP_LEFT = 0x80,
// OP_RIGHT = 0x81,
// OP_SIZE = 0x82,
// splice
OP_CAT: new splice.OP_CAT(),
OP_SUBSTR: new splice.OP_SUBSTR(),
OP_LEFT: new splice.OP_LEFT(),
OP_RIGHT: new splice.OP_RIGHT(),
OP_SIZE: new splice.OP_SIZE(),

// bitwise
OP_INVERT: new bitwise.OP_INVERT(),
Expand Down Expand Up @@ -520,6 +521,12 @@ const HEX_MAPPING: {
0x7b: OP_CODES.OP_ROT,
0x7c: OP_CODES.OP_SWAP,
0x7d: OP_CODES.OP_TUCK,
// splice
0x7e: OP_CODES.OP_CAT,
0x7f: OP_CODES.OP_SUBSTR,
0x80: OP_CODES.OP_LEFT,
0x81: OP_CODES.OP_RIGHT,
0x82: OP_CODES.OP_SIZE,
// bitwise
0x83: OP_CODES.OP_INVERT,
0x84: OP_CODES.OP_AND,
Expand Down
46 changes: 46 additions & 0 deletions packages/jellyfish-transaction/src/script/splice.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
import { StaticCode } from './opcode'

/**
* Concatenates two strings. disabled.
*/
export class OP_CAT extends StaticCode {
constructor () {
super(0x7e, 'OP_CAT')
}
}

/**
* Returns a section of a string. disabled.
*/
export class OP_SUBSTR extends StaticCode {
constructor () {
super(0x7f, 'OP_SUBSTR')
}
}

/**
* Keeps only characters left of the specified point in a string. disabled.
*/
export class OP_LEFT extends StaticCode {
constructor () {
super(0x80, 'OP_LEFT')
}
}

/**
* Keeps only characters right of the specified point in a string. disabled.
*/
export class OP_RIGHT extends StaticCode {
constructor () {
super(0x81, 'OP_RIGHT')
}
}

/**
* Pushes the string length of the top element of the stack (without popping it).
*/
export class OP_SIZE extends StaticCode {
constructor () {
super(0x82, 'OP_SIZE')
}
}

0 comments on commit bd19f65

Please sign in to comment.