Skip to content
Merged
Show file tree
Hide file tree
Changes from 12 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
1 change: 1 addition & 0 deletions .github/CODEOWNERS
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
/packages/jellyfish-transaction/src/script/defi/ @fuxingloh @canonbrother @ivan-zynesis @monstrobishi

/packages/jellyfish-transaction-builder/ @fuxingloh @ivan-zynesis @monstrobishi
/packages/jellyfish-transaction-signature/ @fuxingloh @ivan-zynesis
/packages/jellyfish-wallet/ @fuxingloh @ivan-zynesis
/packages/jellyfish-wallet-mnemonic/ @fuxingloh @ivan-zynesis
/packages/testcontainers/ @fuxingloh
Expand Down
1 change: 1 addition & 0 deletions .github/governance.yml
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ issue:
- jellyfish-network
- jellyfish-transaction
- jellyfish-transaction-builder
- jellyfish-transaction-signature
- jellyfish-wallet
- jellyfish-wallet-mnemonic
- testcontainers
Expand Down
5 changes: 5 additions & 0 deletions .github/labeler.yml
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,11 @@ labels:
matcher:
files: "packages/jellyfish-transaction-builder/**"

- label: area/jellyfish-transaction-signature
sync: true
matcher:
files: "packages/jellyfish-transaction-signature/**"

- label: area/jellyfish-wallet-mnemonic
sync: true
matcher:
Expand Down
2 changes: 2 additions & 0 deletions .github/labels.yml
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,8 @@
name: area/jellyfish-transaction
- color: fbca04
name: area/jellyfish-transaction-builder
- color: fbca04
name: area/jellyfish-transaction-signature
- color: fbca04
name: area/jellyfish-wallet
- color: fbca04
Expand Down
5 changes: 3 additions & 2 deletions jest.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,11 @@ module.exports = {
'@defichain/jellyfish-crypto': '<rootDir>/packages/jellyfish-crypto/src',
'@defichain/jellyfish-json': '<rootDir>/packages/jellyfish-json/src',
'@defichain/jellyfish-network': '<rootDir>/packages/jellyfish-network/src',
'@defichain/jellyfish-transaction': '<rootDir>/packages/jellyfish-transaction/src',
'@defichain/jellyfish-transaction-signature': '<rootDir>/packages/jellyfish-transaction-signature/src',
'@defichain/jellyfish-transaction-builder': '<rootDir>/packages/jellyfish-transaction-builder/src',
'@defichain/jellyfish-wallet': '<rootDir>/packages/jellyfish-wallet/src',
'@defichain/jellyfish-transaction': '<rootDir>/packages/jellyfish-transaction/src',
'@defichain/jellyfish-wallet-mnemonic': '<rootDir>/packages/jellyfish-wallet-mnemonic/src',
'@defichain/jellyfish-wallet': '<rootDir>/packages/jellyfish-wallet/src',
'@defichain/testcontainers': '<rootDir>/packages/testcontainers/src',
'@defichain/testing': '<rootDir>/packages/testing/src'
},
Expand Down
37 changes: 36 additions & 1 deletion package-lock.json

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

Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,9 @@ import {
CTransactionSegWit,
DeFiTransactionConstants,
Transaction,
TransactionSigner,
OP_CODES
} from '@defichain/jellyfish-transaction'
import { TransactionSigner } from '@defichain/jellyfish-transaction-signature'
import { WIF, HASH160 } from '@defichain/jellyfish-crypto'
import { SmartBuffer } from 'smart-buffer'

