Skip to content

Commit

Permalink
Extrinics V5 support
Browse files Browse the repository at this point in the history
  • Loading branch information
freehere107 committed Nov 1, 2024
1 parent 5d3fdc1 commit 13b21b3
Show file tree
Hide file tree
Showing 2 changed files with 79 additions and 17 deletions.
92 changes: 75 additions & 17 deletions extrinsic.go
Original file line number Diff line number Diff line change
Expand Up @@ -81,23 +81,24 @@ func (e *ExtrinsicDecoder) generateHash() string {
}

type GenericExtrinsic struct {
VersionInfo string `json:"version_info"`
ExtrinsicLength int `json:"extrinsic_length"`
AddressType string `json:"address_type"`
Tip decimal.Decimal `json:"tip"`
SignedExtensions map[string]interface{} `json:"signed_extensions"`
AccountId interface{} `json:"account_id"`
Signer interface{} `json:"signer"` // map[string]interface or string
Signature string `json:"signature"`
SignatureRaw interface{} `json:"signature_raw"` // map[string]interface or string
Nonce int `json:"nonce"`
Era string `json:"era"`
ExtrinsicHash string `json:"extrinsic_hash"`
CallModuleFunction string `json:"call_module_function"`
CallCode string `json:"call_code"`
CallModule string `json:"call_module"`
Params []ExtrinsicParam `json:"params"`
ParamsRaw string `json:"params_raw"`
VersionInfo string `json:"version_info"`
ExtrinsicLength int `json:"extrinsic_length"`
AddressType string `json:"address_type"`
Tip decimal.Decimal `json:"tip"`
SignedExtensions map[string]interface{} `json:"signed_extensions"`
AccountId interface{} `json:"account_id"`
Signer interface{} `json:"signer"` // map[string]interface or string
Signature string `json:"signature"`
SignatureRaw interface{} `json:"signature_raw"` // map[string]interface or string
Nonce int `json:"nonce"`
Era string `json:"era"`
ExtrinsicHash string `json:"extrinsic_hash"`
CallModuleFunction string `json:"call_module_function"`
CallCode string `json:"call_code"`
CallModule string `json:"call_module"`
Params []ExtrinsicParam `json:"params"`
ParamsRaw string `json:"params_raw"`
TransactionExtensionVersion int `json:"transaction_extension_version"`
}

func (e *ExtrinsicDecoder) Process() {
Expand Down Expand Up @@ -171,6 +172,63 @@ func (e *ExtrinsicDecoder) Process() {
e.ExtrinsicHash = e.generateHash()
}
e.CallIndex = utiles.BytesToHex(e.NextBytes(2))
} else if e.VersionInfo == "05" || e.VersionInfo == "85" {
if e.ContainsTransaction {
// Address
result.Signer = e.ProcessAndUpdateData(utiles.TrueOrElse(e.Metadata.MetadataVersion >= 14 && scaleType.HasReg("ExtrinsicSigner"), "ExtrinsicSigner", "Address"))
switch v := result.Signer.(type) {
case string:
e.Address = v
result.AddressType = "AccountId"
case map[string]interface{}:
for name, value := range v {
result.AddressType = name
e.Address = value
}
}
// ExtrinsicSignature
result.SignatureRaw = e.ProcessAndUpdateData("ExtrinsicSignature")
result.TransactionExtensionVersion = e.ProcessAndUpdateData("U8").(int)
switch v := result.SignatureRaw.(type) {
case string:
e.Signature = v
case map[string]interface{}:
for _, value := range v {
e.Signature = value.(string)
}
}
e.Era = e.ProcessAndUpdateData("EraExtrinsic").(string)
e.Nonce = int(e.ProcessAndUpdateData("Compact<U64>").(uint64))

if e.Metadata.Extrinsic != nil {
if utiles.SliceIndex("ChargeTransactionPayment", e.Metadata.Extrinsic.SignedIdentifier) != -1 {
result.Tip = utiles.DecimalFromInterface(e.ProcessAndUpdateData("Compact<Balance>"))
}
} else {
result.Tip = utiles.DecimalFromInterface(e.ProcessAndUpdateData("Compact<Balance>"))
}
// spec SignedExtensions
result.SignedExtensions = make(map[string]interface{})
if len(e.SignedExtensions) > 0 {
for _, extension := range e.SignedExtensions {
if utiles.SliceIndex(extension.Name, e.Metadata.Extrinsic.SignedIdentifier) != -1 {
for _, v := range extension.AdditionalSigned {
result.SignedExtensions[v.Name] = e.ProcessAndUpdateData(v.Type)
}
}
}
} else {
if e.Metadata.MetadataVersion >= 14 {
for _, ext := range e.Metadata.Extrinsic.SignedExtensions {
if enable := signedExts[ext.Identifier]; enable || utiles.SliceIndex(ext.Identifier, e.AdditionalCheck) >= 0 {
result.SignedExtensions[ext.Identifier] = e.ProcessAndUpdateData(ext.TypeString)
}
}
}
}
e.ExtrinsicHash = e.generateHash()
}
e.CallIndex = utiles.BytesToHex(e.NextBytes(2))
} else {
panic(fmt.Sprintf("Extrinsics version %s is not support", e.VersionInfo))
}
Expand Down
4 changes: 4 additions & 0 deletions metadata.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,10 @@ func (m *MetadataDecoder) Init(data []byte) {

func (m *MetadataDecoder) Process() error {
magicBytes := m.NextBytes(4)
// if string(magicBytes) != "meta" {
// m.NextBytes(1) // maybe option<metadata> ?
// magicBytes = m.NextBytes(4)
// }
if string(magicBytes) == "meta" {
metadataVersion := utiles.U256(utiles.BytesToHex(m.Data.Data[m.Data.Offset : m.Data.Offset+1]))
m.Version = m.ProcessAndUpdateData("MetadataVersion").(string)
Expand Down

0 comments on commit 13b21b3

Please sign in to comment.