Ejemplo n.º 1
0
func AssetFromDB(typ xdr.AssetType, code string, issuer string) (result xdr.Asset, err error) {
	switch typ {
	case xdr.AssetTypeAssetTypeNative:
		result, err = xdr.NewAsset(xdr.AssetTypeAssetTypeNative, nil)
	case xdr.AssetTypeAssetTypeCreditAlphanum4:
		var (
			an      xdr.AssetAlphaNum4
			decoded []byte
			pkey    xdr.Uint256
		)

		copy(an.AssetCode[:], []byte(code))
		decoded, err = strkey.Decode(strkey.VersionByteAccountID, issuer)
		if err != nil {
			return
		}

		copy(pkey[:], decoded)
		an.Issuer, err = xdr.NewAccountId(xdr.CryptoKeyTypeKeyTypeEd25519, pkey)
		if err != nil {
			return
		}
		result, err = xdr.NewAsset(xdr.AssetTypeAssetTypeCreditAlphanum4, an)
	case xdr.AssetTypeAssetTypeCreditAlphanum12:
		var (
			an      xdr.AssetAlphaNum12
			decoded []byte
			pkey    xdr.Uint256
		)

		copy(an.AssetCode[:], []byte(code))
		decoded, err = strkey.Decode(strkey.VersionByteAccountID, issuer)
		if err != nil {
			return
		}

		copy(pkey[:], decoded)
		an.Issuer, err = xdr.NewAccountId(xdr.CryptoKeyTypeKeyTypeEd25519, pkey)
		if err != nil {
			return
		}
		result, err = xdr.NewAsset(xdr.AssetTypeAssetTypeCreditAlphanum12, an)
	}

	return
}
Ejemplo n.º 2
0
// Parse constructs a new KP from the provided string, which should be either
// an address, or a seed.  If the provided input is a seed, the resulting KP
// will have signing capabilities.
func Parse(addressOrSeed string) (KP, error) {
	_, err := strkey.Decode(strkey.VersionByteAccountID, addressOrSeed)
	if err == nil {
		return &FromAddress{addressOrSeed}, nil
	}

	if err != strkey.ErrInvalidVersionByte {
		return nil, err
	}

	_, err = strkey.Decode(strkey.VersionByteSeed, addressOrSeed)
	if err == nil {
		return &Full{addressOrSeed}, nil
	}

	return nil, err
}
Ejemplo n.º 3
0
func GenerateKeyFromSeed(seed string) (publicKey PublicKey, privateKey PrivateKey, err error) {
	decoded, err := strkey.Decode(strkey.VersionByteSeed, seed)

	if err != nil {
		return
	}

	rawSeed, err := NewRawSeed(decoded)

	if err != nil {
		return
	}

	return GenerateKeyFromRawSeed(rawSeed)
}
Ejemplo n.º 4
0
// GetAddress retrieves a stellar address.  It confirms the value loaded is a
// valid stellar address, setting an invalid field error if it is not.
func (base *Base) GetAddress(name string) (result string) {
	if base.Err != nil {
		return
	}

	result = base.GetString(name)

	_, err := strkey.Decode(strkey.VersionByteAccountID, result)

	if err != nil {
		base.SetInvalidField(name, err)
	}

	return result
}
Ejemplo n.º 5
0
// AddressToAccountId converts the provided address into a xdr.AccountId
func AddressToAccountId(address string) (result xdr.AccountId, err error) {

	bytes, err := strkey.Decode(strkey.VersionByteAccountID, address)

	if err != nil {
		return
	}

	var raw xdr.Uint256
	copy(raw[:], bytes)
	pk, err := xdr.NewPublicKey(xdr.CryptoKeyTypeKeyTypeEd25519, raw)
	if err != nil {
		return
	}

	result = xdr.AccountId(pk)

	return
}
Ejemplo n.º 6
0
// Valid returns true if the webhook is valid
func (hook *Webhook) Valid() error {
	if hook.URL == nil {
		return errors.New("invalid webhook: empty url")
	}

	_, err := strkey.Decode(
		strkey.VersionByteAccountID,
		string(hook.DestinationFilter),
	)
	if err != nil {
		return errors.New("invalid webhook: bad destination filter")
	}

	if hook.MemoFilter == nil {
		return nil
	}

	return hook.MemoFilter.Valid()
}
Ejemplo n.º 7
0
// SetAddress modifies the receiver, setting it's value to the AccountId form
// of the provided address.
func (aid *AccountId) SetAddress(address string) error {
	if aid == nil {
		return nil
	}

	raw, err := strkey.Decode(strkey.VersionByteAccountID, address)
	if err != nil {
		return err
	}

	if len(raw) != 32 {
		return errors.New("invalid address")
	}

	var ui Uint256
	copy(ui[:], raw)

	*aid, err = NewAccountId(CryptoKeyTypeKeyTypeEd25519, ui)

	return err
}
Ejemplo n.º 8
0
// GetAccountID retireves an xdr.AccountID by attempting to decode a stellar
// address at the provided name.
func (base *Base) GetAccountID(name string) (result xdr.AccountId) {
	raw, err := strkey.Decode(strkey.VersionByteAccountID, base.GetString(name))

	if base.Err != nil {
		return
	}

	if err != nil {
		base.SetInvalidField(name, err)
		return
	}

	var key xdr.Uint256
	copy(key[:], raw)

	result, err = xdr.NewAccountId(xdr.CryptoKeyTypeKeyTypeEd25519, key)
	if err != nil {
		base.SetInvalidField(name, err)
		return
	}

	return
}
Ejemplo n.º 9
0
func VerifyGAddress(addr string) error {
	_, err := strkey.Decode(strkey.VersionByteAccountID, addr)
	return err
}
Ejemplo n.º 10
0
func VerifySAddress(addr string) error {
	_, err := strkey.Decode(strkey.VersionByteSeed, addr)
	return err
}