diff --git a/src/interfaces.ts b/src/interfaces.ts index 4cc846c..a38f360 100644 --- a/src/interfaces.ts +++ b/src/interfaces.ts @@ -1,4 +1,4 @@ -import { Address, Amount, Script, CellDep, Transaction } from './models'; +import { Script, CellDep } from './models'; export interface CKBModel { validate(): any; @@ -16,32 +16,6 @@ export enum DepType { depGroup = 'dep_group', } -export interface BlockHeader { - compact_target: string; - number: string; - parent_hash: string; - nonce: string; - timestamp: string; - transactions_root: string; - proposals_hash: string; - uncles_hash: string; - version: string; - epoch: string; - dao: string; -} - -export interface Block { - header: BlockHeader; - transactions: Transaction[]; - proposals: string[]; - uncles: string[]; -} - -export interface CellbaseWitness { - lock: Script; - message: string; -} - export interface WitnessArgs { lock: string; input_type: string; @@ -60,8 +34,3 @@ export interface Config { multiSigLock: ConfigItem; pwLock: ConfigItem; } - -export interface ReceivePair { - address: Address; - amount: Amount; -} diff --git a/src/models/address.ts b/src/models/address.ts index 7c4fed5..a226cdd 100644 --- a/src/models/address.ts +++ b/src/models/address.ts @@ -66,20 +66,6 @@ export class Address { hashType === HashType.data ? AType.DataCodeHash : AType.TypeCodeHash, prefix: APrefix[getDefaultPrefix()], }); - - /* - const script = transformers.TransformScript(this.toLockScript()); - validators.ValidateScript(script); - const script1 = new LumosScript(SerializeScript(script)); - throw new Error('[toCKBAddress] script' + JSON.stringify(script1)); - - return generateAddress( - { ...script, hash_type: 1 }, - { - config: LumosConfigs[this.addressType], - } - ); - */ } toLockScript(): Script { diff --git a/src/models/cell-dep.ts b/src/models/cell-dep.ts index 850cfef..faf32b2 100644 --- a/src/models/cell-dep.ts +++ b/src/models/cell-dep.ts @@ -4,6 +4,7 @@ import { validators, transformers } from 'ckb-js-toolkit'; export class CellDep implements CKBModel { static fromRPC(data: any): CellDep { + if (!data) return null; validators.ValidateCellDep(data); return new CellDep(data.dep_type, data.out_point); } diff --git a/src/models/cell-input.ts b/src/models/cell-input.ts index de6fb5a..661faa2 100644 --- a/src/models/cell-input.ts +++ b/src/models/cell-input.ts @@ -4,6 +4,7 @@ import { validators, transformers } from 'ckb-js-toolkit'; export class CellInput implements CKBModel { static fromRPC(data: any): CellInput { + if (!data) return null; validators.ValidateCellInput(data); return new CellInput(data.previous_output, data.since); } diff --git a/src/models/cell.ts b/src/models/cell.ts index 518b1e4..9270fe6 100644 --- a/src/models/cell.ts +++ b/src/models/cell.ts @@ -1,12 +1,11 @@ import { CKBModel } from '../interfaces'; -import { utf8ToHex, hexToUtf8 } from '@nervosnetwork/ckb-sdk-utils'; import { Amount, Script, OutPoint } from '.'; import { CellInput } from './cell-input'; // import { minimalCellCapacity } from '../utils'; import { AmountUnit } from './amount'; -import { RPC, validators, transformers, normalizers } from 'ckb-js-toolkit'; +import { RPC, validators, transformers } from 'ckb-js-toolkit'; import { HashType } from '..'; -import { SerializeCellOutput } from '@ckb-lumos/types/lib/core'; +import { byteArrayToHex, hexToByteArray } from '../utils'; export class Cell implements CKBModel { static fromRPC(data: any): Cell { @@ -58,12 +57,13 @@ export class Cell implements CKBModel { } resize() { - const base = SerializeCellOutput( - normalizers.NormalizeCellOutput(transformers.TransformCellOutput(this)) - ).byteLength; - const extra = new Buffer(this.data, 'hex').byteLength; + // const base = SerializeCellOutput( + // normalizers.NormalizeCellOutput(transformers.TransformCellOutput(this)) + // ).byteLength; + const base = this.type ? 102 : 61; + const extra = new Buffer(this.data.replace('0x', ''), 'hex').byteLength; const size = base + extra; - this.capacity = new Amount(size.toString(), AmountUnit.ckb); + this.capacity = new Amount(size.toString()); return size; } @@ -97,7 +97,12 @@ export class Cell implements CKBModel { } setData(data: string) { - this.data = utf8ToHex(data.trim()); + data = data.trim(); + const bytes = []; + for (let i = 0; i < data.length; i++) { + bytes.push(data.charCodeAt(i)); + } + this.data = byteArrayToHex(bytes); this.spaceCheck(); } @@ -111,7 +116,9 @@ export class Cell implements CKBModel { } getData(): string { - return hexToUtf8(this.data.trim()); + return hexToByteArray(this.data.trim()) + .map((char) => String.fromCharCode(char)) + .join(''); } getHexData(): string { diff --git a/src/models/script.ts b/src/models/script.ts index c00c6c8..e28122a 100644 --- a/src/models/script.ts +++ b/src/models/script.ts @@ -1,19 +1,17 @@ import { HashType, CKBModel } from '../interfaces'; import { scriptToHash } from '@nervosnetwork/ckb-sdk-utils'; -// import { SerializeScript } from '@ckb-lumos/types/lib/core'; import { Address, AddressType, AddressPrefix, getDefaultPrefix, } from './address'; -// import { Blake2bHasher } from '../hasher'; -// import { Reader } from 'ckb-js-toolkit'; import { generateAddress, LumosConfigs } from '../utils'; import { validators, transformers } from 'ckb-js-toolkit'; export class Script implements CKBModel { static fromRPC(data: any): Script | null { + if (!data) return null; validators.ValidateScript(data); return new Script(data.code_hash, data.args, data.hash_type); } @@ -48,12 +46,6 @@ export class Script implements CKBModel { toHash(): string { return scriptToHash(this); - /* - const serializedScript = SerializeScript(this.serializeJson()); - return new Blake2bHasher() - .hash(new Reader(serializedScript)) - .serializeJson(); - */ } toAddress(prefix: AddressPrefix = getDefaultPrefix()): Address { diff --git a/src/models/transaction.ts b/src/models/transaction.ts index 5a0880f..9f69583 100644 --- a/src/models/transaction.ts +++ b/src/models/transaction.ts @@ -7,6 +7,7 @@ import { SerializeTransaction } from '@ckb-lumos/types/lib/core'; import { RawTransaction } from '.'; export class Transaction implements CKBModel { + // TODO: add WitnessArgs to adapt different transaction structures public witnesses: string[]; constructor( diff --git a/src/utils.ts b/src/utils.ts index 14df43c..55cd708 100644 --- a/src/utils.ts +++ b/src/utils.ts @@ -27,19 +27,19 @@ export const shannonToCKB = ( fraction = `0${fraction}`; } - if (!options?.pad) { + if (options && !options.pad) { fraction = fraction.match(/^([0-9]*[1-9]|0)(0*)/)[1]; } let whole = JSBI.divide(amount, base).toString(10); - if (options?.commify) { + if (options && options.commify) { whole = whole.replace(/\B(?=(\d{3})+(?!\d))/g, ','); } let result = `${whole}${fraction === '0' ? '' : `.${fraction}`}`; - if (options?.section === 'whole') { + if (options && options.section === 'whole') { result = whole; } @@ -47,7 +47,7 @@ export const shannonToCKB = ( result = `-${result}`; } - if (options?.section === 'fraction') { + if (options && options.section === 'fraction') { result = fraction; } @@ -80,7 +80,7 @@ export const ckbToShannon = (ckbAmount: string): string => { return result.toString(); }; -// from helper +// from @lumos/helper const LINA = { PREFIX: 'ckb', @@ -229,9 +229,6 @@ export function parseAddress(address, { config = LINA } = {}) { if (!scriptTemplate) { throw Error(`Invalid code hash index: ${data[1]}!`); } - // return Object.assign({}, scriptTemplate.SCRIPT, { - // args: byteArrayToHex(data.slice(2)), - // }); return { ...scriptTemplate.SCRIPT, args: byteArrayToHex(data.slice(2)) }; case 2: if (data.length < 33) { diff --git a/yarn.lock b/yarn.lock index 796c725..f9114e6 100644 --- a/yarn.lock +++ b/yarn.lock @@ -203,26 +203,6 @@ typedoc-default-themes "^0.6.0-0" typescript "3.5.x" -"@ckb-lumos/common-scripts@^0.2.4": - version "0.2.4" - resolved "https://registry.npm.taobao.org/@ckb-lumos/common-scripts/download/@ckb-lumos/common-scripts-0.2.4.tgz#d73602505408976f8541f530477a73ba7c3b88e8" - integrity sha1-1zYCUFQIl2+FQfUwR3pzunw7iOg= - dependencies: - "@ckb-lumos/helpers" "^0.2.4" - "@ckb-lumos/types" "^0.2.4" - ckb-js-toolkit "^0.9.1" - immutable "^4.0.0-rc.12" - -"@ckb-lumos/helpers@^0.2.4": - version "0.2.4" - resolved "https://registry.npm.taobao.org/@ckb-lumos/helpers/download/@ckb-lumos/helpers-0.2.4.tgz#b9902c405e49b8a3874cd1ecdd7e72ec6d0f1b5c" - integrity sha1-uZAsQF5JuKOHTNHs3X5y7G0PG1w= - dependencies: - "@ckb-lumos/types" "^0.2.4" - bech32 "^1.1.4" - ckb-js-toolkit "^0.9.1" - immutable "^4.0.0-rc.12" - "@ckb-lumos/types@^0.2.4": version "0.2.4" resolved "https://registry.npm.taobao.org/@ckb-lumos/types/download/@ckb-lumos/types-0.2.4.tgz#1733d29a7489af8ddfc297a240249fb3046fda7a"