Skip to content

Commit

Permalink
big boom
Browse files Browse the repository at this point in the history
  • Loading branch information
xuhongxin committed Oct 8, 2018
1 parent 52fc2db commit 29d0429
Show file tree
Hide file tree
Showing 256 changed files with 37,938 additions and 2 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
/.idea
main.wasm
10 changes: 8 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,2 +1,8 @@
# Bytom-WebAssembly-
It is a project for Bytom WebAssembly
# Bytom-WebAssembly
It is a project for Bytom WebAssembly

## build
```sh
govendor sync
GOOS=js GOARCH=wasm go build -o main.wasm
```
71 changes: 71 additions & 0 deletions account/accounts.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
// Package account stores and tracks accounts within a Bytom Core.
package account

import (
"encoding/hex"
"errors"

"github.com/bytom-community/wasm/blockchain/signers"
"github.com/bytom-community/wasm/common"
)

const (
maxAccountCache = 1000
)

var (
accountIndexKey = []byte("AccountIndex")
accountPrefix = []byte("Account:")
aliasPrefix = []byte("AccountAlias:")
contractIndexPrefix = []byte("ContractIndex")
contractPrefix = []byte("Contract:")
)

// pre-define errors for supporting bytom errorFormatter
var (
ErrDuplicateAlias = errors.New("duplicate account alias")
ErrFindAccount = errors.New("fail to find account")
ErrMarshalAccount = errors.New("failed marshal account")
ErrInvalidAddress = errors.New("invalid address")
ErrFindCtrlProgram = errors.New("fail to find account control program")
)

// ContractKey account control promgram store prefix
func ContractKey(hash common.Hash) []byte {
return append(contractPrefix, hash[:]...)
}

// ContractKeyHexString hash hex string
func ContractKeyHexString(hash common.Hash) string {
str := hex.EncodeToString(hash[:])
return string(append(contractPrefix, []byte(str)...))
}

// Key account store prefix
func Key(name string) []byte {
return append(accountPrefix, []byte(name)...)
}

func aliasKey(name string) []byte {
return append(aliasPrefix, []byte(name)...)
}

func contractIndexKey(accountID string) []byte {
return append(contractIndexPrefix, []byte(accountID)...)
}

// Account is structure of Bytom account
type Account struct {
*signers.Signer
ID string `json:"id"`
Alias string `json:"alias"`
}

//CtrlProgram is structure of account control program
type CtrlProgram struct {
AccountID string
Address string
KeyIndex uint64
ControlProgram []byte
Change bool // Mark whether this control program is for UTXO change
}
16 changes: 16 additions & 0 deletions account/indexer.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
package account

import (
"github.com/bytom-community/wasm/blockchain/query"
)

//Annotated init an annotated account object
func Annotated(a *Account) *query.AnnotatedAccount {
return &query.AnnotatedAccount{
ID: a.ID,
Alias: a.Alias,
Quorum: a.Quorum,
XPubs: a.XPubs,
KeyIndex: a.KeyIndex,
}
}
112 changes: 112 additions & 0 deletions blockchain/pseudohsm/key.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,112 @@
package pseudohsm

import (
"fmt"
"io/ioutil"
"os"
"path/filepath"
"time"

"github.com/bytom-community/wasm/crypto/ed25519/chainkd"
"github.com/pborman/uuid"
)

const (
version = 1
keytype = "bytom_kd"
)

// XKey struct type for keystore file
type XKey struct {
ID uuid.UUID
KeyType string
Alias string
XPrv chainkd.XPrv
XPub chainkd.XPub
}

type keyStore interface {
// Loads and decrypts the key from disk.
GetKey(alias string, filename string, auth string) (*XKey, error)
// Writes and encrypts the key.
StoreKey(filename string, k *XKey, auth string) error
// Joins filename with the key directory unless it is already absolute.
JoinPath(filename string) string
}

type encryptedKeyJSON struct {
Crypto cryptoJSON `json:"crypto"`
ID string `json:"id"`
Type string `json:"type"`
Version int `json:"version"`
Alias string `json:"alias"`
XPub string `json:"xpub"`
}

type cryptoJSON struct {
Cipher string `json:"cipher"`
CipherText string `json:"ciphertext"`
CipherParams cipherparamsJSON `json:"cipherparams"`
KDF string `json:"kdf"`
KDFParams map[string]interface{} `json:"kdfparams"`
MAC string `json:"mac"`
}

type cipherparamsJSON struct {
IV string `json:"iv"`
}

type scryptParamsJSON struct {
N int `json:"n"`
R int `json:"r"`
P int `json:"p"`
DkLen int `json:"dklen"`
Salt string `json:"salt"`
}

func writeKeyFile(file string, content []byte) error {
// Create the keystore directory with appropriate permissions
// in case it is not present yet.
const dirPerm = 0700
if err := os.MkdirAll(filepath.Dir(file), dirPerm); err != nil {
return err
}
// Atomic write: create a temporary hidden file first
// then move it into place. TempFile assigns mode 0600.
f, err := ioutil.TempFile(filepath.Dir(file), "."+filepath.Base(file)+".tmp")
if err != nil {
return err
}
if _, err := f.Write(content); err != nil {
f.Close()
os.Remove(f.Name())
return err
}
f.Close()
return os.Rename(f.Name(), file)
}

func zeroKey(k *XKey) {
b := k.XPrv
for i := range b {
b[i] = 0
}
}

// keyFileName implements the naming convention for keyfiles:
// UTC--<created_at UTC ISO8601>-<address hex>
func keyFileName(keyId string) string {
ts := time.Now().UTC()
return fmt.Sprintf("UTC--%s--%s", toISO8601(ts), keyId)
}

func toISO8601(t time.Time) string {
var tz string
name, offset := t.Zone()
if name == "UTC" {
tz = "Z"
} else {
tz = fmt.Sprintf("%03d00", offset/3600)
}
return fmt.Sprintf("%04d-%02d-%02dT%02d-%02d-%02d.%09d%s", t.Year(), t.Month(), t.Day(), t.Hour(), t.Minute(), t.Second(), t.Nanosecond(), tz)
}
Loading

0 comments on commit 29d0429

Please sign in to comment.