Regex test for numeric asset names

Is there a simple regex test for validating numeric asset names?
Alternatively, a function or algorithm?

FYI, regular alphabetic asset names can be validated like this:

I think you could look at the CP and CW source code, there should be a validation regex for alphanumeric assets in there.

Edit: maybe this helps

  1. Protocol spec: integers between 26^12 + 1 and 256^8 (inclusive), prefixed with β€˜A’ (http://counterparty.io/docs/protocol_specification/)

  2. Validation: https://github.com/CounterpartyXCP/counterparty-lib/blob/master/counterpartylib/test/fixtures/vectors.py#L1299

If asset name starts with A, cut off the leftmost character A and check if the value of remaining string is larger than the lower bound and equal or smaller than the higher bound. If true, it’s valid.

I think I got it here. Tested with all edge cases and seems to work, at least if the min and max assumptions are correct.

I did not use BigInt because one does not always want to import a library.

function isValidAsset(asset) {
	if (isValidAlphabeticAsset(asset) || isValidNumericAsset(asset)) return true;
	return false;
}

function isValidAlphabeticAsset(asset) {
	//4-12 chars, cannot start with A
	//A few old ones have 13 or 14 chars
	if (asset.match(/^[B-Z][A-Z]{3,11}$/) == null) return false;
	return true;
}

function isValidNumericAsset(asset) {
	//'A' followed by a really large number
	//Min = 26^12+1 =    95,428,956,661,682,177
	//Max = 2^64-1 = 18,446,744,073,709,551,615
        while (asset.substring(0,2) == 'A0') asset = asset[0] + asset.substring(2);
 	if (asset.length>21) return false;
	if (asset.length<18) return false;
	if (asset[0] != 'A') return false;
	if (asset.substring(1).match(/[^0-9]/) != null) return false;
	if (asset.length==18 && asset.substring(1,9)<95428956) return false;
	if (asset.length==18 && asset.substring(1,9)==95428956  && asset.substring(9)<661682177) return false;
	if (asset.length==21 && asset.substring(1,10)>184467440) return false;
	if (asset.length==21 && asset.substring(1,10)==184467440  && asset.substring(10)>73709551615) return false;
	return true;
}
1 Like