Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 20 additions & 9 deletions extrinsic.go
Original file line number Diff line number Diff line change
Expand Up @@ -62,22 +62,32 @@ func (e *ExtrinsicDecoder) Init(data scaleBytes.ScaleBytes, option *scaleType.Sc
e.ScaleDecoder.Init(data, option)
}

func blake2_256(data []byte) string {
checksum, _ := blake2b.New(32, []byte{})
_, _ = checksum.Write(data)
h := checksum.Sum(nil)
return utiles.BytesToHex(h)
}

func (e *ExtrinsicDecoder) generateHash() string {
if !e.ContainsTransaction {
return ""
}
var extrinsicData []byte
if e.ExtrinsicLength > 0 {
if e.VersionInfo == "45" && !e.ContainsTransaction {
// 69 denotes 0b0100_0101 which is the version and preamble for this Extrinsic
// General transactions: (extrinsic_encoded_len, 0b0100_0101, extension_version_byte, extensions, call)
// for version 45, will add version info as a prefix and add extrinsic length
// https://github.com/polkadot-fellows/RFCs/pull/124
extrinsicLengthType := scaleType.CompactU32{}
extrinsicData = append([]byte{0x45}, e.Data.Data...) // add version info
extrinsicLengthType.Encode(len(extrinsicData))
extrinsicData = append(extrinsicLengthType.Data.Data[:], extrinsicData...)
} else if e.ExtrinsicLength > 0 {
extrinsicData = e.Data.Data
} else {
extrinsicLengthType := scaleType.CompactU32{}
extrinsicLengthType.Encode(len(e.Data.Data))
extrinsicData = append(extrinsicLengthType.Data.Data[:], e.Data.Data[:]...)
}
checksum, _ := blake2b.New(32, []byte{})
_, _ = checksum.Write(extrinsicData)
h := checksum.Sum(nil)
return utiles.BytesToHex(h)
return blake2_256(extrinsicData)
}

type GenericExtrinsic struct {
Expand Down Expand Up @@ -218,6 +228,7 @@ func (e *ExtrinsicDecoder) Process() {
e.CallIndex = utiles.BytesToHex(e.NextBytes(2))
} else if e.VersionInfo == "45" {
e.NextBytes(4)
e.ExtrinsicHash = e.generateHash()
e.CallIndex = utiles.BytesToHex(e.NextBytes(2))
} else {
panic(fmt.Sprintf("Extrinsics version %s is not support", e.VersionInfo))
Expand All @@ -242,8 +253,8 @@ func (e *ExtrinsicDecoder) Process() {
result.Signature = e.Signature
result.Nonce = e.Nonce
result.Era = e.Era
result.ExtrinsicHash = e.ExtrinsicHash
}
result.ExtrinsicHash = utiles.AddHex(e.ExtrinsicHash)
result.CallCode = e.CallIndex
result.CallModuleFunction = call.Call.Name
result.CallModule = call.Module.Name
Expand Down
3 changes: 3 additions & 0 deletions utiles/tools.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,9 @@ func IntInSlice(a int, list []int) bool {
return false
}
func AddHex(s string) string {
if s == "" {
return ""
}
if strings.HasPrefix(s, "0x") {
return s
}
Expand Down