-
Notifications
You must be signed in to change notification settings - Fork 30
/
index.ts
88 lines (79 loc) · 3.18 KB
/
index.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
import JSBI from 'jsbi'
import ECPair from './ecpair'
import { hexToBytes, toBigEndian } from './convertors'
import { pubkeyToAddress, AddressOptions } from './address'
import { ParameterRequiredException, HexStringWithout0xException } from './exceptions'
import crypto from './crypto'
import { serializeScript } from './serialization'
import { cellOccupied } from './occupiedCapacity'
import { serializeRawTransaction, serializeTransaction, serializeWitnessArgs } from './serialization/transaction'
import { PERSONAL } from './const'
export * from './address'
export * from './serialization'
export * from './convertors'
export * from './epochs'
export * from './sizes'
export * from './occupiedCapacity'
export * as systemScripts from './systemScripts'
export * as reconcilers from './reconcilers'
export { serializeScript, serializeRawTransaction, serializeTransaction, serializeWitnessArgs, JSBI, PERSONAL }
export const { blake2b, bech32, bech32m, blake160 } = crypto
export const scriptToHash = (script: CKBComponents.Script) => {
if (!script) throw new ParameterRequiredException('Script')
const serializedScript = serializeScript(script)
const s = blake2b(32, null, null, PERSONAL)
s.update(hexToBytes(serializedScript))
const digest = s.digest('hex')
return `0x${digest}` as string
}
export const rawTransactionToHash = (rawTransaction: Omit<CKBComponents.RawTransaction, 'witnesses'>) => {
if (!rawTransaction) throw new ParameterRequiredException('Raw transaction')
const serializedRawTransaction = serializeRawTransaction(rawTransaction)
const s = blake2b(32, null, null, PERSONAL)
s.update(hexToBytes(serializedRawTransaction))
const digest = s.digest('hex')
return `0x${digest}` as string
}
export const privateKeyToPublicKey = (privateKey: string) => {
const keyPair = new ECPair(privateKey)
return keyPair.publicKey
}
export const privateKeyToAddress = (privateKey: string, options: AddressOptions) =>
pubkeyToAddress(privateKeyToPublicKey(privateKey), options)
export const extractDAOData = (dao: CKBComponents.DAO) => {
if (!dao.startsWith('0x')) {
throw new HexStringWithout0xException(dao)
}
const value = dao.replace('0x', '')
return {
c: toBigEndian(`0x${value.slice(0, 16)}`),
ar: toBigEndian(`0x${value.slice(16, 32)}`),
s: toBigEndian(`0x${value.slice(32, 48)}`),
u: toBigEndian(`0x${value.slice(48, 64)}`)
}
}
export const calculateMaximumWithdraw = (
outputCell: CKBComponents.CellOutput,
outputDataCapacity: CKBComponents.Bytes,
depositDAO: CKBComponents.DAO,
withdrawDAO: CKBComponents.DAO
) => {
const depositCellSerialized = cellOccupied(outputCell) + outputDataCapacity.slice(2).length / 2
const occupiedCapacity = JSBI.asUintN(
128,
JSBI.multiply(JSBI.BigInt(100000000), JSBI.BigInt(depositCellSerialized))
)
return `0x${JSBI.add(
JSBI.divide(
JSBI.multiply(
JSBI.subtract(
JSBI.asUintN(128, JSBI.BigInt(outputCell.capacity)),
occupiedCapacity
),
JSBI.asUintN(128, JSBI.BigInt(extractDAOData(withdrawDAO).ar))
),
JSBI.asUintN(128, JSBI.BigInt(extractDAOData(depositDAO).ar))
),
occupiedCapacity
).toString(16)}`
}