-
Notifications
You must be signed in to change notification settings - Fork 25
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #214 from doitian/docs/custom-script
docs: add docs and examples for signer and builder
- Loading branch information
Showing
7 changed files
with
191 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
package collector_test | ||
|
||
import ( | ||
"reflect" | ||
|
||
"github.com/nervosnetwork/ckb-sdk-go/v2/collector" | ||
"github.com/nervosnetwork/ckb-sdk-go/v2/collector/builder" | ||
"github.com/nervosnetwork/ckb-sdk-go/v2/transaction" | ||
"github.com/nervosnetwork/ckb-sdk-go/v2/types" | ||
) | ||
|
||
// SimpleLockScriptHandler is an example script handler to add specified cell dep and prefill the witness. | ||
type SimpleLockScriptHandler struct { | ||
CellDep *types.CellDep | ||
WitnessPlaceholder []byte | ||
CodeHash types.Hash | ||
} | ||
|
||
func (r *SimpleLockScriptHandler) isMatched(script *types.Script) bool { | ||
if script == nil { | ||
return false | ||
} | ||
return reflect.DeepEqual(script.CodeHash, r.CodeHash) | ||
} | ||
|
||
func (r *SimpleLockScriptHandler) BuildTransaction(builder collector.TransactionBuilder, group *transaction.ScriptGroup, context interface{}) (bool, error) { | ||
// Only run on matched groups | ||
if group == nil || !r.isMatched(group.Script) { | ||
return false, nil | ||
} | ||
index := group.InputIndices[0] | ||
// set the witness placeholder | ||
if err := builder.SetWitness(uint(index), types.WitnessTypeLock, r.WitnessPlaceholder); err != nil { | ||
return false, err | ||
} | ||
// CkbTransactionBuilder.AddCellDep will remove duplications automatically. | ||
builder.AddCellDep(r.CellDep) | ||
return true, nil | ||
} | ||
|
||
func ExampleScriptHandler() { | ||
txHash := "0x1234" | ||
typeScriptHash := "0xabcd" | ||
|
||
s := builder.SimpleTransactionBuilder{} | ||
s.Register(&SimpleLockScriptHandler{ | ||
CellDep: &types.CellDep{ | ||
OutPoint: &types.OutPoint{ | ||
TxHash: types.HexToHash(txHash), | ||
Index: 0, | ||
}, | ||
DepType: types.DepTypeCode, | ||
}, | ||
CodeHash: types.HexToHash(typeScriptHash), | ||
WitnessPlaceholder: make([]byte, 8), | ||
}) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,84 @@ | ||
package signer_test | ||
|
||
import ( | ||
"context" | ||
"encoding/binary" | ||
"reflect" | ||
|
||
"github.com/nervosnetwork/ckb-sdk-go/v2/rpc" | ||
"github.com/nervosnetwork/ckb-sdk-go/v2/transaction" | ||
"github.com/nervosnetwork/ckb-sdk-go/v2/transaction/signer" | ||
"github.com/nervosnetwork/ckb-sdk-go/v2/types" | ||
) | ||
|
||
type CapacityDiffContext struct { | ||
rpc rpc.Client | ||
ctx context.Context | ||
} | ||
|
||
func (ctx CapacityDiffContext) getInputCell(outPoint *types.OutPoint) (*types.CellOutput, error) { | ||
cellWithStatus, err := ctx.rpc.GetLiveCell(ctx.ctx, outPoint, false) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
return cellWithStatus.Cell.Output, nil | ||
} | ||
|
||
type CapacityDiffScriptSigner struct{} | ||
|
||
func (s *CapacityDiffScriptSigner) SignTransaction(tx *types.Transaction, group *transaction.ScriptGroup, ctx *transaction.Context) (bool, error) { | ||
scriptContext, ok := ctx.Payload.(CapacityDiffContext) | ||
if !ok { | ||
return false, nil | ||
} | ||
|
||
total := int64(0) | ||
for _, i := range group.InputIndices { | ||
inputCell, err := scriptContext.getInputCell(tx.Inputs[i].PreviousOutput) | ||
if err != nil { | ||
return false, nil | ||
} | ||
total -= int64(inputCell.Capacity) | ||
} | ||
for _, output := range tx.Outputs { | ||
if reflect.DeepEqual(output.Lock, group.Script) { | ||
total += int64(output.Capacity) | ||
} | ||
} | ||
|
||
// The specification https://go.dev/ref/spec#Numeric_types says integres in | ||
// Go are repsented using two's complementation. So we can just cast it to | ||
// uin64 and get the little endian bytes. | ||
witness := make([]byte, 8) | ||
binary.LittleEndian.PutUint64(witness, uint64(total)) | ||
|
||
witnessIndex := group.InputIndices[0] | ||
witnessArgs, err := types.DeserializeWitnessArgs(tx.Witnesses[witnessIndex]) | ||
if err != nil { | ||
return false, err | ||
} | ||
witnessArgs.Lock = witness | ||
tx.Witnesses[witnessIndex] = witnessArgs.Serialize() | ||
|
||
return true, nil | ||
} | ||
|
||
// This example demonstrates how to use a custom script CapacityDiff | ||
// (https://github.com/doitian/ckb-sdk-examples-capacity-diff). | ||
// | ||
// CapacityDiff verifies the witness matches the capacity difference. | ||
// | ||
// - The script loads the witness for the first input in the script group using the WitnessArgs layout. | ||
// - The total input capacity is the sum of all the input cells in the script group. | ||
// - The total output capacity is the sum of all the output cells having the same lock script as the script group. | ||
// - The capacity difference is a 64-bit signed integer which equals to total output capacity minus total input capacity. | ||
// - The witness is encoded using two's complement and little endian. | ||
func ExampleScriptSigner() { | ||
signer := signer.NewTransactionSigner() | ||
signer.RegisterSigner( | ||
types.HexToHash("0x6283a479a3cf5d4276cd93594de9f1827ab9b55c7b05b3d28e4c2e0a696cfefd"), | ||
types.ScriptTypeType, | ||
&CapacityDiffScriptSigner{}, | ||
) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
// Package transaction implements CKB transaction signing. | ||
package transaction |