Пример #1
0
func TestBase58(t *testing.T) {
	// Base58Encode tests
	for x, test := range stringTests {
		tmp := []byte(test.in)
		if res := btcutil.Base58Encode(tmp); res != test.out {
			t.Errorf("Base58Encode test #%d failed: got: %s want: %s",
				x, res, test.out)
			continue
		}
	}

	// Base58Decode tests
	for x, test := range hexTests {
		b, err := hex.DecodeString(test.in)
		if err != nil {
			t.Errorf("hex.DecodeString failed failed #%d: got: %s", x, test.in)
			continue
		}
		if res := btcutil.Base58Decode(test.out); bytes.Equal(res, b) != true {
			t.Errorf("Base58Decode test #%d failed: got: %q want: %q",
				x, res, test.in)
			continue
		}
	}

	// Base58Decode with invalid input
	for x, test := range invalidStringTests {
		if res := btcutil.Base58Decode(test.in); string(res) != test.out {
			t.Errorf("Base58Decode invalidString test #%d failed: got: %q want: %q",
				x, res, test.out)
			continue
		}
	}
}
Пример #2
0
// Loops through an given amount of outputs and locate recipient address
// with the required sequence number
func locateRecipientAddress(outputs []Output, seqNumber byte, checkAll bool) (address string, err error) {
	var exodusValue int64
	for _, output := range outputs {
		if output.Addr == ExodusAddress {
			exodusValue = output.Value
		}
	}

	for _, output := range outputs {
		if output.Addr == ExodusAddress || (output.Value != exodusValue && !checkAll) {
			continue
		}

		rawData := btcutil.Base58Decode(output.Addr)
		sequence := rawData[1]

		log.Println("Address", output.Addr, "has sequence", sequence)

		if sequence == seqNumber+1 {
			if address != "" {
				err = errors.New("Multiple recipients found, invalidating")
				return
			}
			log.Println("Located recipient address:", address)
			address = output.Addr
		}
	}
	return
}
Пример #3
0
// NewKeyFromString returns a new extended key instance from a base58-encoded
// extended key.
func NewKeyFromString(key string) (*ExtendedKey, error) {
	// The base58-decoded extended key must consist of a serialized payload
	// plus an additional 4 bytes for the checksum.
	decoded := btcutil.Base58Decode(key)
	if len(decoded) != serializedKeyLen+4 {
		return nil, ErrInvalidKeyLen
	}

	// The serialized format is:
	//   version (4) || depth (1) || parent fingerprint (4)) ||
	//   child num (4) || chain code (32) || key data (33) || checksum (4)

	// Split the payload and checksum up and ensure the checksum matches.
	payload := decoded[:len(decoded)-4]
	checkSum := decoded[len(decoded)-4:]
	expectedCheckSum := btcwire.DoubleSha256(payload)[:4]
	if !bytes.Equal(checkSum, expectedCheckSum) {
		return nil, ErrBadChecksum
	}

	// Deserialize each of the payload fields.
	version := payload[:4]
	depth := uint16(payload[4:5][0])
	parentFP := payload[5:9]
	childNum := binary.BigEndian.Uint32(payload[9:13])
	chainCode := payload[13:45]
	keyData := payload[45:78]

	// The key data is a private key if it starts with 0x00.  Serialized
	// compressed pubkeys either start with 0x02 or 0x03.
	isPrivate := keyData[0] == 0x00
	if isPrivate {
		// Ensure the private key is valid.  It must be within the range
		// of the order of the secp256k1 curve and not be 0.
		keyData = keyData[1:]
		keyNum := new(big.Int).SetBytes(keyData)
		if keyNum.Cmp(btcec.S256().N) >= 0 || keyNum.Sign() == 0 {
			return nil, ErrUnusableSeed
		}
	} else {
		// Ensure the public key parses correctly and is actually on the
		// secp256k1 curve.
		_, err := btcec.ParsePubKey(keyData, btcec.S256())
		if err != nil {
			return nil, err
		}
	}

	return newExtendedKey(version, keyData, chainCode, parentFP, depth,
		childNum, isPrivate), nil
}
Пример #4
0
// Decodes Class A - Simple Sends
func DecodeFromAddress(address string) SimpleSend {
	log.Println("Decoding address", address)

	rawData := btcutil.Base58Decode(address)

	log.Println("Base58 decoded data:", rawData)

	sequence := rawData[1]
	log.Println("Sequence", sequence)

	// Takes a byte array value and makes it an integer.
	// i.e. [0,0,1,2] becomes 257
	transactionType := binary.BigEndian.Uint32(rawData[2:6])
	log.Println("Transaction type:", transactionType)

	currencyId := binary.BigEndian.Uint32(rawData[6:10])
	log.Println("Currency id:", currencyId)

	amount := binary.BigEndian.Uint64(rawData[10:18])
	log.Println("Amount:", amount)

	ss := SimpleSend{Amount: amount, CurrencyId: currencyId, TransactionType: transactionType, Sequence: sequence}
	return ss
}
Пример #5
0
func GetTypeFromAddress(address string) MsgType {
	rawData := btcutil.Base58Decode(address)

	return MsgType(binary.BigEndian.Uint32(rawData[2:6]))
}