Skip to content

Commit c09b6d2

Browse files
committed
feat: add hash type in the script structure
1 parent a7cc81c commit c09b6d2

7 files changed

Lines changed: 51 additions & 12 deletions

File tree

packages/neuron-wallet/src/database/chain/entities/input.ts

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { Entity, BaseEntity, Column, ManyToOne, PrimaryGeneratedColumn } from 'typeorm'
2-
import { OutPoint, Input as InputInterface, CellOutPoint } from '../../../types/cell-types'
2+
import { OutPoint, Input as InputInterface, CellOutPoint, ScriptHashType } from '../../../types/cell-types'
33
import Transaction from './transaction'
44

55
/* eslint @typescript-eslint/no-unused-vars: "warn" */
@@ -42,13 +42,22 @@ export default class Input extends BaseEntity {
4242
})
4343
capacity: string | null = null
4444

45+
@Column({
46+
type: 'enum',
47+
enum: ScriptHashType,
48+
default: ScriptHashType.Data,
49+
nullable: false,
50+
})
51+
hashType: ScriptHashType = ScriptHashType.Data
52+
4553
public cellOutPoint(): CellOutPoint | null {
4654
if (!this.outPointTxHash || !this.outPointIndex) {
4755
return null
4856
}
4957
return {
5058
txHash: this.outPointTxHash,
5159
index: this.outPointIndex,
60+
hashType: this.hashType,
5261
}
5362
}
5463

packages/neuron-wallet/src/database/chain/entities/output.ts

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { Entity, BaseEntity, Column, PrimaryColumn, ManyToOne } from 'typeorm'
2-
import { Script, OutPoint, Cell, CellOutPoint } from '../../../types/cell-types'
2+
import { Script, OutPoint, Cell, CellOutPoint, ScriptHashType } from '../../../types/cell-types'
33
import TransactionEntity from './transaction'
44

55
/* eslint @typescript-eslint/no-unused-vars: "warn" */
@@ -35,10 +35,19 @@ export default class Output extends BaseEntity {
3535
})
3636
status!: string
3737

38+
@Column({
39+
type: 'enum',
40+
enum: ScriptHashType,
41+
default: ScriptHashType.Data,
42+
nullable: false,
43+
})
44+
hashType!: ScriptHashType
45+
3846
public cellOutPoint(): CellOutPoint {
3947
return {
4048
txHash: this.outPointTxHash,
4149
index: this.outPointIndex,
50+
hashType: this.hashType,
4251
}
4352
}
4453

