func (c *client) RegisterUser(username, address string) (OneNameErrorResponse, error) { _, _, err := base58.CheckDecode(address) if err != nil { return OneNameErrorResponse{}, errors.New("Request not sent. The address provided is not a valid bitcoin address: " + err.Error()) } url := c.BaseUrl + "/users" payload := OneNamePost{RegisterUserPost{username, address}, BroadcastTransactionPost{}} return c.PostRequest(url, payload) }
func TestBase58Check(t *testing.T) { for x, test := range checkEncodingStringTests { // test encoding if res := base58.CheckEncode([]byte(test.in), test.version); res != test.out { t.Errorf("CheckEncode test #%d failed: got %s, want: %s", x, res, test.out) } // test decoding res, version, err := base58.CheckDecode(test.out) if err != nil { t.Errorf("CheckDecode test #%d failed with err: %v", x, err) } else if version != test.version { t.Errorf("CheckDecode test #%d failed: got version: %d want: %d", x, version, test.version) } else if string(res) != test.in { t.Errorf("CheckDecode test #%d failed: got: %s want: %s", x, res, test.in) } } // test the two decoding failure cases // case 1: checksum error _, _, err := base58.CheckDecode("3MNQE1Y") if err != base58.ErrChecksum { t.Error("Checkdecode test failed, expected ErrChecksum") } // case 2: invalid formats (string lengths below 5 mean the version byte and/or the checksum // bytes are missing). testString := "" for len := 0; len < 4; len++ { // make a string of length `len` _, _, err = base58.CheckDecode(testString) if err != base58.ErrInvalidFormat { t.Error("Checkdecode test failed, expected ErrInvalidFormat") } } }
// This example demonstrates how to decode Base58Check encoded data. func ExampleCheckDecode() { // Decode an example Base58Check encoded data. encoded := "1A1zP1eP5QGefi2DMPTfTL5SLmv7DivfNa" decoded, version, err := base58.CheckDecode(encoded) if err != nil { fmt.Println(err) return } // Show the decoded data. fmt.Printf("Decoded data: %x\n", decoded) fmt.Println("Version Byte:", version) // Output: // Decoded data: 62e907b15cbf27d5425399ebf6f0fb50ebb88f18 // Version Byte: 0 }
func (c *client) GetNames(address string) (NamesResponse, error) { var jsonObj NamesResponse _, _, err := base58.CheckDecode(address) if err != nil { return jsonObj, errors.New("Address must be a valid cryptocurrency address. Instead it is " + address + err.Error()) } url := c.BaseUrl + "/addresses/" + address + "/names" jsonBytes, err := c.GetRequest(url) if err != nil { return jsonObj, err } err = json.Unmarshal(jsonBytes, &jsonObj) if err != nil { return jsonObj, JSONRead(err, jsonBytes) } if jsonObj.Error != nil { err = errors.New("Error: " + jsonObj.Error.Type + " - " + jsonObj.Error.Message) } return jsonObj, err }
// DecodeAddress decodes the string encoding of an address and returns // the Address if addr is a valid encoding for a known address type. // // The bitcoin network the address is associated with is extracted if possible. // When the address does not encode the network, such as in the case of a raw // public key, the address will be associated with the passed defaultNet. func DecodeAddress(addr string, defaultNet *chaincfg.Params) (Address, error) { // Serialized public keys are either 65 bytes (130 hex chars) if // uncompressed/hybrid or 33 bytes (66 hex chars) if compressed. if len(addr) == 130 || len(addr) == 66 { serializedPubKey, err := hex.DecodeString(addr) if err != nil { return nil, err } return NewAddressPubKey(serializedPubKey, defaultNet) } // Switch on decoded length to determine the type. decoded, netID, err := base58.CheckDecode(addr) if err != nil { if err == base58.ErrChecksum { return nil, ErrChecksumMismatch } return nil, errors.New("decoded address is of unknown format") } switch len(decoded) { case ripemd160.Size: // P2PKH or P2SH isP2PKH := chaincfg.IsPubKeyHashAddrID(netID) isP2SH := chaincfg.IsScriptHashAddrID(netID) switch hash160 := decoded; { case isP2PKH && isP2SH: return nil, ErrAddressCollision case isP2PKH: return newAddressPubKeyHash(hash160, netID) case isP2SH: return newAddressScriptHashFromHash(hash160, netID) default: return nil, ErrUnknownAddressType } default: return nil, errors.New("decoded address is of unknown size") } }