Skip to content

Commit

Permalink
OP_CODES for stack (#389)
Browse files Browse the repository at this point in the history
* Added OP_TOALTSTACK, OP_FROMALTSTACK, OP_2DROP

* Added OP_2DUP

* Added OP_3DUP and OP_2OVER

* Added OP_2ROT

* Added OP_2SWAP

* Added OP_IFDUP

* Added OP_DEPTH

* Added OP_DROP

* Added OP_NIP

* Added OP_OVER

* Added OP_PICK

* Added OP_ROLL

* Changed the description of OP_ROLL:

* Added OP_SWAP

* Added OP_TUCK

* Removed extra test of OP_2DUP

* Moved all the tests to before OP_RETURN

* Removed unnecessary comment at the top of the file

* Added test for OP_DUP.

* Added test for OP_2DUP
  • Loading branch information
aikchun authored Jun 22, 2021
1 parent 3efc13e commit d26d5e1
Show file tree
Hide file tree
Showing 4 changed files with 591 additions and 20 deletions.
342 changes: 342 additions & 0 deletions packages/jellyfish-transaction/__tests__/script/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -540,6 +540,348 @@ describe('[OP_RETURN]', () => {
})
})

describe('[OP_TOALTSTACK]', () => {
const hex = '016b'

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

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

describe('[OP_FROMALTSTACK]', () => {
const hex = '016c'

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

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

describe('[OP_2DROP]', () => {
const hex = '016d'

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

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

describe('[OP_2DUP]', () => {
const hex = '016e'

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

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

describe('[OP_3DUP]', () => {
const hex = '016f'

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

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

describe('[OP_2OVER]', () => {
const hex = '0170'

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

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

describe('[OP_2ROT]', () => {
const hex = '0171'

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

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

describe('[OP_2SWAP]', () => {
const hex = '0172'

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

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

describe('[OP_IFDUP]', () => {
const hex = '0173'

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

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

describe('[OP_DEPTH]', () => {
const hex = '0174'

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

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

describe('[OP_DROP]', () => {
const hex = '0175'

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

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

describe('[OP_DUP]', () => {
const hex = '0176'

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

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

describe('[OP_NIP]', () => {
const hex = '0177'

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

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

describe('[OP_OVER]', () => {
const hex = '0178'

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

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

describe('[OP_PICK]', () => {
const hex = '0179'

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

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

describe('[OP_ROLL]', () => {
const hex = '017a'

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

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

describe('[OP_ROT]', () => {
const hex = '017b'

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

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

describe('[OP_SWAP]', () => {
const hex = '017c'

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

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

describe('[OP_TUCK]', () => {
const hex = '017d'

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

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

describe('[OP_RIPEMD160]', () => {
const hex = '01a6'

Expand Down
Loading

0 comments on commit d26d5e1

Please sign in to comment.