|
| 1 | +package builder |
| 2 | + |
| 3 | +import ( |
| 4 | + "github.com/coinbase/rosetta-sdk-go/types" |
| 5 | + "github.com/nervosnetwork/ckb-rosetta-sdk/server/config" |
| 6 | + "github.com/nervosnetwork/ckb-sdk-go/address" |
| 7 | + ckbTypes "github.com/nervosnetwork/ckb-sdk-go/types" |
| 8 | + "strconv" |
| 9 | +) |
| 10 | + |
| 11 | +var _ UnsignedTxBuilder = UnsignedTxBuilderSecp256k1{} |
| 12 | + |
| 13 | +const txVersion uint = 0 |
| 14 | + |
| 15 | +type UnsignedTxBuilderSecp256k1 struct { |
| 16 | + UnsignedTx |
| 17 | + Cfg *config.Config |
| 18 | + InputOperations []*types.Operation |
| 19 | + OutputOperations []*types.Operation |
| 20 | +} |
| 21 | + |
| 22 | +func NewUnsignedTxBuilderSecp256k1(cfg *config.Config, inputOperations []*types.Operation, outputOperations []*types.Operation) *UnsignedTxBuilderSecp256k1 { |
| 23 | + b := UnsignedTxBuilderSecp256k1{ |
| 24 | + Cfg: cfg, |
| 25 | + InputOperations: inputOperations, |
| 26 | + OutputOperations: outputOperations, |
| 27 | + } |
| 28 | + b.UnsignedTx.BuildVersion = b.BuildVersion |
| 29 | + b.UnsignedTx.BuildCellDeps = b.BuildCellDeps |
| 30 | + b.UnsignedTx.BuildHeaderDeps = b.BuildHeaderDeps |
| 31 | + b.UnsignedTx.BuildInputs = b.BuildInputs |
| 32 | + b.UnsignedTx.BuildOutputs = b.BuildOutputs |
| 33 | + b.UnsignedTx.BuildOutputsData = b.BuildOutputsData |
| 34 | + b.UnsignedTx.BuildWitnesses = b.BuildWitnesses |
| 35 | + return &b |
| 36 | +} |
| 37 | + |
| 38 | +func (b UnsignedTxBuilderSecp256k1) BuildVersion() (uint, error) { |
| 39 | + return txVersion, nil |
| 40 | +} |
| 41 | + |
| 42 | +func (b UnsignedTxBuilderSecp256k1) BuildCellDeps() ([]*ckbTypes.CellDep, error) { |
| 43 | + var cellDeps []*ckbTypes.CellDep |
| 44 | + cellDeps = append(cellDeps, &ckbTypes.CellDep{ |
| 45 | + OutPoint: &ckbTypes.OutPoint{ |
| 46 | + TxHash: ckbTypes.HexToHash(b.Cfg.Secp256k1Blake160.Deps[0].TxHash), |
| 47 | + Index: b.Cfg.Secp256k1Blake160.Deps[0].Index, |
| 48 | + }, |
| 49 | + DepType: ckbTypes.DepType(b.Cfg.Secp256k1Blake160.Deps[0].DepType), |
| 50 | + }) |
| 51 | + |
| 52 | + return cellDeps, nil |
| 53 | +} |
| 54 | + |
| 55 | +func (b UnsignedTxBuilderSecp256k1) BuildHeaderDeps() ([]ckbTypes.Hash, error) { |
| 56 | + return []ckbTypes.Hash{}, nil |
| 57 | +} |
| 58 | + |
| 59 | +func (b UnsignedTxBuilderSecp256k1) BuildInputs() ([]*ckbTypes.CellInput, map[string]interface{}, error) { |
| 60 | + var cellInputs []*ckbTypes.CellInput |
| 61 | + for _, operation := range b.InputOperations { |
| 62 | + outPoint, err := GenerateOutPointFromCoinIdentifier(operation.CoinChange.CoinIdentifier.Identifier) |
| 63 | + if err != nil { |
| 64 | + return nil, nil, err |
| 65 | + } |
| 66 | + cellInputs = append(cellInputs, &ckbTypes.CellInput{ |
| 67 | + Since: 0, |
| 68 | + PreviousOutput: outPoint, |
| 69 | + }) |
| 70 | + } |
| 71 | + return cellInputs, nil, nil |
| 72 | +} |
| 73 | + |
| 74 | +func (b UnsignedTxBuilderSecp256k1) BuildOutputs(options map[string]interface{}) ([]*ckbTypes.CellOutput, map[string]interface{}, error) { |
| 75 | + var cellOutputs []*ckbTypes.CellOutput |
| 76 | + for _, operation := range b.OutputOperations { |
| 77 | + parsedAddress, err := address.Parse(operation.Account.Address) |
| 78 | + if err != nil { |
| 79 | + return nil, nil, err |
| 80 | + } |
| 81 | + capacity, err := strconv.ParseUint(operation.Amount.Value, 10, 64) |
| 82 | + if err != nil { |
| 83 | + return nil, nil, err |
| 84 | + } |
| 85 | + cellOutputs = append(cellOutputs, &ckbTypes.CellOutput{ |
| 86 | + Capacity: capacity, |
| 87 | + Lock: parsedAddress.Script, |
| 88 | + }) |
| 89 | + } |
| 90 | + return cellOutputs, nil, nil |
| 91 | +} |
| 92 | + |
| 93 | +func (b UnsignedTxBuilderSecp256k1) BuildOutputsData(options map[string]interface{}) ([][]byte, error) { |
| 94 | + var outputsData [][]byte |
| 95 | + outputsSize := len(b.OutputOperations) |
| 96 | + for i := 0; i < outputsSize; i++ { |
| 97 | + outputsData = append(outputsData, []byte{}) |
| 98 | + } |
| 99 | + return outputsData, nil |
| 100 | +} |
| 101 | + |
| 102 | +func (b UnsignedTxBuilderSecp256k1) BuildWitnesses() ([][]byte, error) { |
| 103 | + cellInputsSize := len(b.InputOperations) |
| 104 | + witnesses := make([][]byte, cellInputsSize) |
| 105 | + indexGroups, err := BuildIndexGroups(b.InputOperations) |
| 106 | + if err != nil { |
| 107 | + return nil, err |
| 108 | + } |
| 109 | + for _, indexes := range indexGroups { |
| 110 | + firstIndexOfGroup := indexes[0] |
| 111 | + witnesses[firstIndexOfGroup] = make([]byte, 85) // empty witnessArg placeholder |
| 112 | + } |
| 113 | + |
| 114 | + return witnesses, nil |
| 115 | +} |
0 commit comments