packages/neuron-wallet/src/database/chain/migrations/1561695143591-InitMigration.ts

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,9 @@ import {MigrationInterface, QueryRunner} from "typeorm";
33
export class InitMigration1561695143591 implements MigrationInterface {
44

55
public async up(queryRunner: QueryRunner): Promise<any> {
6-
await queryRunner.query(`CREATE TABLE "output" ("outPointTxHash" varchar NOT NULL, "outPointIndex" varchar NOT NULL, "capacity" varchar NOT NULL, "lock" text NOT NULL, "lockHash" varchar NOT NULL, "status" varchar NOT NULL, "transactionHash" varchar, PRIMARY KEY ("outPointTxHash", "outPointIndex"))`);
6+
await queryRunner.query(`CREATE TABLE "output" ("outPointTxHash" varchar NOT NULL, "outPointIndex" varchar NOT NULL, "capacity" varchar NOT NULL, "lock" text NOT NULL, "lockHash" varchar NOT NULL, "status" varchar NOT NULL, "hashType" enum NOT NULL, transactionHash" varchar, PRIMARY KEY ("outPointTxHash", "outPointIndex"))`);
77
await queryRunner.query(`CREATE TABLE "transaction" ("hash" varchar PRIMARY KEY NOT NULL, "version" varchar NOT NULL, "deps" text NOT NULL, "witnesses" text NOT NULL, "timestamp" varchar, "blockNumber" varchar, "blockHash" varchar, "description" varchar, "createdAt" varchar NOT NULL, "updatedAt" varchar NOT NULL)`);
8-
await queryRunner.query(`CREATE TABLE "input" ("id" integer PRIMARY KEY AUTOINCREMENT NOT NULL, "outPointTxHash" varchar, "outPointIndex" varchar, "since" varchar NOT NULL, "lockHash" varchar, "capacity" varchar, "transactionHash" varchar)`);
8+
await queryRunner.query(`CREATE TABLE "input" ("id" integer PRIMARY KEY AUTOINCREMENT NOT NULL, "outPointTxHash" varchar, "outPointIndex" varchar, "since" varchar NOT NULL, "lockHash" varchar, "capacity" varchar, "hashType" enum NOT NULL, "transactionHash" varchar)`);
99
await queryRunner.query(`CREATE TABLE "sync_info" ("name" varchar PRIMARY KEY NOT NULL, "value" varchar NOT NULL)`);
1010
await queryRunner.query(`CREATE TABLE "temporary_output" ("outPointTxHash" varchar NOT NULL, "outPointIndex" varchar NOT NULL, "capacity" varchar NOT NULL, "lock" text NOT NULL, "lockHash" varchar NOT NULL, "status" varchar NOT NULL, "transactionHash" varchar, CONSTRAINT "FK_29236a0eb11fac458990882f985" FOREIGN KEY ("transactionHash") REFERENCES "transaction" ("hash") ON DELETE CASCADE ON UPDATE NO ACTION, PRIMARY KEY ("outPointTxHash", "outPointIndex"))`);
1111
await queryRunner.query(`INSERT INTO "temporary_output"("outPointTxHash", "outPointIndex", "capacity", "lock", "lockHash", "status", "transactionHash") SELECT "outPointTxHash", "outPointIndex", "capacity", "lock", "lockHash", "status", "transactionHash" FROM "output"`);
@@ -19,12 +19,12 @@ export class InitMigration1561695143591 implements MigrationInterface {
1919

2020
public async down(queryRunner: QueryRunner): Promise<any> {
2121
await queryRunner.query(`ALTER TABLE "input" RENAME TO "temporary_input"`);
22-
await queryRunner.query(`CREATE TABLE "input" ("id" integer PRIMARY KEY AUTOINCREMENT NOT NULL, "outPointTxHash" varchar, "outPointIndex" varchar, "since" varchar NOT NULL, "lockHash" varchar, "capacity" varchar, "transactionHash" varchar)`);
23-
await queryRunner.query(`INSERT INTO "input"("id", "outPointTxHash", "outPointIndex", "since", "lockHash", "capacity", "transactionHash") SELECT "id", "outPointTxHash", "outPointIndex", "since", "lockHash", "capacity", "transactionHash" FROM "temporary_input"`);
22+
await queryRunner.query(`CREATE TABLE "input" ("id" integer PRIMARY KEY AUTOINCREMENT NOT NULL, "outPointTxHash" varchar, "outPointIndex" varchar, "since" varchar NOT NULL, "lockHash" varchar, "capacity" varchar, "hashType: enum NOT NULL, "transactionHash" varchar)`);
23+
await queryRunner.query(`INSERT INTO "input"("id", "outPointTxHash", "outPointIndex", "since", "lockHash", "capacity", "hashType", "transactionHash") SELECT "id", "outPointTxHash", "outPointIndex", "since", "lockHash", "capacity", "hashType", "transactionHash" FROM "temporary_input"`);
2424
await queryRunner.query(`DROP TABLE "temporary_input"`);
2525
await queryRunner.query(`ALTER TABLE "output" RENAME TO "temporary_output"`);
26-
await queryRunner.query(`CREATE TABLE "output" ("outPointTxHash" varchar NOT NULL, "outPointIndex" varchar NOT NULL, "capacity" varchar NOT NULL, "lock" text NOT NULL, "lockHash" varchar NOT NULL, "status" varchar NOT NULL, "transactionHash" varchar, PRIMARY KEY ("outPointTxHash", "outPointIndex"))`);
27-
await queryRunner.query(`INSERT INTO "output"("outPointTxHash", "outPointIndex", "capacity", "lock", "lockHash", "status", "transactionHash") SELECT "outPointTxHash", "outPointIndex", "capacity", "lock", "lockHash", "status", "transactionHash" FROM "temporary_output"`);
26+
await queryRunner.query(`CREATE TABLE "output" ("outPointTxHash" varchar NOT NULL, "outPointIndex" varchar NOT NULL, "capacity" varchar NOT NULL, "lock" text NOT NULL, "lockHash" varchar NOT NULL, "status" varchar NOT NULL, "hashType" enum NOT NULL, "transactionHash" varchar, PRIMARY KEY ("outPointTxHash", "outPointIndex"))`);
27+
await queryRunner.query(`INSERT INTO "output"("outPointTxHash", "outPointIndex", "capacity", "lock", "lockHash", "status", "hashType", "transactionHash") SELECT "outPointTxHash", "outPointIndex", "capacity", "lock", "lockHash", "status", hashType", "transactionHash" FROM "temporary_output"`);
2828
await queryRunner.query(`DROP TABLE "temporary_output"`);
2929
await queryRunner.query(`DROP TABLE "sync_info"`);
3030
await queryRunner.query(`DROP TABLE "input"`);

packages/neuron-wallet/src/models/lock-utils.ts

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import NodeService from '../services/node'
2-
import { OutPoint, Script } from '../types/cell-types'
2+
import { OutPoint, Script, ScriptHashType } from '../types/cell-types'
33
import env from '../env'
44
import systemScriptSubject from './subjects/system-script'
55

@@ -56,6 +56,7 @@ export default class LockUtils {
5656
cell: {
5757
txHash,
5858
index,
59+
hashType: ScriptHashType.Data,
5960
},
6061
},
6162
}
@@ -77,6 +78,7 @@ export default class LockUtils {
7778
const lockHash: string = core.utils.lockScriptToHash({
7879
codeHash,
7980
args,
81+
hashType: ScriptHashType.Data,
8082
})
8183

8284
if (lockHash.startsWith('0x')) {
@@ -92,6 +94,7 @@ export default class LockUtils {
9294
const lock: Script = {
9395
codeHash: systemScript.codeHash,
9496
args: [LockUtils.addressToBlake160(address)],
97+
hashType: ScriptHashType.Data,
9598
}
9699
return lock
97100
}
@@ -120,7 +123,7 @@ export default class LockUtils {
120123
static addressToBlake160(address: string): string {
121124
const prefix = env.testnet ? core.utils.AddressPrefix.Testnet : core.utils.AddressPrefix.Mainnet
122125
const result: string = core.utils.parseAddress(address, prefix, 'hex') as string
123-
const hrp: string = `01${Buffer.from('P2PH').toString('hex')}`
126+
const hrp: string = `0100`
124127
let blake160: string = result.slice(hrp.length, result.length)
125128
if (!blake160.startsWith('0x')) {
126129
blake160 = `0x${blake160}`

packages/neuron-wallet/src/services/transactions.ts

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,14 @@
11
import { getConnection, In, ObjectLiteral } from 'typeorm'
22
import { ReplaySubject } from 'rxjs'
3-
import { OutPoint, Transaction, TransactionWithoutHash, Input, Cell, TransactionStatus } from '../types/cell-types'
3+
import {
4+
OutPoint,
5+
Transaction,
6+
TransactionWithoutHash,
7+
Input,
8+
Cell,
9+
TransactionStatus,
10+
ScriptHashType,
11+
} from '../types/cell-types'
412
import CellsService, { MIN_CELL_CAPACITY } from './cells'
513
import InputEntity from '../database/chain/entities/input'
614
import OutputEntity from '../database/chain/entities/output'
@@ -522,6 +530,7 @@ export default class TransactionsService {
522530
lock: {
523531
codeHash,
524532
args: [blake160],
533+
hashType: ScriptHashType.Data,
525534
},
526535
}
527536

@@ -540,6 +549,7 @@ export default class TransactionsService {
540549
lock: {
541550
codeHash,
542551
args: [changeBlake160],
552+
hashType: ScriptHashType.Data,
543553
},
544554
}
545555

packages/neuron-wallet/src/types/cell-types.ts

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,10 @@
11
// define types in app
22

3+
export enum ScriptHashType {
4+
Data = 'Data',
5+
Type = 'Type',
6+
}
7+
38
export interface Block {
49
header: BlockHeader
510
transactions: Transaction[]
@@ -70,10 +75,11 @@ export interface OutPoint {
7075
export interface CellOutPoint {
7176
txHash: string
7277
index: string
78+
hashType: ScriptHashType
7379
}
7480

7581
export interface Script {
7682
args?: string[]
7783
codeHash?: string | null
78-
hashType: string
84+
hashType: ScriptHashType
7985
}

packages/neuron-wallet/src/types/type-convert.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,7 @@ export default class TypeConvert {
4848
const cell: CellOutPoint = {
4949
txHash: outPoint.cell!.txHash,
5050
index: outPoint.cell!.index,
51+
hashType: outPoint.cell!.hashType,
5152
}
5253
return {
5354
blockHash: null,
@@ -66,6 +67,7 @@ export default class TypeConvert {
6667
return {
6768
args: script.args,
6869
codeHash: script.codeHash,
70+
hashType: script.hashType,
6971
}
7072
}
7173
}

0 commit comments

Comments
 (0)