Expand Down
3 changes: 2 additions & 1 deletion packages/jellyfish-transaction-builder/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,8 @@
},
"dependencies": {
"@defichain/jellyfish-crypto": "0.0.0",
"@defichain/jellyfish-transaction": "0.0.0"
"@defichain/jellyfish-transaction": "0.0.0",
"@defichain/jellyfish-transaction-signature": "0.0.0"
},
"devDependencies": {
"@defichain/jellyfish-api-jsonrpc": "0.0.0",
Expand Down
4 changes: 2 additions & 2 deletions packages/jellyfish-transaction-builder/src/txn/txn_builder.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
import {
DeFiTransactionConstants, OP_CODES,
Script,
SignInputOption,
Transaction,
TransactionSegWit,
TransactionSigner, Vin, Vout, OP_DEFI_TX
Vin, Vout, OP_DEFI_TX
} from '@defichain/jellyfish-transaction'
import { TransactionSigner, SignInputOption } from '@defichain/jellyfish-transaction-signature'
import { BigNumber } from 'bignumber.js'
import { EllipticPairProvider, FeeRateProvider, Prevout, PrevoutProvider } from '../provider'
import { calculateFeeP2WPKH } from './txn_fee'
Expand Down
12 changes: 12 additions & 0 deletions packages/jellyfish-transaction-signature/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
[![npm](https://img.shields.io/npm/v/@defichain/jellyfish-transaction-signature)](https://www.npmjs.com/package/@defichain/jellyfish-transaction-signature/v/latest)
[![npm@next](https://img.shields.io/npm/v/@defichain/jellyfish-transaction-signature/next)](https://www.npmjs.com/package/@defichain/jellyfish-transaction-signature/v/next)

# @defichain/jellyfish-transaction-signature

Stateless utility library to perform transaction signing.

## Prior-art

- https://github.com/ilyavf/tx-builder
- https://github.com/bitpay/bitcore
- https://github.com/bitcoinjs/bitcoinjs-lib
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
import BigNumber from 'bignumber.js'
import { SmartBuffer } from 'smart-buffer'
import { Bech32, WIF } from '@defichain/jellyfish-crypto'
import { OP_CODES, CTransaction, CTransactionSegWit } from '@defichain/jellyfish-transaction'
import { SignInputOption, TransactionSigner } from '../../src'

// From Address P2WPKH
const input = {
bech32: 'bcrt1qykj5fsrne09yazx4n72ue4fwtpx8u65zac9zhn',
privKey: 'cQSsfYvYkK5tx3u1ByK2ywTTc9xJrREc1dd67ZrJqJUEMwgktPWN'
}

const unsigned = '0400000001346faf87ccf6fe45463831a23054780d78e4a6decab10575aba212d2e087eb3d0000000000ffffffff010065cd1d000000001600144ab4391ce5a732e36139e72d79a28e01b7b080340000000000'
const signed = '04000000000101346faf87ccf6fe45463831a23054780d78e4a6decab10575aba212d2e087eb3d0000000000ffffffff010065cd1d000000001600144ab4391ce5a732e36139e72d79a28e01b7b0803400024730440220691a0adc945ae033716e7025235b19618a579545874521d3d70faffcbe95971602200dd1886fe71ab5ab447077680bf891c8faeede9c52e15c0b3789e128899b320e012103987aec2e508e124468f0f07a836d185b329026e7aaf75be48cf12be8f18cbe8100000000'

it('sign transaction', async () => {
const txUnsigned = new CTransaction(
SmartBuffer.fromBuffer(Buffer.from(unsigned, 'hex'))
)

const inputs: SignInputOption[] = [{
prevout: {
script: {
stack: [
OP_CODES.OP_0,
OP_CODES.OP_PUSHDATA(Bech32.toHash160(input.bech32, 'bcrt', 0x00), 'little')
]
},
value: new BigNumber('10'),
tokenId: 0
},
ellipticPair: WIF.asEllipticPair(input.privKey)
}]
const txSigned = new CTransactionSegWit(await TransactionSigner.sign(txUnsigned, inputs))

const toBuffer = new SmartBuffer()
txSigned.toBuffer(toBuffer)

expect(toBuffer.toBuffer().toString('hex')).toStrictEqual(signed)
})
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
import BigNumber from 'bignumber.js'
import { SmartBuffer } from 'smart-buffer'
import { Bech32, WIF } from '@defichain/jellyfish-crypto'
import { OP_CODES, CTransaction, CTransactionSegWit } from '@defichain/jellyfish-transaction'
import { SignInputOption, TransactionSigner } from '../../src'

// From Address P2WPKH
const input = {
bech32: 'bcrt1qykj5fsrne09yazx4n72ue4fwtpx8u65zac9zhn',
privKey: 'cQSsfYvYkK5tx3u1ByK2ywTTc9xJrREc1dd67ZrJqJUEMwgktPWN'
}

const unsigned = '040000000193c90783761bf94838ced5a8313eb355c3bdd053cdbdbb3f9e0f3dbc3243609b0000000000ffffffff020065cd1d000000001600144ab4391ce5a732e36139e72d79a28e01b7b080340080ce341d0000000016001425a544c073cbca4e88d59f95ccd52e584c7e6a820000000000'
const signed = '0400000000010193c90783761bf94838ced5a8313eb355c3bdd053cdbdbb3f9e0f3dbc3243609b0000000000ffffffff020065cd1d000000001600144ab4391ce5a732e36139e72d79a28e01b7b080340080ce341d0000000016001425a544c073cbca4e88d59f95ccd52e584c7e6a82000247304402201142c461b7b52323654710b14074928dd8e623d75141f9eb8c2132b7cb2d47c202202883fde993e1ecf0cf3955235522e9fe948b523b568d0e6b427f83c6f1b3efd9012103987aec2e508e124468f0f07a836d185b329026e7aaf75be48cf12be8f18cbe8100000000'

it('sign transaction', async () => {
const txUnsigned = new CTransaction(
SmartBuffer.fromBuffer(Buffer.from(unsigned, 'hex'))
)

const inputs: SignInputOption[] = [{
prevout: {
script: {
stack: [
OP_CODES.OP_0,
OP_CODES.OP_PUSHDATA(Bech32.toHash160(input.bech32, 'bcrt', 0x00), 'little')
]
},
value: new BigNumber('10'),
tokenId: 0
},
ellipticPair: WIF.asEllipticPair(input.privKey)
}]
const txSigned = new CTransactionSegWit(await TransactionSigner.sign(txUnsigned, inputs))

const toBuffer = new SmartBuffer()
txSigned.toBuffer(toBuffer)

expect(toBuffer.toBuffer().toString('hex')).toStrictEqual(signed)
})
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import BigNumber from 'bignumber.js'
import { OP_CODES, OP_PUSHDATA, SIGHASH, Transaction, TransactionSigner, Vout } from '../../src'

import { OP_CODES, OP_PUSHDATA, SIGHASH, Transaction, Vout } from '@defichain/jellyfish-transaction'
import { SHA256, HASH160, Elliptic } from '@defichain/jellyfish-crypto'
import { TransactionSigner } from '../../src'

describe('sign single input', () => {
const transaction: Transaction = {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import BigNumber from 'bignumber.js'
import { OP_CODES, OP_PUSHDATA, DeFiTransactionConstants, SIGHASH, Transaction, TransactionSigner, Vout } from '../../src'

import { OP_CODES, OP_PUSHDATA, DeFiTransactionConstants, SIGHASH, Transaction, Vout } from '@defichain/jellyfish-transaction'
import { Elliptic } from '@defichain/jellyfish-crypto'
import { TransactionSigner } from '../../src'

describe('sign transaction', () => {
const transaction: Transaction = {
Expand Down
12 changes: 12 additions & 0 deletions packages/jellyfish-transaction-signature/jest.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
module.exports = {
testEnvironment: 'node',
testMatch: [
'**/__tests__/**/*.test.ts'
],
transform: {
'^.+\\.ts$': 'ts-jest'
},
verbose: true,
clearMocks: true,
testTimeout: 120000
}
50 changes: 50 additions & 0 deletions packages/jellyfish-transaction-signature/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
{
"private": false,
"name": "@defichain/jellyfish-transaction-signature",
"version": "0.0.0",
"description": "A collection of TypeScript + JavaScript tools and libraries for DeFi Blockchain developers to build decentralized finance on Bitcoin",
"keywords": [
"DeFiChain",
"DeFi",
"Blockchain",
"API",
"Bitcoin"
],
"repository": "DeFiCh/jellyfish",
"bugs": "https://github.com/DeFiCh/jellyfish/issues",
"license": "MIT",
"contributors": [
{
"name": "DeFiChain Foundation",
"email": "engineering@defichain.com",
"url": "https://defichain.com/"
},
{
"name": "DeFi Blockchain Contributors"
},
{
"name": "DeFi Jellyfish Contributors"
}
],
"main": "dist",
"types": "dist",
"files": [
"dist"
],
"scripts": {
"build": "tsc -b ./tsconfig.build.json"
},
"peerDependencies": {
"bignumber.js": "^9.0.1"
},
"dependencies": {
"@defichain/jellyfish-crypto": "0.0.0",
"@defichain/jellyfish-transaction": "0.0.0",
"smart-buffer": "^4.1.0"
},
"devDependencies": {
"@defichain/jellyfish-api-core": "0.0.0",
"@defichain/jellyfish-api-jsonrpc": "0.0.0",
"@defichain/testcontainers": "0.0.0"
}
Comment thread
fuxingloh marked this conversation as resolved.
Outdated
}
1 change: 1 addition & 0 deletions packages/jellyfish-transaction-signature/src/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export * from './tx_signature'
Original file line number Diff line number Diff line change
@@ -1,21 +1,20 @@
import { EllipticPair, dSHA256, HASH160 } from '@defichain/jellyfish-crypto'
import { SmartBuffer } from 'smart-buffer'
import { Script, Transaction, TransactionSegWit, Vin, Vout, Witness } from './tx'
import { OP_CODES, OP_PUSHDATA } from './script'
import { CWitnessProgram, WitnessProgram } from './tx_segwit'
import { DeFiTransactionConstants } from './index'
import { writeVarUInt } from './buffer/buffer_varuint'
import { ONE_HUNDRED_MILLION, writeBigNumberUInt64 } from './buffer/buffer_bignumber'

export enum SIGHASH {
ALL = 0x01,
NONE = 0x02,
SINGLE = 0x03,
ANYONECANPAY = 0x80,
ALL_ANYONECANPAY = SIGHASH.ALL | SIGHASH.ANYONECANPAY,
NONE_ANYONECANPAY = SIGHASH.NONE | SIGHASH.ANYONECANPAY,
SINGLE_ANYONECANPAY = SIGHASH.SINGLE | SIGHASH.ANYONECANPAY,
}
import {
Script,
Transaction,
TransactionSegWit,
Vin,
Vout,
Witness,
OP_CODES,
OP_PUSHDATA,
CWitnessProgram,
WitnessProgram,
DeFiTransactionConstants,
SIGHASH,
CVoutV4
} from '@defichain/jellyfish-transaction'

export interface SignInputOption {
/**
Expand Down Expand Up @@ -73,12 +72,7 @@ function hashOutputs (transaction: Transaction, sigHashType: SIGHASH): string {
}

const buffer = new SmartBuffer()
for (const vout of transaction.vout) {
const satoshi = vout.value.multipliedBy(ONE_HUNDRED_MILLION)
writeBigNumberUInt64(satoshi, buffer)
OP_CODES.toBuffer(vout.script.stack, buffer)
writeVarUInt(vout.tokenId, buffer)
}
transaction.vout.forEach(vout => (new CVoutV4(vout)).toBuffer(buffer))
return dSHA256(buffer.toBuffer()).toString('hex')
}

Expand Down
Loading