Skip to content

Commit

Permalink
feat: first version
Browse files Browse the repository at this point in the history
  • Loading branch information
pranksteess committed Dec 6, 2021
0 parents commit 0581047
Show file tree
Hide file tree
Showing 61 changed files with 16,776 additions and 0 deletions.
16 changes: 16 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
# Binaries for programs and plugins
*.exe
*.exe~
*.dll
*.so
*.dylib

# Test binary, built with `go test -c`
*.test

# Output of the go coverage tool, specifically when used with LiteIDE
*.out

# Dependency directories (remove the comment below to include it)
# vendor/
.idea
21 changes: 21 additions & 0 deletions LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
MIT License

Copyright (c) 2021 DAS Team

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
19 changes: 19 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
# das-lib

## Quick View
This repo is a base dependence for most tool in DAS. It includes but not limited to the following features:

* Assembly transaction
* Witness
* Data output
* Parse transaction
* Witness
* Data output
* Sign transaction
* Normal sign
* EIP 712
* Cache for query data on chain
* Molecule SDK for DAS

## License
Das-lib is released under the terms of the MIT license. See [LICENSE](https://github.com/DeAccountSystems/das-lib/blob/main/LICENSE) for more information or see https://opensource.org/licenses/MIT.
185 changes: 185 additions & 0 deletions common/712.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,185 @@
package common

import (
"encoding/hex"
"encoding/json"
"fmt"
"github.com/DeAccountSystems/das-lib/molecule"
"github.com/nervosnetwork/ckb-sdk-go/types"
"math/big"
"strings"
)

type MMJsonObj struct {
Types struct {
EIP712Domain []struct {
Name string `json:"name"`
Type string `json:"type"`
} `json:"EIP712Domain"`
Action []struct {
Name string `json:"name"`
Type string `json:"type"`
} `json:"Action"`
Cell []struct {
Name string `json:"name"`
Type string `json:"type"`
} `json:"Cell"`
Transaction []struct {
Name string `json:"name"`
Type string `json:"type"`
} `json:"Transaction"`
} `json:"types"`
PrimaryType string `json:"primaryType"`
Domain struct {
ChainID int64 `json:"chainId"`
Name string `json:"name"`
VerifyingContract string `json:"verifyingContract"`
Version string `json:"version"`
} `json:"domain"`
Message struct {
DasMessage string `json:"DAS_MESSAGE"`
InputsCapacity string `json:"inputsCapacity"`
OutputsCapacity string `json:"outputsCapacity"`
Fee string `json:"fee"`
Digest string `json:"digest"`
Action *MMJsonAction `json:"action"`
Inputs []MMJsonCellInfo `json:"inputs"`
Outputs []MMJsonCellInfo `json:"outputs"`
} `json:"message"`
}

type MMJsonAction struct {
Action string `json:"action"`
Params string `json:"params"`
}

type MMJsonCellInfo struct {
Capacity string `json:"capacity"`
LockStr string `json:"lock"`
TypeStr string `json:"type"`
Data string `json:"data"`
ExtraData string `json:"extraData"`
}

func (m *MMJsonObj) String() string {
bys, err := json.Marshal(m)
if err != nil {
return ""
}
return string(bys)
}

const (
MaxHashLen = 20
MMJsonObjStr = `{
"types": {
"EIP712Domain": [
{"name": "chainId", "type": "uint256"},
{"name": "name", "type": "string"},
{"name": "verifyingContract", "type": "address"},
{"name": "version", "type": "string"}
],
"Action": [
{"name": "action", "type": "string"},
{"name": "params", "type": "string"}
],
"Cell": [
{"name": "capacity", "type": "string"},
{"name": "lock", "type": "string"},
{"name": "type", "type": "string"},
{"name": "data", "type": "string"},
{"name": "extraData", "type": "string"}
],
"Transaction": [
{"name": "DAS_MESSAGE", "type": "string"},
{"name": "inputsCapacity", "type": "string"},
{"name": "outputsCapacity", "type": "string"},
{"name": "fee", "type": "string"},
{"name": "action", "type": "Action"},
{"name": "inputs", "type": "Cell[]"},
{"name": "outputs", "type": "Cell[]"},
{"name": "digest", "type": "bytes32"}
]
},
"primaryType": "Transaction",
"domain": {
"chainId": 1,
"name": "da.systems",
"verifyingContract": "0x0000000000000000000000000000000020210722",
"version": "1"
},
"message": {
"DAS_MESSAGE": "",
"inputsCapacity": "",
"outputsCapacity": "",
"fee": "",
"action": {},
"inputs": [],
"outputs": []
}
}`
)

func GetMaxHashLenParams(s string) string {
if Has0xPrefix(s) {
s = s[2:]
}
if len(s) > MaxHashLen {
s = s[:MaxHashLen] + "..."
}
return "0x" + s
}

func GetMaxHashLenData(data []byte) string {
if len(data) > MaxHashLen {
return "0x" + hex.EncodeToString(data[:MaxHashLen]) + "..."
} else {
if len(data) == 0 {
return ""
}
return "0x" + hex.EncodeToString(data)
}
}

func GetMaxHashLenScript(script *types.Script, dasContractName DasContractName) string {
if script == nil || dasContractName == "" {
return ""
}
tmp := ""
if len(script.Args) > MaxHashLen {
tmp = "0x" + hex.EncodeToString(script.Args[:MaxHashLen]) + "..."
} else {
tmp = "0x" + hex.EncodeToString(script.Args)
}
return fmt.Sprintf("%s,0x01,%s", dasContractName, tmp)
}

func GetAccountCellExpiredAtFromOutputData(data []byte) (uint64, error) {
if size := len(data); size < ExpireTimeEndIndex {
return 0, fmt.Errorf("invalid data, len not enough, your: %d, want: %d", size, ExpireTimeEndIndex)
}
expireTime, err := molecule.Bytes2GoU64(data[ExpireTimeEndIndex-8 : ExpireTimeEndIndex])
if err != nil {
return 0, fmt.Errorf("BytesToGoU64 err: %s", err)
}
return expireTime, nil
}

func Capacity2Str(capacity uint64) string {
capacityRat := new(big.Rat).SetInt(new(big.Int).SetUint64(capacity))
oneCkbRat := new(big.Rat).SetInt(new(big.Int).SetUint64(OneCkb))
capacityStr := new(big.Rat).Quo(capacityRat, oneCkbRat).FloatString(8)
if !strings.Contains(capacityStr, ".") {
return capacityStr
}
for i := len(capacityStr) - 1; i >= 0; i-- {
if capacityStr[i] == '.' {
return capacityStr[:i]
}
if capacityStr[i] != '0' {
return capacityStr[:i+1]
}

}
return capacityStr
}
40 changes: 40 additions & 0 deletions common/action.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
package common

type DasAction = string

const (
DasActionConfig DasAction = "config"
DasActionApplyRegister DasAction = "apply_register"
DasActionRefundApply DasAction = "refund_apply"
DasActionPreRegister DasAction = "pre_register"
DasActionPropose DasAction = "propose"
DasActionTransferAccount DasAction = "transfer_account"
DasActionRenewAccount DasAction = "renew_account"
DasActionExtendPropose DasAction = "extend_proposal"
DasActionConfirmProposal DasAction = "confirm_proposal"
DasActionRecycleProposal DasAction = "recycle_proposal"
DasActionWithdrawFromWallet DasAction = "withdraw_from_wallet"
DasActionEditManager DasAction = "edit_manager"
DasActionEditRecords DasAction = "edit_records"
DasActionStartAccountSale DasAction = "start_account_sale"
DasActionEditAccountSale DasAction = "edit_account_sale"
DasActionCancelAccountSale DasAction = "cancel_account_sale"
DasActionBuyAccount DasAction = "buy_account"
DasActionSellAccount DasAction = "sell_account"
DasActionCreateIncome DasAction = "create_income"
DasActionConsolidateIncome DasAction = "consolidate_income"
DasActionDeclareReverseRecord DasAction = "declare_reverse_record"
DasActionRedeclareReverseRecord DasAction = "redeclare_reverse_record"
DasActionRetractReverseRecord DasAction = "retract_reverse_record"
DasActionTransfer DasAction = "transfer"

DasActionMakeOffer DasAction = "make_offer"
DasActionEditOffer DasAction = "edit_offer"
DasActionCancelOffer DasAction = "cancel_offer"
DasActionAcceptOffer DasAction = "accept_offer"
)

const (
ParamOwner = "0x00"
ParamManager = "0x01"
)
66 changes: 66 additions & 0 deletions common/address.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
package common

import (
"fmt"
"github.com/tron-us/go-common/crypto"
)

type ChainType int

const (
ChainTypeCkb ChainType = 0
ChainTypeEth ChainType = 1
ChainTypeBtc ChainType = 2
ChainTypeTron ChainType = 3

HexPreFix = "0x"
TronPreFix = "41"
TronBase58PreFix = "T"
DasLockCkbPreFix = "00"
DasLockEthPreFix = "03"
DasLockTronPreFix = "04"
DasLockEth712PreFix = "05"
)

const (
TronMessageHeader = "\x19TRON Signed Message:\n32"
EthMessageHeader = "\x19Ethereum Signed Message:\n32"
)

const (
DasAccountSuffix = ".bit"
DasLockArgsLen = 42
DasAccountIdLen = 20
HashBytesLen = 32
ExpireTimeEndIndex = HashBytesLen + DasAccountIdLen*2 + 8
)

func (c ChainType) String() string {
switch c {
case ChainTypeCkb:
return "CKB"
case ChainTypeBtc:
return "BTC"
case ChainTypeEth:
return "ETH"
case ChainTypeTron:
return "TRON"
}
return ""
}

func TronHexToBase58(address string) (string, error) {
tAddr, err := crypto.Encode58Check(&address)
if err != nil {
return "", fmt.Errorf("Encode58Check:%v", err)
}
return *tAddr, nil
}

func TronBase58ToHex(address string) (string, error) {
addr, err := crypto.Decode58Check(&address)
if err != nil {
return "", fmt.Errorf("Decode58Check:%v", err)
}
return *addr, nil
}
Loading

0 comments on commit 0581047

Please sign in to comment.