@@ -12,7 +12,7 @@ import { loadTests } from "./utils.js"
1212import type { TestCaseNamehash , TestCaseSolidityHash } from "./types.js" ;
1313
1414
15- // import { dnsEncode, isValidName, namehash } from "../index.js";
15+ import { dnsEncode } from "../index.js" ;
1616
1717describe ( "Tests Namehash" , function ( ) {
1818 const tests = loadTests < TestCaseNamehash > ( "namehash" ) ;
@@ -141,6 +141,58 @@ describe("Tests DNS Names", function() {
141141});
142142*/
143143
144+ interface TestCaseDnsEncode {
145+ name : string ;
146+ length ?: number ;
147+ result ?: string ;
148+ error ?: string ;
149+ }
150+
151+ describe ( "Test dnsEncode" , function ( ) {
152+
153+ const tests : Array < TestCaseDnsEncode > = [
154+ { name : "ricmoo.com" , result : "0x067269636d6f6f03636f6d00" } ,
155+ { name : "ricmoo.com" , length : 5 , error : "exceeds 5 bytes" } ,
156+ {
157+ name : "a-very-long-label-without-a-length-override-foo-12345678901234567890" ,
158+ error : "exceeds 63 bytes"
159+ } ,
160+ {
161+ name : "a-very-long-label-with-a-length-override-to-255-foo-12345678901234567890" ,
162+ length : 255 , result : "0x48612d766572792d6c6f6e672d6c6162656c2d776974682d612d6c656e6774682d6f766572726964652d746f2d3235352d666f6f2d313233343536373839303132333435363738393000"
163+ } ,
164+ ] ;
165+
166+ for ( const test of tests ) {
167+ it ( `tests dnsEncode: ${ test . name } ` , function ( ) {
168+ if ( test . error ) {
169+
170+ assert . throws ( ( ) => {
171+ let result ;
172+ if ( test . length != null ) {
173+ result = dnsEncode ( test . name , test . length ) ;
174+ } else {
175+ result = dnsEncode ( test . name ) ;
176+ }
177+ console . log ( "result" , result ) ;
178+
179+ } , ( error ) => {
180+ return ( isError ( error , "INVALID_ARGUMENT" ) &&
181+ error . argument === "name" && error . value === test . name &&
182+ error . message . indexOf ( test . error || "" ) >= 0 ) ;
183+ } ) ;
184+
185+ } else {
186+ if ( test . length != null ) {
187+ assert . equal ( dnsEncode ( test . name , test . length ) , test . result , "dnsEncode(name, length)" ) ;
188+ } else {
189+ assert . equal ( dnsEncode ( test . name ) , test . result , "dnsEncode(name)" ) ;
190+ }
191+ }
192+ } ) ;
193+ }
194+ } ) ;
195+
144196describe ( "Test EIP-191 Personal Message Hash" , function ( ) {
145197 const tests = [
146198 {
0 commit comments