Skip to content

Commit

Permalink
feat(release 0.1.0): release alpha
Browse files Browse the repository at this point in the history
  • Loading branch information
louzhixian committed Jun 15, 2020
1 parent 0eb42ef commit 8814bdc
Show file tree
Hide file tree
Showing 9 changed files with 27 additions and 93 deletions.
33 changes: 1 addition & 32 deletions src/interfaces.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { Address, Amount, Script, CellDep, Transaction } from './models';
import { Script, CellDep } from './models';

export interface CKBModel {
validate(): any;
Expand All @@ -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;
Expand All @@ -60,8 +34,3 @@ export interface Config {
multiSigLock: ConfigItem;
pwLock: ConfigItem;
}

export interface ReceivePair {
address: Address;
amount: Amount;
}
14 changes: 0 additions & 14 deletions src/models/address.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
1 change: 1 addition & 0 deletions src/models/cell-dep.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
Expand Down
1 change: 1 addition & 0 deletions src/models/cell-input.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
Expand Down
27 changes: 17 additions & 10 deletions src/models/cell.ts
Original file line number Diff line number Diff line change
@@ -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 {
Expand Down Expand Up @@ -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;
}

Expand Down Expand Up @@ -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();
}

Expand All @@ -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 {
Expand Down
10 changes: 1 addition & 9 deletions src/models/script.ts
Original file line number Diff line number Diff line change
@@ -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);
}
Expand Down Expand Up @@ -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 {
Expand Down
1 change: 1 addition & 0 deletions src/models/transaction.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down
13 changes: 5 additions & 8 deletions src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,27 +27,27 @@ 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;
}

if (negative) {
result = `-${result}`;
}

if (options?.section === 'fraction') {
if (options && options.section === 'fraction') {
result = fraction;
}

Expand Down Expand Up @@ -80,7 +80,7 @@ export const ckbToShannon = (ckbAmount: string): string => {
return result.toString();
};

// from helper
// from @lumos/helper

const LINA = {
PREFIX: 'ckb',
Expand Down Expand Up @@ -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) {
Expand Down
20 changes: 0 additions & 20 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down

0 comments on commit 8814bdc

Please sign in to comment.