Skip to content

Commit

Permalink
Merge pull request #5 from klever-io/create-offline-payload
Browse files Browse the repository at this point in the history
new method to create a detached payload
  • Loading branch information
brunocampos-ssa authored Nov 8, 2024
2 parents da62cae + dbd351f commit b251a85
Showing 1 changed file with 31 additions and 23 deletions.
54 changes: 31 additions & 23 deletions types/extrinsic/extrinsic.go
Original file line number Diff line number Diff line change
Expand Up @@ -78,34 +78,12 @@ func (e Extrinsic) Type() uint8 {

// Sign adds a signature to the extrinsic.
func (e *Extrinsic) Sign(signer signature.KeyringPair, meta *types.Metadata, opts ...SigningOption) error {
if e.Type() != Version4 {
//nolint:lll
return ErrInvalidVersion.WithMsg("unsupported extrinsic version: %v (isSigned: %v, type: %v)", e.Version, e.IsSigned(), e.Type())
}

encodedMethod, err := codec.Encode(e.Method)
if err != nil {
return ErrScaleEncode.Wrap(err)
}

fieldValues := SignedFieldValues{}

for _, opt := range opts {
opt(fieldValues)
}

payload, err := createPayload(meta, encodedMethod)

payload, err := e.NewPayload(meta, opts...)
if err != nil {
return ErrPayloadCreation.Wrap(err)
}

if err := payload.MutateSignedFields(fieldValues); err != nil {
return ErrPayloadMutation.Wrap(err)
}

signerPubKey, err := types.NewMultiAddressFromAccountID(signer.PublicKey)

if err != nil {
return ErrMultiAddressCreation.Wrap(err)
}
Expand All @@ -129,6 +107,36 @@ func (e *Extrinsic) Sign(signer signature.KeyringPair, meta *types.Metadata, opt
return nil
}

func (e *Extrinsic) NewPayload(meta *types.Metadata, opts ...SigningOption) (*Payload, error) {
if e.Type() != Version4 {
//nolint:lll
return nil, ErrInvalidVersion.WithMsg("unsupported extrinsic version: %v (isSigned: %v, type: %v)", e.Version, e.IsSigned(), e.Type())
}

encodedMethod, err := codec.Encode(e.Method)
if err != nil {
return nil, ErrScaleEncode.Wrap(err)
}

fieldValues := SignedFieldValues{}

for _, opt := range opts {
opt(fieldValues)
}

payload, err := createPayload(meta, encodedMethod)

if err != nil {
return nil, ErrPayloadCreation.Wrap(err)
}

if err := payload.MutateSignedFields(fieldValues); err != nil {
return nil, ErrPayloadMutation.Wrap(err)
}

return payload, nil
}

func (e Extrinsic) Encode(encoder scale.Encoder) error {
if e.Type() != Version4 {
return fmt.Errorf("unsupported extrinsic version: %v (isSigned: %v, type: %v)", e.Version, e.IsSigned(),
Expand Down

0 comments on commit b251a85

Please sign in to comment.