Skip to content

Commit

Permalink
feat(type): set the value of hash_type to "data" and "type"
Browse files Browse the repository at this point in the history
BREAKING CHANGE: set the value of hash_type to "data" and "type"
  • Loading branch information
Keith-CY committed Aug 24, 2019
1 parent 06c324a commit 36a5512
Show file tree
Hide file tree
Showing 7 changed files with 42 additions and 54 deletions.
6 changes: 3 additions & 3 deletions packages/ckb-sdk-core/examples/sendTransaction.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ const bootstrap = async () => {
*/

const script = {
type: secp256k1Dep.type,
hashType: secp256k1Dep.hashType,
codeHash: `0x${secp256k1Dep.codeHash}`,
args: [`0x${myAddressObj.identifier}`],
}
Expand Down Expand Up @@ -117,7 +117,7 @@ const bootstrap = async () => {
const targetOutput = {
capacity: targetCapacity,
lock: {
type: secp256k1Dep.type,
hashType: secp256k1Dep.hashTypetype,
codeHash: secp256k1Dep.codeHash,
args: [targetIdentifier],
},
Expand All @@ -129,7 +129,7 @@ const bootstrap = async () => {
const changeOutput = {
capacity: 0n,
lock: {
type: secp256k1Dep.type,
hashTypetype: secp256k1Dep.hashType,
codeHash: `0x${secp256k1Dep.codeHash}`,
args: [`0x${myAddressObj.identifier}`],
},
Expand Down
4 changes: 2 additions & 2 deletions packages/ckb-sdk-core/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import Address from '@nervosnetwork/ckb-sdk-address'
import * as utils from '@nervosnetwork/ckb-sdk-utils'

interface DepCellInfo {
type: CKBComponents.ScriptHashType
hashType: CKBComponents.ScriptHashType
codeHash: CKBComponents.Hash256
outPoint: CKBComponents.OutPoint
}
Expand Down Expand Up @@ -106,7 +106,7 @@ class Core {
}

this.config.secp256k1Dep = {
type: 'Type' as CKBComponents.ScriptHashType.Type,
hashType: 'type',
codeHash: secp256k1TypeHash,
outPoint: {
txHash: secp256k1DepTx.hash.replace(/^0x/, ''),
Expand Down
12 changes: 6 additions & 6 deletions packages/ckb-sdk-rpc/__tests__/formatters/params.json
Original file line number Diff line number Diff line change
Expand Up @@ -141,7 +141,7 @@
"lock": {
"args": [],
"codeHash": "0x0000000000000000000000000000000000000000000000000000000000000001",
"hashType": "Data"
"hashType": "data"
},
"type": null
},
Expand All @@ -151,7 +151,7 @@
"lock": {
"args": [],
"code_hash": "0x0000000000000000000000000000000000000000000000000000000000000001",
"hash_type": "Data"
"hash_type": "data"
},
"type": null
}
Expand All @@ -163,7 +163,7 @@
"lock": {
"args": [],
"codeHash": "0x0000000000000000000000000000000000000000000000000000000000000001",
"hashType": "Data"
"hashType": "data"
}
},
"target": {
Expand All @@ -172,7 +172,7 @@
"lock": {
"args": [],
"code_hash": "0x0000000000000000000000000000000000000000000000000000000000000001",
"hash_type": "Data"
"hash_type": "data"
},
"type": null
}
Expand Down Expand Up @@ -228,7 +228,7 @@
"lock": {
"args": [],
"codeHash": "0x0000000000000000000000000000000000000000000000000000000000000001",
"hashType": "Data"
"hashType": "data"
},
"type": null
}
Expand Down Expand Up @@ -267,7 +267,7 @@
"lock": {
"args": [],
"code_hash": "0x0000000000000000000000000000000000000000000000000000000000000001",
"hash_type": "Data"
"hash_type": "data"
},
"type": null
}
Expand Down
52 changes: 26 additions & 26 deletions packages/ckb-sdk-rpc/__tests__/formatters/result.json
Original file line number Diff line number Diff line change
Expand Up @@ -21,12 +21,12 @@
"source": {
"args": [],
"code_hash": "0x0000000000000000000000000000000000000000000000000000000000000001",
"hash_type": "Data"
"hash_type": "data"
},
"target": {
"args": [],
"codeHash": "0x0000000000000000000000000000000000000000000000000000000000000001",
"hashType": "Data"
"hashType": "data"
}
}
],
Expand Down Expand Up @@ -63,25 +63,25 @@
"lock": {
"args": [],
"code_hash": "0x0000000000000000000000000000000000000000000000000000000000000001",
"hash_type": "Data"
"hash_type": "data"
},
"type": {
"args": [],
"code_hash": "0x0000000000000000000000000000000000000000000000000000000000000001",
"hash_type": "Type"
"hash_type": "type"
}
},
"target": {
"capacity": "5000000000000",
"lock": {
"args": [],
"codeHash": "0x0000000000000000000000000000000000000000000000000000000000000001",
"hashType": "Data"
"hashType": "data"
},
"type": {
"args": [],
"codeHash": "0x0000000000000000000000000000000000000000000000000000000000000001",
"hashType": "Type"
"hashType": "type"
}
}
},
Expand All @@ -91,7 +91,7 @@
"lock": {
"args": [],
"code_hash": "0x0000000000000000000000000000000000000000000000000000000000000001",
"hash_type": "Data"
"hash_type": "data"
},
"type": null
},
Expand All @@ -100,7 +100,7 @@
"lock": {
"args": [],
"codeHash": "0x0000000000000000000000000000000000000000000000000000000000000001",
"hashType": "Data"
"hashType": "data"
},
"type": null
}
Expand Down Expand Up @@ -197,7 +197,7 @@
"lock": {
"args": [],
"code_hash": "0x0000000000000000000000000000000000000000000000000000000000000001",
"hash_type": "Data"
"hash_type": "data"
},
"type": null
}
Expand Down Expand Up @@ -226,7 +226,7 @@
"lock": {
"args": [],
"codeHash": "0x0000000000000000000000000000000000000000000000000000000000000001",
"hashType": "Data"
"hashType": "data"
},
"type": null
}
Expand Down Expand Up @@ -345,7 +345,7 @@
"lock": {
"args": [],
"code_hash": "0x0000000000000000000000000000000000000000000000000000000000000001",
"hash_type": "Data"
"hash_type": "data"
},
"type": null
}
Expand Down Expand Up @@ -396,7 +396,7 @@
"lock": {
"args": [],
"codeHash": "0x0000000000000000000000000000000000000000000000000000000000000001",
"hashType": "Data"
"hashType": "data"
},
"type": null
}
Expand Down Expand Up @@ -659,25 +659,25 @@
"lock": {
"args": [],
"code_hash": "0x0000000000000000000000000000000000000000000000000000000000000001",
"hash_type": "Data"
"hash_type": "data"
},
"type": {
"args": [],
"code_hash": "0x0000000000000000000000000000000000000000000000000000000000000001",
"hash_type": "Type"
"hash_type": "type"
}
},
"target": {
"capacity": "5000000000000",
"lock": {
"args": [],
"codeHash": "0x0000000000000000000000000000000000000000000000000000000000000001",
"hashType": "Data"
"hashType": "data"
},
"type": {
"args": [],
"codeHash": "0x0000000000000000000000000000000000000000000000000000000000000001",
"hashType": "Type"
"hashType": "type"
}
}
}
Expand All @@ -694,7 +694,7 @@
"lock": {
"args": [],
"code_hash": "0x0000000000000000000000000000000000000000000000000000000000000001",
"hash_type": "Data"
"hash_type": "data"
},
"type": null
},
Expand All @@ -706,7 +706,7 @@
"lock": {
"args": [],
"codeHash": "0x0000000000000000000000000000000000000000000000000000000000000001",
"hashType": "Data"
"hashType": "data"
},
"type": null
},
Expand All @@ -724,7 +724,7 @@
"lock": {
"code_hash": "0x9e3b3557f11b2b3532ce352bfe8017e9fd11d154c4c7f9b7aaaa1e621b539a08",
"args": ["0x7f52f0fccdd1d11391c441adfb174f87bca612b0"],
"hash_type": "Data"
"hash_type": "data"
},
"type": null,
"capacity": "5000000000000",
Expand All @@ -737,7 +737,7 @@
"lock": {
"codeHash": "0x9e3b3557f11b2b3532ce352bfe8017e9fd11d154c4c7f9b7aaaa1e621b539a08",
"args": ["0x7f52f0fccdd1d11391c441adfb174f87bca612b0"],
"hashType": "Data"
"hashType": "data"
},
"type": null,
"capacity": "5000000000000",
Expand All @@ -761,7 +761,7 @@
"lock": {
"args": [],
"code_hash": "0x0000000000000000000000000000000000000000000000000000000000000001",
"hash_type": "Data"
"hash_type": "data"
},
"type": null
}
Expand All @@ -773,7 +773,7 @@
"lock": {
"args": [],
"codeHash": "0x0000000000000000000000000000000000000000000000000000000000000001",
"hashType": "Data"
"hashType": "data"
},
"type": null
}
Expand All @@ -791,7 +791,7 @@
"lock": {
"codeHash": "0x9e3b3557f11b2b3532ce352bfe8017e9fd11d154c4c7f9b7aaaa1e621b539a08",
"args": ["0x7f52f0fccdd1d11391c441adfb174f87bca612b0"],
"hashType": "Data"
"hashType": "data"
},
"type": null,
"capacity": "5000000000000",
Expand All @@ -806,7 +806,7 @@
"lock": {
"codeHash": "0x9e3b3557f11b2b3532ce352bfe8017e9fd11d154c4c7f9b7aaaa1e621b539a08",
"args": ["0x7f52f0fccdd1d11391c441adfb174f87bca612b0"],
"hashType": "Data"
"hashType": "data"
},
"type": null,
"capacity": "5000000000000",
Expand Down Expand Up @@ -884,7 +884,7 @@
"lock": {
"args": [],
"code_hash": "0x0000000000000000000000000000000000000000000000000000000000000001",
"hash_type": "Data"
"hash_type": "data"
},
"type": null
}
Expand Down Expand Up @@ -918,7 +918,7 @@
"lock": {
"args": [],
"codeHash": "0x0000000000000000000000000000000000000000000000000000000000000001",
"hashType": "Data"
"hashType": "data"
},
"type": null
}
Expand Down
5 changes: 1 addition & 4 deletions packages/ckb-sdk-rpc/types/CKB_RPC/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,10 +23,7 @@ declare module CKB_RPC {

export type TransactionStatus = CKBComponents.TransactionStatus

export enum ScriptHashType {
Data = 'Data',
Type = 'Type',
}
export type ScriptHashType = 'data' | type

export type DepType = 'code' | 'dep_group'

Expand Down
9 changes: 2 additions & 7 deletions packages/ckb-sdk-utils/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,18 +42,13 @@ export const utf8ToBytes = (str: string) => textEncoder.encode(str)

export const utf8ToHex = (str: string) => bytesToHex(utf8ToBytes(str))

export enum ScriptHashType {
Data = 'Data',
Type = 'Type',
}

export const scriptToHash = ({ codeHash = '', args = [], hashType = ScriptHashType.Data }: CKBComponents.Script) => {
export const scriptToHash = ({ codeHash = '', args = [], hashType = 'data' }: CKBComponents.Script) => {
const s = blake2b(32, null, null, PERSONAL)
if (codeHash) {
s.update(hexToBytes(codeHash.replace(/^0x/, '')))
}

if (hashType === ScriptHashType.Data) {
if (hashType === 'data') {
s.update(Buffer.from([0x0]))
} else {
s.update(Buffer.from([0x1]))
Expand Down
8 changes: 2 additions & 6 deletions packages/ckb-types/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,10 +25,7 @@ declare namespace CKBComponents {
Committed = 'committed',
}

export enum ScriptHashType {
Data = 'Data',
Type = 'Type',
}
export type ScriptHashType = 'data' | 'type'

export type DepType = 'code' | 'depGroup'

Expand Down Expand Up @@ -58,8 +55,7 @@ declare namespace CKBComponents {
* @description Script, the script model in CKB. CKB scripts use UNIX standard execution environment. Each script binary should contain a main function with the following signature `int main(int argc, char* argv[]);`. CKB will concat `signed_args` and `args`, then use the concatenated array to fill `argc/argv` part, then start the script execution. Upon termination, the executed `main` function here will provide a return code, `0` means the script execution succeeds, other values mean the execution fails.
* @property args, arguments.
* @property codeHash, point to its dependency, if the referred dependency is listed in the deps field in a transaction, the codeHash means the hash of the referred cell's data.
* @see https://github.com/nervosnetwork/ckb/blob/develop/core/src/script.rs#L16
* @tutorial Each script has a `lock_hash` which uniquely identifies the script, for example, the `lock_hash` of lock script, is exactly the corresponding `lock` script field value in the referenced cell, when calculating hash for a script, `bianryHash`, and `args` will all be used.
* @property hashType, a enumerate indicates the type of the code which is referened by the code hash
*/
/* eslint-enable max-len */
export interface Script {
Expand Down

0 comments on commit 36a5512

Please sign in to comment.