This repository has been archived by the owner on Jan 25, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 16
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
25 changed files
with
1,088 additions
and
80 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
package neoutils_test | ||
|
||
import ( | ||
"bytes" | ||
"log" | ||
"testing" | ||
|
||
"github.com/o3labs/neo-utils/neoutils" | ||
"github.com/o3labs/neo-utils/neoutils/smartcontract" | ||
) | ||
|
||
func TestDeploy(t *testing.T) { | ||
|
||
contract := neoutils.SmartContractInfo{ | ||
AVMHEX: "54c56b6c766b00527ac46c766b51527ac46168164e656f2e52756e74696d652e47657454726967676572635e0061149ba59c6b61d40aecf939c72cf1b6d21bba346dc2c00114907c907c9e63380061149ba59c6b61d40aecf939c72cf1b6d21bba346dc26168184e656f2e52756e74696d652e436865636b5769746e657373616c756600616c75666168164e656f2e52756e74696d652e4765745472696767657260907c907c9e630b016c766b00c3066465706c6f7987640b0061652501616c75666c766b00c30b746f74616c537570706c7987640b0061655b02616c75666c766b00c3046e616d6587640b006165c900616c75666c766b00c30673796d626f6c87640b006165c700616c75666c766b00c3087472616e7366657287644a006c766b51c3c0539c63080000616c75666c766b51c300c36c766b51c351c36c766b52527ac46c766b51c352c36c766b53527ac46c766b52c36c766b53c3615272651302616c75666c766b00c30962616c616e63654f66876422006c766b51c3c0519c63080000616c75666c766b51c300c36165d103616c75666c766b00c308646563696d616c7387640b0061652d00616c756600616c756600c56b0e5374656d2043656c6c20436f696e616c756600c56b03534343616c756600c56b58616c756600c56b6114f106089975d5964c41b5c80136814257665312326168184e656f2e52756e74696d652e436865636b5769746e65737363080000616c75666168164e656f2e53746f726167652e476574436f6e746578740b746f74616c537570706c79617c680f4e656f2e53746f726167652e476574c064080000616c75666168164e656f2e53746f726167652e476574436f6e7465787461149ba59c6b61d40aecf939c72cf1b6d21bba346dc2080000b2d3595bf006615272680f4e656f2e53746f726167652e5075746168164e656f2e53746f726167652e476574436f6e746578740b746f74616c537570706c79080000b2d3595bf006615272680f4e656f2e53746f726167652e507574610061149ba59c6b61d40aecf939c72cf1b6d21bba346dc2080000b2d3595bf006615272087472616e7366657254c168124e656f2e52756e74696d652e4e6f7469667951616c756600c56b6168164e656f2e53746f726167652e476574436f6e746578740b746f74616c537570706c79617c680f4e656f2e53746f726167652e476574616c756655c56b6c766b00527ac46c766b51527ac46c766b52527ac46c766b52c300a164080000616c75666c766b00c36168184e656f2e52756e74696d652e436865636b5769746e65737363080000616c75666c766b51c3c001149c63080000616c75666168164e656f2e53746f726167652e476574436f6e746578746c766b00c3617c680f4e656f2e53746f726167652e4765746c766b53527ac46c766b53c36c766b52c39f64080000616c75666c766b00c36c766b51c3907c907c9e63080051616c75666c766b53c36c766b52c39c643a006168164e656f2e53746f726167652e476574436f6e746578746c766b00c3617c68124e656f2e53746f726167652e44656c6574656240006168164e656f2e53746f726167652e476574436f6e746578746c766b00c36c766b53c36c766b52c394615272680f4e656f2e53746f726167652e5075746168164e656f2e53746f726167652e476574436f6e746578746c766b51c3617c680f4e656f2e53746f726167652e4765746c766b54527ac46168164e656f2e53746f726167652e476574436f6e746578746c766b51c36c766b54c36c766b52c393615272680f4e656f2e53746f726167652e507574616c766b00c36c766b51c36c766b52c3615272087472616e7366657254c168124e656f2e52756e74696d652e4e6f7469667951616c756651c56b6c766b00527ac46168164e656f2e53746f726167652e476574436f6e746578746c766b00c3617c680f4e656f2e53746f726167652e476574616c7566", | ||
Name: "Stem Cell Coin", | ||
Version: "1.0", | ||
Author: "MTA Intec PTE LTD", | ||
Email: "[email protected]", | ||
Description: "SCC Token - https://www.stemcell-pj.net/", | ||
InputTypes: []smartcontract.ParameterType{smartcontract.String, smartcontract.Array}, | ||
ReturnType: smartcontract.ByteArray, | ||
Properties: smartcontract.HasStorage + smartcontract.Payable, | ||
} | ||
|
||
log.Printf("sc hash %v", contract.GetScriptHash()) | ||
|
||
asset := smartcontract.GAS | ||
amount := float64(490) | ||
|
||
encryptedKey := "" | ||
passphrase := "" | ||
wif, _ := neoutils.NEP2Decrypt(encryptedKey, passphrase) | ||
|
||
privateNetwallet, err := neoutils.GenerateFromWIF(wif) | ||
if err != nil { | ||
log.Printf("%v", err) | ||
t.Fail() | ||
return | ||
} | ||
log.Printf("wallet address %v", privateNetwallet.Address) | ||
|
||
unspent, err := utxo("main", privateNetwallet.Address) | ||
log.Printf("unspent %+v", unspent) | ||
if err != nil { | ||
log.Printf("error %v", err) | ||
t.Fail() | ||
return | ||
} | ||
attributes := map[smartcontract.TransactionAttribute][]byte{} | ||
tx, err := neoutils.DeploySmartContractScript(contract, *privateNetwallet, asset, amount, unspent, attributes) | ||
if err != nil { | ||
log.Printf("error %v", err) | ||
return | ||
} | ||
log.Printf("tx %x", tx) | ||
|
||
} | ||
|
||
func TestVarInt(t *testing.T) { | ||
buff := new(bytes.Buffer) | ||
neoutils.WriteVarUint(buff, uint64(286)) | ||
log.Printf("%x", buff.Bytes()) | ||
} |
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,135 @@ | ||
package neoutils | ||
|
||
import ( | ||
"bytes" | ||
"encoding/binary" | ||
"io" | ||
"log" | ||
|
||
"github.com/o3labs/neo-utils/neoutils/smartcontract" | ||
) | ||
|
||
func WriteVarUint(w io.Writer, val uint64) error { | ||
if val < 0xfd { | ||
binary.Write(w, binary.LittleEndian, uint8(val)) | ||
return nil | ||
} | ||
if val < 0xFFFF { | ||
binary.Write(w, binary.LittleEndian, byte(0xfd)) | ||
binary.Write(w, binary.LittleEndian, uint16(val)) | ||
return nil | ||
} | ||
if val < 0xFFFFFFFF { | ||
binary.Write(w, binary.LittleEndian, byte(0xfe)) | ||
binary.Write(w, binary.LittleEndian, uint32(val)) | ||
return nil | ||
} | ||
|
||
binary.Write(w, binary.LittleEndian, byte(0xff)) | ||
binary.Write(w, binary.LittleEndian, val) | ||
|
||
return nil | ||
} | ||
|
||
type SmartContractInfo struct { | ||
AVMHEX string | ||
Name string | ||
Version string | ||
Author string | ||
Email string | ||
Description string | ||
Properties smartcontract.Properties | ||
InputTypes []smartcontract.ParameterType | ||
ReturnType smartcontract.ParameterType | ||
} | ||
|
||
func (s *SmartContractInfo) GetScriptHash() string { | ||
address := VMCodeToNEOAddress(hex2bytes(s.AVMHEX)) | ||
scripthash := NEOAddressToScriptHashWithEndian(address, binary.BigEndian) | ||
return scripthash | ||
} | ||
|
||
func (s *SmartContractInfo) Serialize() []byte { | ||
|
||
params := []byte{} | ||
for _, p := range s.InputTypes { | ||
params = append(params, p.Byte()) | ||
} | ||
|
||
scriptBuilder := smartcontract.NewScriptBuilder() | ||
scriptBuilder.Push([]byte(s.Description)) | ||
scriptBuilder.Push([]byte(s.Email)) | ||
scriptBuilder.Push([]byte(s.Author)) | ||
scriptBuilder.Push([]byte(s.Version)) | ||
scriptBuilder.Push([]byte(s.Name)) | ||
scriptBuilder.Push(int(s.Properties)) | ||
scriptBuilder.Push([]byte{s.ReturnType.Byte()}) | ||
scriptBuilder.Push(params) | ||
scriptBuilder.PushVarData(hex2bytes(s.AVMHEX)) | ||
scriptBuilder.PushSysCall("Neo.Contract.Create") | ||
|
||
b := scriptBuilder.ToBytes() | ||
buff := new(bytes.Buffer) | ||
WriteVarUint(buff, uint64(len(b))) | ||
endPayload := []byte{} | ||
endPayload = append(endPayload, buff.Bytes()...) | ||
endPayload = append(endPayload, b...) | ||
return endPayload | ||
} | ||
|
||
func DeploySmartContractScript(contractInfo SmartContractInfo, wallet Wallet, asset smartcontract.NativeAsset, amount float64, unspent smartcontract.Unspent, attributes map[smartcontract.TransactionAttribute][]byte) ([]byte, error) { | ||
|
||
tx := smartcontract.NewInvocationTransactionPayable() | ||
|
||
tx.Data = contractInfo.Serialize() | ||
tx.GAS = uint64(490) | ||
|
||
amountToSend := amount | ||
assetToSend := asset | ||
|
||
networkFee := smartcontract.NetworkFeeAmount(0) | ||
|
||
txInputs, err := smartcontract.NewScriptBuilder().GenerateTransactionInput(unspent, assetToSend, amountToSend, networkFee) | ||
if err != nil { | ||
return nil, err | ||
} | ||
tx.Inputs = txInputs | ||
|
||
txAttributes, err := smartcontract.NewScriptBuilder().GenerateTransactionAttributes(attributes) | ||
if err != nil { | ||
return nil, err | ||
} | ||
tx.Attributes = txAttributes | ||
|
||
//when deploy smart contract, you don't actually send asset to another address | ||
//so the receiver is the same address | ||
sender := smartcontract.ParseNEOAddress(wallet.Address) | ||
receiver := smartcontract.ParseNEOAddress(wallet.Address) | ||
txOutputs, err := smartcontract.NewScriptBuilder().GenerateTransactionOutputPayableGAS(sender, receiver, unspent, assetToSend, amount, networkFee, float64(tx.GAS)) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
tx.Outputs = txOutputs | ||
|
||
//begin signing process and invocation script | ||
privateKeyInHex := bytesToHex(wallet.PrivateKey) | ||
signedData, err := Sign(tx.ToBytes(), privateKeyInHex) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
signature := smartcontract.TransactionSignature{ | ||
SignedData: signedData, | ||
PublicKey: wallet.PublicKey, | ||
} | ||
|
||
scripts := []interface{}{signature} | ||
txScripts := smartcontract.NewScriptBuilder().GenerateVerificationScripts(scripts) | ||
tx.Script = txScripts | ||
//end signing process | ||
|
||
log.Printf("txid = %v", tx.ToTXID()) | ||
|
||
return tx.ToBytes(), nil | ||
} |
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
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
Oops, something went wrong.