Esempio n. 1
0
func (db *T) PutKey(alias string, key *ecdsa.PrivateKey, main bool) (e error) {
	writable(&e, db, func(dbtx *bolt.Tx) bool {
		bucket, e := dbtx.CreateBucketIfNotExists([]byte("keys"))
		if e != nil {
			return false
		}

		encodedKey, e := keys.EncodeECDSAPrivateKey(key)
		if e != nil {
			return false
		}

		e = bucket.Put([]byte(alias), encodedKey)

		if e != nil {
			return false
		}
		if main {
			e = bucket.Put([]byte("main"), []byte(alias))
			if e != nil {
				return false
			}
		}
		return true
	})
	return
}
Esempio n. 2
0
func (db *T) PutKey(alias string, key *ecdsa.PrivateKey, main bool) error {
	dbtx, err := db.DB.Begin(true)
	success := false
	defer func() {
		if success {
			dbtx.Commit()
		} else {
			dbtx.Rollback()
		}
	}()
	if err != nil {
		return err
	}
	bucket, err := dbtx.CreateBucketIfNotExists([]byte("keys"))
	if err != nil {
		return err
	}

	encodedKey, err := keys.EncodeECDSAPrivateKey(key)
	if err != nil {
		return err
	}

	err = bucket.Put([]byte(alias), encodedKey)

	if err != nil {
		return err
	}
	if main {
		err = bucket.Put([]byte("main"), []byte(alias))
		if err != nil {
			return err
		}
	}
	success = true
	return nil
}