"github.com/bytom/blockchain/signers"
"github.com/bytom/common"
"github.com/bytom/consensus"
+ "github.com/bytom/consensus/segwit"
"github.com/bytom/crypto/sha3pool"
"github.com/bytom/protocol/bc"
"github.com/bytom/protocol/bc/types"
}
func (w *Wallet) getExternalDefinition(assetID *bc.AssetID) json.RawMessage {
- definitionByte := w.DB.Get(asset.CalcExtAssetKey(assetID))
+ definitionByte := w.DB.Get(asset.ExtAssetKey(assetID))
if definitionByte == nil {
return nil
}
return nil
}
- saveAlias := assetID.String()
- storeBatch := w.DB.NewBatch()
-
- externalAsset := &asset.Asset{AssetID: *assetID, Alias: &saveAlias, DefinitionMap: definitionMap, Signer: &signers.Signer{Type: "external"}}
- if rawAsset, err := json.Marshal(externalAsset); err == nil {
- log.WithFields(log.Fields{"assetID": assetID.String(), "alias": saveAlias}).Info("index external asset")
- storeBatch.Set(asset.Key(assetID), rawAsset)
+ alias := assetID.String()
+ externalAsset := &asset.Asset{
+ AssetID: *assetID,
+ Alias: &alias,
+ DefinitionMap: definitionMap,
+ RawDefinitionByte: definitionByte,
+ Signer: &signers.Signer{Type: "external"},
}
- storeBatch.Set(asset.AliasKey(saveAlias), []byte(assetID.String()))
- storeBatch.Write()
+ if err := w.AssetReg.SaveAsset(externalAsset, alias); err != nil {
+ log.WithFields(log.Fields{"err": err, "assetID": alias}).Warning("fail on save external asset to internal asset DB")
+ }
return definitionByte
}
sha3pool.Sum256(hash[:], program)
- rawProgram := walletDB.Get(account.CPKey(hash))
+ rawProgram := walletDB.Get(account.ContractKey(hash))
if rawProgram == nil {
return nil, fmt.Errorf("failed get account control program:%x ", hash)
}
return err == nil
}
-func buildAnnotatedTransaction(orig *types.Tx, b *types.Block, statusFail bool, indexInBlock int) *query.AnnotatedTx {
+func (w *Wallet) buildAnnotatedTransaction(orig *types.Tx, b *types.Block, statusFail bool, indexInBlock int) *query.AnnotatedTx {
tx := &query.AnnotatedTx{
ID: orig.ID,
Timestamp: b.Timestamp,
Inputs: make([]*query.AnnotatedInput, 0, len(orig.Inputs)),
Outputs: make([]*query.AnnotatedOutput, 0, len(orig.Outputs)),
StatusFail: statusFail,
+ Size: orig.SerializedSize,
}
for i := range orig.Inputs {
- tx.Inputs = append(tx.Inputs, BuildAnnotatedInput(orig, uint32(i)))
+ tx.Inputs = append(tx.Inputs, w.BuildAnnotatedInput(orig, uint32(i)))
}
for i := range orig.Outputs {
- tx.Outputs = append(tx.Outputs, BuildAnnotatedOutput(orig, i))
+ tx.Outputs = append(tx.Outputs, w.BuildAnnotatedOutput(orig, i))
}
return tx
}
// BuildAnnotatedInput build the annotated input.
-func BuildAnnotatedInput(tx *types.Tx, i uint32) *query.AnnotatedInput {
+func (w *Wallet) BuildAnnotatedInput(tx *types.Tx, i uint32) *query.AnnotatedInput {
orig := tx.Inputs[i]
in := &query.AnnotatedInput{
AssetDefinition: &emptyJSONObject,
}
id := tx.Tx.InputIDs[i]
+ in.InputID = id
e := tx.Entries[id]
switch e := e.(type) {
case *bc.Spend:
in.Type = "spend"
in.ControlProgram = orig.ControlProgram()
+ in.Address = w.getAddressFromControlProgram(in.ControlProgram)
in.SpentOutputID = e.SpentOutputId
case *bc.Issuance:
in.Type = "issue"
return in
}
+func (w *Wallet) getAddressFromControlProgram(prog []byte) string {
+ if segwit.IsP2WPKHScript(prog) {
+ if pubHash, err := segwit.GetHashFromStandardProg(prog); err == nil {
+ return buildP2PKHAddress(pubHash)
+ }
+ } else if segwit.IsP2WSHScript(prog) {
+ if scriptHash, err := segwit.GetHashFromStandardProg(prog); err == nil {
+ return buildP2SHAddress(scriptHash)
+ }
+ }
+
+ return ""
+}
+
+func buildP2PKHAddress(pubHash []byte) string {
+ address, err := common.NewAddressWitnessPubKeyHash(pubHash, &consensus.ActiveNetParams)
+ if err != nil {
+ return ""
+ }
+
+ return address.EncodeAddress()
+}
+
+func buildP2SHAddress(scriptHash []byte) string {
+ address, err := common.NewAddressWitnessScriptHash(scriptHash, &consensus.ActiveNetParams)
+ if err != nil {
+ return ""
+ }
+
+ return address.EncodeAddress()
+}
+
// BuildAnnotatedOutput build the annotated output.
-func BuildAnnotatedOutput(tx *types.Tx, idx int) *query.AnnotatedOutput {
+func (w *Wallet) BuildAnnotatedOutput(tx *types.Tx, idx int) *query.AnnotatedOutput {
orig := tx.Outputs[idx]
outid := tx.OutputID(idx)
out := &query.AnnotatedOutput{
AssetDefinition: &emptyJSONObject,
Amount: orig.Amount,
ControlProgram: orig.ControlProgram,
+ Address: w.getAddressFromControlProgram(orig.ControlProgram),
}
+
if vmutil.IsUnspendable(out.ControlProgram) {
out.Type = "retire"
} else {